M
M
Mirror
Search…
SyncLists
SyncLists are array based lists similar to C# List that synchronize their contents from the server to the clients.
A SyncList can contain any supported mirror type.

Differences with UNET

UNET also supports SyncLists, but we have redesigned them to make them more efficient and easier to use. Some of the key differences include:
  • In UNET, SyncLists were synchronized immediately when they changed. If you add 10 elements, that means 10 separate messages. Mirror synchronizes SyncLists with the SyncVars. The 10 elements and other SyncVars are batched together into a single message. Mirror also respects the sync interval when synchronizing lists.
  • In UNET if you want a list of structs, you have to use SyncListStruct, we changed it to just SyncList
  • In UNET the Callback is a delegate. In Mirror we changed it to an event, so that you can add many subscribers.
  • In UNET the Callback tells you the operation and index. In Mirror, the callback also receives an item. We made this change so that we could tell what item was removed.
  • In UNET you must create a class that inherits from SyncList. In Mirror you can just use SyncList directly (starting with version 20.0.0)

Usage

Add a SyncList field to your NetworkBehaviour class.
SyncList must be declared readonly and initialized in the constructor.
1
public struct Item
2
{
3
public string name;
4
public int amount;
5
public Color32 color;
6
}
7
8
public class Player : NetworkBehaviour
9
{
10
public readonly SyncList<Item> inventory = new SyncList<Item>();
11
12
public int coins = 100;
13
14
[Command]
15
public void CmdPurchase(string itemName)
16
{
17
if (coins > 10)
18
{
19
coins -= 10;
20
Item item = new Item
21
{
22
name = "Sword",
23
amount = 3,
24
color = new Color32(125, 125, 125, 255)
25
};
26
27
// during next synchronization, all clients will see the item
28
inventory.Add(item);
29
}
30
}
31
}
Copied!
You can also detect when a SyncList changes in the client or server. This is useful for refreshing your character when you add equipment or determining when you need to update your database. Subscribe to the Callback event typically during Start, OnClientStart, or OnServerStart for that.
Note that by the time you subscribe, the list will already be populated, so you will not get a call for the initial data, only updates.
1
class Player : NetworkBehaviour {
2
3
public override void OnStartClient()
4
{
5
inventory.Callback += OnInventoryUpdated;
6
7
// Process initial SyncList payload
8
for (int index = 0; index < inventory.Count; index++)
9
OnInventoryUpdated(SyncList<Item>.Operation.OP_ADD, index, new Item(), inventory[index]);
10
}
11
12
void OnInventoryUpdated(SyncList<Item>.Operation op, int index, Item oldItem, Item newItem)
13
{
14
switch (op)
15
{
16
case SyncList<Item>.Operation.OP_ADD:
17
// index is where it was added into the list
18
// newItem is the new item
19
break;
20
case SyncList<Item>.Operation.OP_INSERT:
21
// index is where it was inserted into the list
22
// newItem is the new item
23
break;
24
case SyncList<Item>.Operation.OP_REMOVEAT:
25
// index is where it was removed from the list
26
// oldItem is the item that was removed
27
break;
28
case SyncList<Item>.Operation.OP_SET:
29
// index is of the item that was changed
30
// oldItem is the previous value for the item at the index
31
// newItem is the new value for the item at the index
32
break;
33
case SyncList<Item>.Operation.OP_CLEAR:
34
// list got cleared
35
break;
36
}
37
}
38
}
Copied!
Last modified 5d ago