Commands

Overview

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.

Design Phase

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:

command FemZombie_Character_SendDamage
  damageValue Int
  staminaBlockCost Float
  staminaRecoveryDelay Float
  ignoreDefense Bool
  hitReaction Bool
  hitPosition Vector3
  sourceConnection Int
  isPlayer Bool

Sending a Command on a Networked Object

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.

var target = enemy.GetComponent<CoherenceSync>();
                    
target.SendCommand("Character.SendDamage", damageValue, 
                staminaBlockCost, staminaRecoveryDelay,
                ignoreDefense, hitReaction, hitPosition, 
                ConnectionId, isPlayer);

Sending a Command on All Instances of a Networked Object

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:

SendCommand(string commandName, bool broadcast, params object[] args)

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.

Receiving a Command

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.

Sending a Command to multiple entities

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.

public void MulticastCommand()
{
    var self = GetComponent<CoherenceSync>();
    var listeners = CoherenceSync.instances.Values;
    
    foreach (var listener in listeners)
    {
        if (!listener || listener == self)
        {
            continue;
        }

        listener.SendCommand("MyScript.ReceiveCommand");
    }
}

// bind to this method via the Bindings window
public void ReceiveCommand()
{
    Debug.Log("Received!");
}

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.

Supported types in Commands

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.

Last updated