FishNet icon indicating copy to clipboard operation
FishNet copied to clipboard

Defining rpc / prediction methods in generic class will throw exception

Open Alexejhero opened this issue 2 years ago • 0 comments

If you define rpc methods or prediction methods within a class which is generic, an exception will be thrown.

public class Parent<T> : NetworkBehaviour 
{
    [ServerRpc(RequireOwnership = false)]
    private void RPC(NetworkConnection conn = null)
    {
        Debug.Log("Received RPC");
        GiveOwnership(conn);
    }
    
    [Replicate]
    private void CSPMove(MoveStruct moveData, bool replaying, bool asServer)
    {
        Debug.Log("Received Replicate");
    }

    [Reconcile]
    private void CSPReconcile(ReconcileStruct recData, bool isServer)
    {
    }

    [Button]
    private void TestRPC() => RPC();
    
    [Button]
    private void TestReplicate() => CSPMove(new MoveStruct() { x = 1 }, false, false);
}

public class Child : Parent<int> // Add this component to the GameObject
{
}

public struct MoveStruct
{
}

public struct ReconcileStruct
{
}

This causes the following exception when the methods are invoked and in Child.NetworkInitialize___Early

BadImageFormatException: Method with open type while not compiling gshared

If you move these methods out of the generic class and into child class or another parent class which is not generic, the same error is thrown in Child.Awake, but the rpc/prediction methods work just fine.

Alexejhero avatar Aug 16 '22 07:08 Alexejhero