How To Go About Synchronizing A Dictionary With Netcode For Game Objects?

I would like to allow units in my game to store, transfer, and consume resources.

My “vison” is to have the server have the “real” copy of all of the resources, but clients will be able to see what resources a unit has.

The best way I see to do this is to create something similar to mirror’s SyncDictonary, but I have no clue how to even attempt to do this using Netcode For Gameobjects.

(Of course, if you have an alternative better/simpler idea for how I could accomplish my goal, I would very happy if you could share that with me, too! Also, thanks in advance for at least reading my question!)

Hi @Duck_With_Grape ,
I don’t familiar with mirror but the link you shared is like a NetworkVariable and NetworkList. You can take a look in documentation page.

Also NetworkList is good approach for your case.

Basic principle of how a dictionary operates:

// outline of a network dictionary
public class NetworkDictionary<TKey, TValue> : INetworkSerializable
{
NetworkList<TKey> Keys;
NetworkList<TValue> Values;
}

You’d have to:

  • Ensure the keys and values remain in sync (never add/remove just a key or just a value, or sort the lists)
  • Ensure there cannot be duplicate keys by first finding the key in the keys list

Adding a Key-Value pair where the key doesn’t exist yet will Add the key to the keys and the value to the values. Finding the value for the key requires finding the index of the key in the Keys list, then use that index to get the value in the values list.

NetworkVariables support collections, but I’m not a fan of finding changes by comparing the old collection with the new one.

There is also this third party NetworkDictionary that piggybacks onto the Netcode namespace in order to work. It’s quite old and I don’t know if this still works with later versions of NGO.