Syncing Custom Data + Commands

Hi, sorry if it was already answered elsewhere.

Is there any way for me to sync custom classes like Lists, Dictionaries, or my own class, the inventory class?

When asking on the discord, I have been told to use commands.
So, I have a method:

[Command] public static void ReplicateInventory(Class_Inventory inv){
// code here
}

For some reason, when I check the coherence sync, there is no method syncable.
Why is that? I thought a method had to be public and void? Shouldn’t the method: ReplicateInventory appear in the syncables list of methods?

I tried making it an instance method:
[Command] public void ReplicateInventory(Class_Inventory inv){
// code here
}

Still no results.
Thanks.

Hello! Thanks for posting here in the forums

The issue is that the argument to your command is not a primitive we support: Specification | SDK 1.1 | Unity Multiplayer SDK Documentation | coherence

For the command to work the arguments must be one of the support types. Your Class_Inventory class most likely holds its data in some combination of the primitives, so for instance you can have a method that accepts your Class_Inventory and then sends commands to sync each of the primitives.

Or, you can serialize your entire inventory into e.g. json (or something more compact like a byte array) and sync that. (noting command length limits)

We realize this isn’t super easy or straight forward and we’re exploring ways to make this easier.

What is your use case for this inventory syncing? It can help us inform you towards the best way to handle this.

Hey, thanks for the fast response.

So, I have an item pickup system that I don’t want ANY client to have access to.
It is simulated server side.

The problem begins when the server has no reference to the client’s inventory.
Thats why I want to sync it, because I need the reference to the inventory in order to add the item.

If you are curious, the reference is held in a script attached to the networked client player object.

So all the simulator has to do to access it is to trygetcomponent the script, then access the getter method for the inventory.

Assuming the player’s inventory replicates to the server when the client creates the inventory object, the server should be able to access it from the player object ( If I understand that right )

By the way, do you know of any better way of doing this
Because I don’t want the client tampering with the inventory values either.

Thanks.

Hello, yes if your goal is to have no client tampering with inventory then the inventory data needs to be completely owned by the simulator, rather than the client sending inventory data to the simulator.

Where is your inventory data stored now? Is it ephemeral (e.g. only exists in a single session) or is it stored somewhere and loaded by player authentication?

It is loaded by unity cloud save service. I was considering making inventories server only but I don’t know how to make that work with the simulator.

One method would be to identify the players that join the room or world via authentication, and the simulator requests each of their inventories from Unity Cloud Service. Player clients would then need their inventory sync’d to them.

Here are some docs on our authentication: Authentication Service (Player Accounts) | SDK 1.1 | Unity Multiplayer SDK Documentation | coherence

1 Like