SyncArray

A Synchronized Array, more commonly referred to as a "SyncArray", is easily definable in your code. It will handle automatically aligning the contents of an array between all players.

Working with SyncArrays is as easy as using a regular array, you have only to be mindful of who has authority over it.

SyncArrays are built with the Network Module setup, meaning that you have to initialize it. Below is a usage example:

//Creates a new instance of the array
//20 sets the initial length of the array
//`true` means it is owner auth. 
public SyncArray<int> syncArray = new(20, true);

protected override void OnSpawned()
{
    //Subscribing to changes made to the array
    syncArray.onChanged += OnArrayChange;
}

private void OnArrayChange(SyncArrayChange<int> change)
{
    //This is now called for everyone when the array changes.
    //It will log out the value, index and operation
    Debug.Log(change);
}

private void ChangeMyArray()
{
    //This will change or add a value
    syncArray [0] = 69;
    
    //Resized the array to 15 elements
    syncArray.Length = 15;
}

SyncArrayChange

When working with the SyncArrayChange which comes with the onChanged callback, you have some basic information available to you.

The .operation tells you what the change is, that can be: Set, Cleared, Resized.

The .value is the new value which it has been changed. This is of type T to match the type of the SyncArray.

The .index tells you at what index this change is relevant.

Last updated