Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using Generics with ServerRpc will generate error at line (0,0) #1860

Closed
ddonk opened this issue Apr 4, 2022 · 0 comments · Fixed by #2720
Closed

Using Generics with ServerRpc will generate error at line (0,0) #1860

ddonk opened this issue Apr 4, 2022 · 0 comments · Fixed by #2720
Labels
stat:awaiting triage Status - Awaiting triage from the Netcode team. type:support Questions or other support

Comments

@ddonk
Copy link

ddonk commented Apr 4, 2022

Descripition

I was trying to make a handler that can sync variables over network, my only problem is that when I use a method with the ServerRpc attribute it gives my a compile error.

Reproduce Steps

  1. Create a script with this as the code:
public class NetworkHandler<T> : NetworkBehaviour where T : unmanaged
{
    private NetworkVariable<T> _networkVariable = new NetworkVariable<T>();
    public void InitGeneric(T value)
    {
        Debug.Log("Testing Generic Method");
    }

    public void SetValue(T value)
    {
        if(!IsOwner) return;

        if (NetworkManager.Singleton.IsServer)
        {
            _networkVariable.Value = value;
        }
        else
        {
            SubmitRequestServerRpc(value);
        }
    }
    
    [ServerRpc]
    void SubmitRequestServerRpc(T value)
    {
        //TODO implement Rpc logic
    }
}
  1. Save Script so Unity will recompile
  2. See Error

Actual Outcome

This is the error i get:
(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) in D:\Unity Projects\FPS Multiplayer\Library\PackageCache\[email protected]\Editor\CodeGen\NetworkBehaviourILPP.cs:line 621|| at Unity.Netcode.Editor.CodeGen.NetworkBehaviourILPP.InjectWriteAndCallBlocks(MethodDefinition methodDefinition, CustomAttribute rpcAttribute, UInt32 rpcMethodId) in D:\Unity Projects\FPS Multiplayer\Library\PackageCache\[email protected]\Editor\CodeGen\NetworkBehaviourILPP.cs:line 1014|| at Unity.Netcode.Editor.CodeGen.NetworkBehaviourILPP.ProcessNetworkBehaviour(TypeDefinition typeDefinition, String[] assemblyDefines) in D:\Unity Projects\FPS Multiplayer\Library\PackageCache\[email protected]\Editor\CodeGen\NetworkBehaviourILPP.cs:line 409|| at Unity.Netcode.Editor.CodeGen.NetworkBehaviourILPP.<>c__DisplayClass5_0.<Process>b__1(TypeDefinition b) in D:\Unity Projects\FPS Multiplayer\Library\PackageCache\[email protected]\Editor\CodeGen\NetworkBehaviourILPP.cs:line 62|| at System.Collections.Generic.List1.ForEach(Action1 action)|| at Unity.Netcode.Editor.CodeGen.NetworkBehaviourILPP.Process(ICompiledAssembly compiledAssembly) in D:\Unity Projects\FPS Multiplayer\Library\PackageCache\[email protected]\Editor\CodeGen\NetworkBehaviourILPP.cs:line 59 at Unity.Netcode.Editor.CodeGen.NetworkBehaviourILPP.GetWriteMethodForParameter(TypeReference paramType, MethodReference& methodRef) in D:\Unity Projects\FPS Multiplayer\Library\PackageCache\[email protected]\Editor\CodeGen\NetworkBehaviourILPP.cs:line 621|| at Unity.Netcode.Editor.CodeGen.NetworkBehaviourILPP.InjectWriteAndCallBlocks(MethodDefinition methodDefinition, CustomAttribute rpcAttribute, UInt32 rpcMethodId) in D:\Unity Projects\FPS Multiplayer\Library\PackageCache\[email protected]\Editor\CodeGen\NetworkBehaviourILPP.cs:line 1014|| at Unity.Netcode.Editor.CodeGen.NetworkBehaviourILPP.ProcessNetworkBehaviour(TypeDefinition typeDefinition, String[] assemblyDefines) in D:\Unity Projects\FPS Multiplayer\Library\PackageCache\[email protected]\Editor\CodeGen\NetworkBehaviourILPP.cs:line 409|| at Unity.Netcode.Editor.CodeGen.NetworkBehaviourILPP.<>c__DisplayClass5_0.<Process>b__1(TypeDefinition b) in D:\Unity Projects\FPS Multiplayer\Library\PackageCache\[email protected]\Editor\CodeGen\NetworkBehaviourILPP.cs:line 62|| at System.Collections.Generic.List1.ForEach(Action1 action)|| at Unity.Netcode.Editor.CodeGen.NetworkBehaviourILPP.Process(ICompiledAssembly compiledAssembly) in D:\Unity Projects\FPS Multiplayer\Library\PackageCache\[email protected]\Editor\CodeGen\NetworkBehaviourILPP.cs:line 59

Expected Outcome

I expected unity wouldnt have given my any compile errors because i see no issues with what could go wrong at compile level. I was going to use this code to deploy on other scripts to be able to sync variables over network.

Environment

OS: Windows 10 Education
Unity Version: 2021.2.13f1
Netcode Version: 1.0.0-pre.6 - March 03, 2022

Additional Context

I suspect that a Rpc with generics is giving the error but my implentation of the code above would be like this , which would give it a hard type at compile time because of inheritance so I see no issue of my implemetation:

public class NetworkTest : NetworkHandler<int>
{
    private Random _random;
    private void Start()
    {
        _random = new Random();
        Debug.Log("Testing Less Generic Method");
        InitGeneric(10);
    }

    private void Update()
    {
        //Post a random value and sync over network
        if (Input.GetKeyDown(KeyCode.Space))
        {
            SetValue(_random.Next(1, 20));
        }
    }
}
@ddonk ddonk added stat:awaiting triage Status - Awaiting triage from the Netcode team. type:support Questions or other support labels Apr 4, 2022
@ddonk ddonk changed the title Using Generics with ServerRpc Using Generics with ServerRpc will generate error at line (0,0) Apr 5, 2022
@ddonk ddonk closed this as completed Apr 5, 2022
@ddonk ddonk reopened this Apr 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stat:awaiting triage Status - Awaiting triage from the Netcode team. type:support Questions or other support
Projects
None yet
1 participant