mirror of
https://github.com/MirrorNetworking/Mirror.git
synced 2024-11-18 19:10:32 +00:00
docs: document synclists
This commit is contained in:
parent
9d043182bf
commit
0fc408b3ea
@ -12,6 +12,8 @@ HLAPI SyncLists sent a message for every change immediately. They did not respec
|
|||||||
|
|
||||||
In Mirror SyncList were redesigned. The lists queue up their changes, and the changes are sent as part of the syncvar synchronization. If you add 10 items, then only 1 message is sent with all changes according to the next SyncInterval.
|
In Mirror SyncList were redesigned. The lists queue up their changes, and the changes are sent as part of the syncvar synchronization. If you add 10 items, then only 1 message is sent with all changes according to the next SyncInterval.
|
||||||
|
|
||||||
|
In HLAPI, if you wanted a list of structs, you needed to use `SyncListStruct<MyStructure>`, we changed it to just `SyncList<MyStructure>`
|
||||||
|
|
||||||
We also raised the limit from 32 SyncVars to 64 per NetworkBehavior.
|
We also raised the limit from 32 SyncVars to 64 per NetworkBehavior.
|
||||||
|
|
||||||
A SyncList can only be of the following type
|
A SyncList can only be of the following type
|
||||||
@ -20,39 +22,103 @@ A SyncList can only be of the following type
|
|||||||
- Built-in Unity math type (Vector3, Quaternion, etc)
|
- Built-in Unity math type (Vector3, Quaternion, etc)
|
||||||
- NetworkIdentity
|
- NetworkIdentity
|
||||||
- GameObject with a NetworkIdentity component attached.
|
- GameObject with a NetworkIdentity component attached.
|
||||||
|
- Structure with any of the above
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
Don't modify them in Awake, use OnStartServer or Start. SyncListStructs use Structs. C\# structs are value types, just like int, float, Vector3 etc. You can't do synclist.value = newvalue; You have to copy the element, assign the new value, assign the new element to the synclist. You can use a callback hook like this:
|
Create a class that derives from SyncList<T> for your specific type. This is necesary because Mirror will add methods to that class with the weaver. Then add a SyncList field to your NetworkBehaviour class. For example:
|
||||||
|
|
||||||
```cs
|
```cs
|
||||||
// In this example we are using String
|
|
||||||
// This can also be used for any custom data type
|
|
||||||
SyncListString myStringList;
|
|
||||||
SyncListItem myItemList;
|
|
||||||
|
|
||||||
// this will add the delegates on both server and client.
|
|
||||||
// Use OnStartClient instead if you just want the client to act upon updates
|
|
||||||
void Start()
|
|
||||||
{
|
|
||||||
myStringList.Callback += MyStringCallback;
|
|
||||||
myItemList.Callback += MyItemCallback;
|
|
||||||
}
|
|
||||||
|
|
||||||
void MyStringCallback(SyncListString.Operation op, int index, String item)
|
|
||||||
{
|
|
||||||
// Callback functionality here
|
|
||||||
}
|
|
||||||
|
|
||||||
void MyItemCallback(SyncListItem.Operation op, int index, Item item)
|
|
||||||
{
|
|
||||||
// Callback functionality here
|
|
||||||
}
|
|
||||||
|
|
||||||
public struct Item
|
public struct Item
|
||||||
{
|
{
|
||||||
// Define struct
|
public string name;
|
||||||
|
public int amount;
|
||||||
|
public Color32 color;
|
||||||
}
|
}
|
||||||
|
|
||||||
public class SyncListItem : SyncListSTRUCT<Item> { }
|
class SyncListItem : SyncList<Item> {}
|
||||||
|
|
||||||
|
class Player : NetworkBehaviour {
|
||||||
|
|
||||||
|
SyncListItem inventory;
|
||||||
|
|
||||||
|
public int coins = 100;
|
||||||
|
|
||||||
|
[Command]
|
||||||
|
public void CmdPurchase(string itemName)
|
||||||
|
{
|
||||||
|
if (coins > 10)
|
||||||
|
{
|
||||||
|
coins -= 10;
|
||||||
|
Item item = new Item
|
||||||
|
{
|
||||||
|
name = "Sword",
|
||||||
|
amount = 3,
|
||||||
|
color = new Color32(125,125,125);
|
||||||
|
};
|
||||||
|
|
||||||
|
// during next synchronization, all clients will see the item
|
||||||
|
inventory.Add(item)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
There are some ready made SyncLists you can use:
|
||||||
|
* `SyncListString`
|
||||||
|
* `SyncListFloat`
|
||||||
|
* `SyncListInt`
|
||||||
|
* `SyncListUInt`
|
||||||
|
* `SyncListBool`
|
||||||
|
|
||||||
|
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 initialized, so you will not get a call for the initial data, only updates.
|
||||||
|
|
||||||
|
```cs
|
||||||
|
class Player : NetworkBehaviour {
|
||||||
|
|
||||||
|
SyncListItem inventory;
|
||||||
|
|
||||||
|
// this will add the delegates on both server and client.
|
||||||
|
// Use OnStartClient instead if you just want the client to act upon updates
|
||||||
|
void Start()
|
||||||
|
{
|
||||||
|
myStringList.Callback += OnInventoryUpdated;
|
||||||
|
}
|
||||||
|
|
||||||
|
void OnInventoryUpdated(SyncListItem.Operation op, int index, Item item)
|
||||||
|
{
|
||||||
|
switch (op)
|
||||||
|
{
|
||||||
|
case SyncListItem.Operation.OP_ADD:
|
||||||
|
// index is where it got added in the list
|
||||||
|
// item is the new item
|
||||||
|
break;
|
||||||
|
case SyncListItem.Operation.OP_CLEAR:
|
||||||
|
// list got cleared
|
||||||
|
break;
|
||||||
|
case SyncListItem.Operation.OP_INSERT:
|
||||||
|
// index is where it got added in the list
|
||||||
|
// item is the new item
|
||||||
|
break;
|
||||||
|
case SyncListItem.Operation.OP_REMOVE:
|
||||||
|
// index is where it got removed in the list
|
||||||
|
// item is the item that was removed
|
||||||
|
break;
|
||||||
|
case SyncListItem.Operation.OP_REMOVEAT:
|
||||||
|
// index is where it got removed in the list
|
||||||
|
// item is the item that was removed
|
||||||
|
break;
|
||||||
|
case SyncListItem.Operation.OP_SET:
|
||||||
|
// index is the index of the item that was updated
|
||||||
|
// item is the previous item
|
||||||
|
break;
|
||||||
|
case SyncListItem.Operation.OP_SET:
|
||||||
|
// index is the index of the item that was updated
|
||||||
|
// item is the previous item
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
Loading…
Reference in New Issue
Block a user