2019-03-30 12:09:09 +00:00
# SyncSortedSet
2019-06-25 06:00:42 +00:00
`SyncSortedSet` are sets similar to C\# [SortedSet\<T\> ](https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.sortedset-1 ) that synchronize their contents from the server to the clients.
2019-03-30 16:51:06 +00:00
2019-06-25 06:00:42 +00:00
Unlike SyncHashSets, all elements in a SyncSortedSet are sorted when they are inserted. Please note this has some performance implications.
2019-03-30 16:51:06 +00:00
2019-09-21 03:34:03 +00:00
A SyncSortedSet can contain any [supported mirror type ](../DataTypes.md )
2019-03-30 16:51:06 +00:00
## Usage
2020-10-02 18:56:20 +00:00
Add a SyncSortedSet field to your NetworkBehaviour class. For example:
2019-03-30 16:51:06 +00:00
2019-07-07 05:52:37 +00:00
```cs
2019-03-30 16:51:06 +00:00
class Player : NetworkBehaviour {
2020-10-02 18:56:20 +00:00
readonly SyncSortedSet< string > skills = new SyncSortedSet< string > ();
2019-03-30 16:51:06 +00:00
int skillPoints = 10;
[Command]
public void CmdLearnSkill(string skillName)
{
if (skillPoints > 1)
{
skillPoints--;
skills.Add(skillName);
}
}
}
```
2019-08-19 21:55:49 +00:00
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.
2019-08-20 13:29:55 +00:00
> 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.</p>
> Note SyncSets must be initialized in the constructor, not in Startxxx(). You can make them readonly to ensure correct usage.
2019-03-30 16:51:06 +00:00
2019-07-07 05:52:37 +00:00
```cs
2019-03-30 16:51:06 +00:00
class Player : NetworkBehaviour
{
2020-10-02 18:56:20 +00:00
readonly SyncSortedSet< string > buffs = new SyncSortedSet< string > ();
2019-03-30 16:51:06 +00:00
// this will add the delegate on the client.
// Use OnStartServer instead if you want it on the server
public override void OnStartClient()
{
buffs.Callback += OnBuffsChanged;
}
2020-10-02 18:56:20 +00:00
void OnBuffsChanged(SyncSortedSet< string > .Operation op, string buff)
2019-03-30 16:51:06 +00:00
{
switch (op)
{
2020-10-02 18:56:20 +00:00
case SyncSortedSet< string > .Operation.OP_ADD:
2019-03-30 16:51:06 +00:00
// we added a buff, draw an icon on the character
break;
2020-10-02 18:56:20 +00:00
case SyncSortedSet< string > .Operation.OP_CLEAR:
2019-03-30 16:51:06 +00:00
// clear all buffs from the character
break;
2020-10-02 18:56:20 +00:00
case SyncSortedSet< string > .Operation.OP_REMOVE:
2019-03-30 16:51:06 +00:00
// We removed a buff from the character
break;
}
}
}
```