doc: Updated RemoteActions

This commit is contained in:
Chris Langsenkamp 2020-06-09 10:46:54 -04:00
parent 2c9030333c
commit fdc65ebe29

View File

@ -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<Player>().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
{