M
M
Mirror
Search…
SyncHashSet
SyncHashSet are sets similar to C# HashSet that synchronize their contents from the server to the clients.
A SyncHashSet can contain any supported mirror type

Usage

SyncHashSet must be declared readonly and initialized in the constructor.
Add a SyncHashSet field to your NetworkBehaviour class. For example:
1
public class Player : NetworkBehaviour
2
{
3
[SerializeField]
4
public readonly SyncHashSet<string> skills = new SyncHashSet<string>();
5
6
int skillPoints = 10;
7
8
[Command]
9
public void CmdLearnSkill(string skillName)
10
{
11
if (skillPoints > 1)
12
{
13
skillPoints--;
14
skills.Add(skillName);
15
}
16
}
17
}
Copied!
You can also detect when a SyncHashSet 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 populated, so you will not get a call for the initial data, only updates.
1
public class Player : NetworkBehaviour
2
{
3
[SerializeField]
4
public readonly SyncHashSet<string> buffs = new SyncHashSet<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 SyncHashSet payload
13
foreach (string buff in buffs)
14
OnBuffsChanged(SyncSet<string>.Operation.OP_ADD, buff);
15
}
16
17
// SyncHashSet inherits from SyncSet so use SyncSet here
18
void OnBuffsChanged(SyncSet<string>.Operation op, string buff)
19
{
20
switch (op)
21
{
22
case SyncSet<string>.Operation.OP_ADD:
23
// Added a buff to the character
24
break;
25
case SyncSet<string>.Operation.OP_REMOVE:
26
// Removed a buff from the character
27
break;
28
case SyncSet<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