Unity Netcode for gameobjects, create custom networkclass with array

Hello, i am trying to create custom class with this interface INetworkSerializable

i am trying to serialize custom array that have 3 variables (int, int , int)
when i try to implement the interface
public void NetworkSerialize(BufferSerializer serializer) where T : IReaderWriter

this is my classes:

public struct InventoryIdItemData : INetworkSerializable
{
    public int Slot;
    public int ItemId;
    public int Number;

    public InventoryIdItemData(int slot, int itemId, int number)
    {
        Slot = slot;
        ItemId = itemId;
        Number = number;
    }

    public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter
    {
        serializer.SerializeValue(ref Slot);
        serializer.SerializeValue(ref ItemId);
        serializer.SerializeValue(ref Number);
    }
}

and this:

    public class PlayerInventoryIdItem : INetworkSerializable
    {
        public InventoryIdItemData[] InventoryIdItemsData = new InventoryIdItemData[32];

        public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter
        {
            int count = InventoryIdItemsData.Length;
            serializer.SerializeValue(ref count);

            for (int i = 0; i < count; i++)
            {
                InventoryIdItemData item = InventoryIdItemsData[i];
                item.NetworkSerialize(serializer);
                InventoryIdItemsData[i] = item;
            }
        }
    }

is it the way to do it?
or there is another way?

Would appreciate help! thank you

In your first example, the ref variables are class scoped variables, but in your second example, the “count” is a local variable. Could this be an issue? Otherwise your first example looks correct.

The best way to check if you did it correctly is simply to test it in your game and see if it works.

It is work, but i have some weird issues that i am not understand(some illuminati errors , chat GPT don’t know how to handle them).

any way, i left this problem to another day…
so if you have any solution:
how to implement NetworkSerialize method(for array and lists) in the right wayת i would appreciate for help,

Your best bet is just to follow the documentation:

The documentation not show how to implement INetworkSerializable interface(for array and lists).

I’m not sure you need INetworkSerializable for those. Certainly if you are using a list you can just use the NetworkList:
https://docs.unity.cn/Packages/com.unity.netcode.gameobjects@1.0/api/Unity.Netcode.NetworkList-1.html

thank you!
I’ll try it, I’ll let you know if it works.

1 Like

Privacy & Terms