NetworkMap<TKey, TValue>
A keyed collection replicated from the authoritative worker to clients and ghost workers, sending only what changed.
Nebula · Packages/com.1by3.nebula/Runtime/Core/NetworkMap.cspublic sealed class NetworkMap<TKey, TValue> : NetworkMapBase, IReadOnlyDictionary<TKey, TValue>A keyed collection replicated from the authoritative worker to clients and ghost workers, sending only what changed. Declare it on a NetworkBehaviour like a NetworkVariable<T>, and add PersistAttribute to save it:
[Persist] public NetworkMap<int, ItemStack> Slots = new NetworkMap<int, ItemStack>();Only the authority may write it. Each tick, the keys that changed go out as one reliable message; a client that arrives later is given the current contents by its gateway, then the same changes as everyone else. Keys and values use NetworkSerialization, so any type a NetworkVariable<T> can hold works. A map is capped at NetworkMapBase.Capacity encoded bytes (60 KB by default).
Constructors
NetworkMap
public NetworkMap();
public NetworkMap(IEqualityComparer<TKey> comparer, int capacityBytes = 0);| Parameter | Description |
|---|---|
comparer | How keys are compared; the default comparer of TKey when null. |
capacityBytes | The most the map may hold, encoded (NetworkMapBase.Capacity); 0 for NetworkMapBase.MaxEncodedBytes. |
Properties
Count
public override int Count { get; }EncodedBytes
public override int EncodedBytes { get; }Keys
public IEnumerable<TKey> Keys { get; }Values
public IEnumerable<TValue> Values { get; }this[]
public TValue this[TKey key]
{
get => _map[key];
set
{
if (!TrySet(key, value))
throw new InvalidOperationException(CapMessage(key));
}
}Get a value, or (authority) add or replace one. Setting a value equal to the current one does nothing. Throws InvalidOperationException when the map would pass its size cap.
Events
OnChanged
public event Action<NetworkMapChange<TKey, TValue>> OnChanged;Raised on every copy (the authority as it writes, others as they apply what it sent) once per key that was added, updated or removed.
Methods
ContainsKey
public bool ContainsKey(TKey key);TryGetValue
public bool TryGetValue(TKey key, out TValue value);GetEnumerator
public Dictionary<TKey, TValue>.Enumerator GetEnumerator();Add
public void Add(TKey key, TValue value);Add a key that is not in the map (authority). Throws ArgumentException if it is.
TrySet
public bool TrySet(TKey key, TValue value);Add or replace a value (authority). Returns false, and changes nothing, when the map would pass NetworkMapBase.Capacity; true otherwise, including when the write was ignored because this copy may not write.
Remove
public bool Remove(TKey key);Remove a key (authority). Returns whether it was there.
Clear
public void Clear();Remove every key (authority). Raises NetworkMapChangeKind.Removed for each.
SetDirty
public bool SetDirty(TKey key);Send the current value of key again (authority): for a reference-typed value that was changed in place, which the map cannot see. Re-measures it against the size cap; returns false, sending nothing, when it no longer fits.
ToString
public override string ToString();