#Inheritance and Serialization

1 messages · Page 1 of 1 (latest)

digital widget
#

I want to inherit an abstract class which implements INetworkSerializable. My abstract class serializes one field:

public virtual void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter
    {
        serializer.SerializeValue(ref senderId);
    }

And in my subclass class I have:

public override void NetworkSerialize<T>(BufferSerializer<T> serializer)
    {
        base.NetworkSerialize(serializer);
        serializer.SerializeValue(ref exampleField);
    }

Will this work? I don't see why it shouldn't do, but https://docs.unity3d.com/Packages/com.unity.netcode.gameobjects@2.4/manual/advanced-topics/serialization/inetworkserializable.html seems to imply that it won't, at the very bottom of that page, though I don't understand why.

Also, is there anyway I can have a field that I pass into the serializer be like a property so I don't have do to getters and setters? I tried before but it says I couldn't pass in properties with ref.

Thanks for the help!

#

Also can I have a constructor with parameters in a class that implements INetworkSerialiable?

low spruce
#

There isn't much info on using classes with INetworkSerializable. The docs don't say you can't do it, but every example they do provide uses structs. I would stick to this example with structs unless you absolutely need to use classes.

digital widget
# low spruce There isn't much info on using classes with INetworkSerializable. The docs don't...

I'd prefer classes, ig I'll have to test. Any idea regarding the other points? The reason I was asking about inheritence is because it says While you can have nested INetworkSerializable implementations (an INetworkSerializable implementation with INetworkSerializable implementations as properties) like demonstrated in the example above, you can't have derived children of an INetworkSerializable implementation. but they don't call a base method in the subclass and instead access all properties, including those in the parent class, inside the subclass. So I don't know if the way I've done it will actually make a difference

maiden wren
#

You can use classes with RPCs but not with network variables

digital widget
#

Nice. Does that include inheritance?

#

I'm guessing the answer to parametrized constructors is no

maiden wren
digital widget
#

The object is created somewhere in the serialiation process, and so I don't see how having a parameterised constructor would be compatible with that

#

Looking at the docs, you could probably do it with custom serialization

#

but seems like it would be better to use some kind of builder pattern

#

with INetworkSerializable

low spruce
digital widget
#

Do either of you know re the properties and passing that in to the serializer with ref?

low spruce
digital widget
#

manual getters and setters it is then

#

Thanks for the help both of you 🙂

digital widget
#

Also realised you can use properties and a backing field to get around the ref buisness