Variable Replication

As mentioned in the Replication, there are two ways to synchronize variables.

Note

Whichever method you use, your class needs to be derived from StrixBehaviour, and the StrixReplicator component is attached to the GameObject that your script is attached to.

Sync Fields

You can make the variable an instance field of a class and mark it with the StrixSyncField attribute. The StrixReplicator will automatically synchronize the value of the marked field from the original object on the owner client to replicas on other clients. The field must be serializable.

[StrixSyncField]
public int MyValue = 10;

[StrixSyncField]
public int MySecondValue= 7;

This is a handy way to synchronize a variable in an object, but please note that Strix Unity SDK uses reflection to synchronize these fields, so it is less performant.

On Serialization Methods

Instead of relying on reflection to read and write the values of the fields, you can override two methods, OnStrixSerialize and OnStrixDeserialize, and read and write the values manually using appropriate string or integer keys.

public int MyValue = 10;
public int MySecondValue = 7;

public override void OnStrixSerialize(StrixSerializationProperties properties)
{
    base.OnStrixSerialize(properties);
    properties.Set("MyValue", MyValue);
    properties.Set(-1, MySecondValue);
}

public override void OnStrixDeserialize(StrixSerializationProperties properties)
{
    base.OnStrixDeserialize(properties);
    properties.Get("MyValue", ref MyValue);
    properties.Get(-1, ref MySecondValue);
}

Note

A property key in StrixSerializationProperties acts as an identifier of a replicated property.

You need to use a unique key value (regardless it is a string or an integer) to represent each property. If a same key is used for two properties (even in different classes if attached to a same GameObject), the variable synchronization doesn’t work as expected, and Strix Unity SDK will throw an exception.

If you use an integer key, the key should be a negative value (less than zero), because STRIX internally uses positive integer keys for its own purposes.

Synchronization send-rate

In both cases, the synchronization is automatically invoked at certain intervals depending on the value of sendRate. The value of sendRate is how many times per second the synchronization is performed. Also, the synchronization is not performed if the GameObject or the script is disabled.