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. publicSyncArray<int>syncArray=new(20,true);protectedoverridevoidOnSpawned(){ //Subscribing to changes made to the arraysyncArray.onChanged+=OnArrayChange;}privatevoidOnArrayChange(SyncArrayChange<int>change){ //This is now called for everyone when the array changes. //It will log out the value, index and operationDebug.Log(change);}privatevoidChangeMyArray(){ //This will change or add a valuesyncArray[0]=69; //Resized the array to 15 elementssyncArray.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.