M
M
Mirror
Search…
SyncSortedSet
SyncSortedSet are sets similar to C# SortedSet that synchronize their contents from the server to the clients.
Unlike SyncHashSets, all elements in a SyncSortedSet are sorted when they are inserted. Please note this has some performance implications.
A SyncSortedSet can contain any supported mirror type

Usage

SyncSortedSet must be declared readonly and initialized in the constructor.
Add a SyncSortedSet field to your NetworkBehaviour class. For example:
1
class Player : NetworkBehaviour
2
{
3
public readonly SyncSortedSet<string> skills = new SyncSortedSet<string>();
4
int skillPoints = 10;
5
6
[Command]
7
public void CmdLearnSkill(string skillName)
8
{
9
if (skillPoints > 1)
10
{
11
skillPoints--;
12
skills.Add(skillName);
13
}
14
}
15
}
Copied!
You can also detect when a SyncSortedSet changes. This is useful for refreshing your character in the client 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 set will already be initialized, so you will not get a call for the initial data, only updates.
1
class Player : NetworkBehaviour
2
{
3
[SerializeField]
4
public readonly SyncSortedSet<string> buffs = new SyncSortedSet<string>();
5
6
// this will add the delegate on the client.
7
// Use OnStartServer instead if you want it on the server
8
public override void OnStartClient()
9
{
10
buffs.Callback += OnBuffsChanged;
11
12
// Process initial SyncSortedSet payload
13
foreach (string buff in buffs)
14
OnBuffsChanged(SyncSortedSet<string>.Operation.OP_ADD, buff);
15
}
16
17
// SyncSortedSet inherits from SyncSet so use SyncSet here
18
void OnBuffsChanged(SyncSortedSet<string>.Operation op, string buff)
19
{
20
switch (op)
21
{
22
case SyncSortedSet<string>.Operation.OP_ADD:
23
// Added a buff to the character
24
break;
25
case SyncSortedSet<string>.Operation.OP_REMOVE:
26
// Removed a buff from the character
27
break;
28
case SyncSortedSet<string>.Operation.OP_CLEAR:
29
// cleared all buffs from the character
30
break;
31
}
32
}
33
}
Copied!
Last modified 5d ago
Copy link
Contents
Usage