Skip to content

Having no default constructor in a INetworkSerializable class crashes the editor without any information #2445

@BrunoSilvaFreire

Description

@BrunoSilvaFreire

Description

Having no default constructor in a INetworkSerializable class crashes the editor without any information because when code is generated and includes a call to the method InitializeSerializer_ManagedINetworkSerializable, the new() type constraint is not satisfied.

image

Not trying to argue with the design principle, just with the bad ux of having to figure out why the editor is crashing. Having it not properly generate IL and have yell at you with a console log would be enough and a much better experience.

Reproduce Steps

  1. Have the following example class:
[Serializable]
public class PlayerProfile : INetworkSerializable {
    [SerializeField]
    private string _nickname;

    public PlayerProfile() { }

    public PlayerProfile(string nickname) {
        _nickname = nickname;
    }

    public string Nickname => _nickname;

    public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter {
        serializer.SerializeValue(ref _nickname);
    }
}
  1. Add it as a NetworkVariable like so:
public class Player : NetworkBehaviour {
    private NetworkVariable<PlayerProfile> _profile;
}
  1. Trigger a recompile and observe the editor crash

Actual Outcome

The editor crashes without any details other than the crash log to identify what happened.

Expected Outcome

CodeGen fails gracefully with some way of letting you know that you're not allowed to not have a no-args ctor

Environment

  • OS: Windows 11
  • Unity Version: 2022.2.3f1
  • Netcode Version: 1.2.0
  • Netcode Commit: 789f093

Additional Context

Editor.log

Metadata

Metadata

Assignees

Labels

priority:highThis issue has high priority and we are focusing to resolve itstat:importedStatus - Issue is tracked internally at Unitytype:bugBug Report

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions