From fdc65ebe29c102ef5575422b46c376b60c0d2d1c Mon Sep 17 00:00:00 2001 From: Chris Langsenkamp Date: Tue, 9 Jun 2020 10:46:54 -0400 Subject: [PATCH] doc: Updated RemoteActions --- doc/Guides/Communications/RemoteActions.md | 36 ++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/doc/Guides/Communications/RemoteActions.md b/doc/Guides/Communications/RemoteActions.md index 238f9fdec..c15fdee96 100644 --- a/doc/Guides/Communications/RemoteActions.md +++ b/doc/Guides/Communications/RemoteActions.md @@ -44,14 +44,46 @@ public class Player : NetworkBehaviour Be careful of sending commands from the client every frame! This can cause a lot of network traffic. -It is possible to send commands from non-player objects that have client authority. These objects must have been spawned with client authority or have authority set with `NetworkIdentity.AssignClientAuthority`. Commands sent from these object are run on the server instance of the object, not on the associated player object for the client. +### Commands and Authority + +It is possible to invoke commands on non-player objects if any of the following are true: + +- The object was spawned with client authority +- The object has client authority set with `NetworkIdentity.AssignClientAuthority` +- the Command has the `ignoreAuthority` option set true. + - You can include an optional `NetworkConnectionToClient sender = null` parameter in the Command method signature and Mirror will fill in the sending client for you. + - Do not try to set a value for this optional parameter...it will be ignored. + +Commands sent from these object are run on the server instance of the object, not on the associated player object for the client. + +```cs +public enum DoorState : byte +{ + Open, Closed +} + +public class Door : NetworkBehaviour +{ + [SyncVar] + public DoorState doorState; + + [Command(ignoreAuthority = true)] + public void CmdSetDoorState(DoorState newDoorState, NetworkConnectionToClient sender = null) + { + if (sender.identity.GetComponent().hasDoorKey) + doorState = newDoorState; + } +} +``` ## ClientRpc Calls -ClientRpc calls are sent from objects on the server to objects on clients. They can be sent from any server object with a NetworkIdentity that has been spawned. Since the server has authority, then there no security issues with server objects being able to send these calls. To make a function into a ClientRpc call, add the [ClientRpc] custom attribute to it, and add the “Rpc” prefix. This function will now be run on clients when it is called on the server. Any arguments will automatically be passed to the clients with the ClientRpc call.. +ClientRpc calls are sent from objects on the server to objects on clients. They can be sent from any server object with a NetworkIdentity that has been spawned. Since the server has authority, then there no security issues with server objects being able to send these calls. To make a function into a ClientRpc call, add the [ClientRpc] custom attribute to it, and add the “Rpc” prefix. This function will now be run on clients when it is called on the server. Any parameters of [allowed data type](../DataTypes.md) will automatically be passed to the clients with the ClientRpc call.. ClientRpc functions must have the prefix “Rpc” and cannot be static. This is a hint when reading code that calls the method - this function is special and is not invoked locally like a normal function. +ClientRpc messages are only sent to observers of an object according to its [Network Visibility](../Visibility.md). Player objects are always obeservers of themselves. In some cases, you may want to exclude the owner client when calling a ClientRpc. This is done with the `excludeOwner` option: `[ClientRpc(excludeOwner = true)]`. + ``` cs public class Player : NetworkBehaviour {