Commands are network messages sent from one entity to another entity in the networked world. Commands bind to public methods accessible on the GameObject that CoherenceSync sits on.
In the design phase, you can expose public methods the same way you select fields for synchronization: by using the Select Bindings interface on your CoherenceSync object.
Selected public methods will be exposed as network commands in the baking process.
The resulting schema section will look something like this:
To send a command, we call the SendCommand
method on the target CoherenceSync object. We must supply as many parameters as are defined in the target method and the schema.
Sometimes you want to inform not only the entity that has authority, but all the networked instances across all clients. To do that, use the alternative method overload and set it's boolean value to true:
This is useful when you want every client that networks that entity to execute the same logic. Similar to how RPCs work on other networking solutions.
We don't have to do anything special to receive the command. The system will simply call the corresponding method on the target network entity.
If the target is a locally simulated entity, SendCommand
will recognize that and not send a network command, but instead simply call the method directly.
Sometimes you want to inform a bunch of different entities about a change. For example, a explosion impact on a few players. To do so, we have to go through the instances we want to notify, and send commands to each of them.
In this example, a command will get sent to every network entity known to this client. To make it only affect entities within a certain criteria, you need to filter to which CoherenceSync you send the command to on your own.
When a prefab is using reflection there are some restrictions for what types can be sent in a single command:
4 integers
4 floats
4 bools
4 entity references
1 string
If you ever need to send commands containing any other type (like a vector) or a higher quantity (like two strings) you must mark the prefab to use baked code. This will generate a custom command that can handle all the supported primitive types of the coherence SDK.
If a single command is bigger than the supported packet size, it won't work even with baked code. For a good and performant game experience, always try to keep command sizes low.