Skip to content

Generic Network Behavior class #2690

@m0tholith

Description

@m0tholith

Description

I'm trying to make a generic class for syncing a variable through RPC calls (a generic class that inherits from NetworkBehaviour) and it seems the ILPP code generator freaks out a little bit.

Reproduce Steps

  1. Create a new script called RPCSync.cs.
  2. Paste the code below:
using System;
using Unity.Netcode;

public class RPCSync<T> : NetworkBehaviour where T : INetworkSerializable
{
    public T Data { get; private set; }
    public Action OnDataRecieved;

    public void SetData(T data)
    {
        Data = data;
        SyncDataServerRPC(data);
    }

    [ServerRpc(RequireOwnership = false)]
    private void SyncDataServerRPC(T data) => SyncDataClientRPC(data);
    private void SyncDataClientRPC(T data)
    {
        if (IsOwner)
            return;

        Data = data;
        OnDataRecieved?.Invoke();
    }
}
  1. Save and open Unity
  2. See the (only) error that appears in the console.

Actual Outcome

The ILPP code generator complains about a null reference.

Unity.Netcode.Editor.CodeGen.NetworkBehaviourILPP: (0,0): error  - System.NullReferenceException: Object reference not set to an instance of an object.|   at Unity.Netcode.Editor.CodeGen.NetworkBehaviourILPP.GetWriteMethodForParameter(TypeReference paramType, MethodReference& methodRef)|   at Unity.Netcode.Editor.CodeGen.NetworkBehaviourILPP.InjectWriteAndCallBlocks(MethodDefinition methodDefinition, CustomAttribute rpcAttribute, UInt32 rpcMethodId)|   at Unity.Netcode.Editor.CodeGen.NetworkBehaviourILPP.ProcessNetworkBehaviour(TypeDefinition typeDefinition, String[] assemblyDefines)|   at System.Collections.Generic.List`1.ForEach(Action`1 action)|   at Unity.Netcode.Editor.CodeGen.NetworkBehaviourILPP.Process(ICompiledAssembly compiledAssembly)   at Unity.Netcode.Editor.CodeGen.NetworkBehaviourILPP.GetWriteMethodForParameter(TypeReference paramType, MethodReference& methodRef)|   at Unity.Netcode.Editor.CodeGen.NetworkBehaviourILPP.InjectWriteAndCallBlocks(MethodDefinition methodDefinition, CustomAttribute rpcAttribute, UInt32 rpcMethodId)|   at Unity.Netcode.Editor.CodeGen.NetworkBehaviourILPP.ProcessNetworkBehaviour(TypeDefinition typeDefinition, String[] assemblyDefines)|   at System.Collections.Generic.List`1.ForEach(Action`1 action)|   at Unity.Netcode.Editor.CodeGen.NetworkBehaviourILPP.Process(ICompiledAssembly compiledAssembly)

Expected Outcome

No errors.

Environment

  • OS: Debian 12 (Linux 6.5)
  • Unity Version: 2022.3.6f1
  • Netcode Version: 1.5.2
  • Netcode Commit: ???

Additional Context

Below is a small sample Unity project that demonstrates the problem. You may need to open the project in safe mode (since the script above causes the error).
RPC Sync Problem.zip

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions