FirstGearGames / FishNet

FishNet: Unity Networking Evolved.
Other
1.38k stars 149 forks source link

Automatic serializer can't serialize simple class with a copy constructor #755

Closed Penca53 closed 3 months ago

Penca53 commented 3 months ago

Important

If General, Description, and Replication are not completed the issue will be closed immediately.

General Unity version: 2022.3 Fish-Networking version: 4.3.8 Discord link: https://discord.com/channels/424284635074134018/1034477094731784302/1273031045561847839

Description Automatic serializer generation is not working when there is a copy constructor in a class (or struct).

Replication The following snippet works fine, as the automatic serializer is correctly implemented.

using FishNet.Connection;
using FishNet.Object;
using FishNet.Serializing;
using UnityEngine;

public class SpawnObject : NetworkBehaviour
{
    public override void WritePayload(NetworkConnection connection, Writer writer)
    {
        base.WritePayload(connection, writer);
        writer.Write(new Data(1, 2, 3, 5));
    }

    public override void ReadPayload(NetworkConnection connection, Reader reader)
    {
        base.ReadPayload(connection, reader);
        var data = reader.Read<Data>();
        Debug.Log($"DATA: {data.A}, {data.B}, {data.C}, {data.D}, ");
    }
}

public class Data
{
    public float A;
    public float B;
    public float C;
    public float D;

    public Data()
    {
        A = 0;
        B = 0;
        C = 0;
        D = 0;
    }

    public Data(float a, float b, float c, float d)
    {
        A = a;
        B = b;
        C = c;
        D = d;
    }
}

But as soon as you add a copy constructor, the serializer won't be automatically implemented, returning the error "Write method not found for Data. Use a supported type or create a custom serializer."

public Data(SomeOtherData otherData)
{
    A = otherData.A;
    B = otherData.B;
    C = otherData.C;
    D = otherData.D;
}

Expected behavior The code generator should automatically implement the serializer.

FirstGearGames commented 3 months ago

Have you tried placing [IncludeSerialization] above your Data class?

maxkratt commented 3 months ago

That appears to make it work

Penca53 commented 3 months ago

The attribute fixes this issue. It only fails to create a serializer with nested structs / classes. Would it be possible to support that as well?

FirstGearGames commented 3 months ago

That would need to be a suggestion, though it will have a low priority for a bit. Marking as resolved.