Static RPC

From Docs :

Now , I try to use static RPC ;

public class TestRPC : SimulationBehaviour

{

Start()

{

Runner.AddSimulationBehaviour(this);

}

public static void Rpc_MyStaticRpc(NetworkRunner runner, int a) { }

}

When i run on editor , it give error :

"NullReferenceException: Object reference not set to an instance of an object

Fusion.NetworkRunner.AddSimulationBehaviour (Fusion.SimulationBehaviour behaviour, Fusion.NetworkObject obj)"

But in doc :

"can be used on a SimulationBehaviour, as the callback receiver is not bound to a particular NetworkObject. Note that any SimualtionBehaviour not associated with a NetworkObject will need to be attached to the NetworkRunner manually using Runner.AddSimulationBehaviour()"


As far as I understand, when using static RPC, there is no need to spawn any NetworkObject

Did I misunderstand something?

Best Answer

  • emotitron
    emotitron ✭✭✭
    Answer ✓

    I would just put your SimulationBehaviour with the static RPCs on your NetworkRunner prefab for simplicity here.

Answers

  • emotitron
    emotitron ✭✭✭

    I don't see in your code above the [Rpc] attribute being applied to your RPC method? Be sure to include that or Fusion will not be aware of the method.

  • Runner.AddSimulationBehaviour(this);

    It give error ""NullReferenceException: Object reference not set to an instance of an object

    Fusion.NetworkRunner.AddSimulationBehaviour (Fusion.SimulationBehaviour behaviour, Fusion.NetworkObject obj)"

    If use instance RPC i only add Runner.AddCallback(this), this : object i will spawn on Network.

    But if use Runner.AddSimulationBehaviour(); i dont know pass something for that ?

  • emotitron
    emotitron ✭✭✭

    Ah, I follow now. The issue I am seeing there is that you are trying to use Runner.AddSimulationBehaviour... but that is a chicken/egg issue. To use Runner, this SimulationBehaviour already needs to have been added to the Runner (the SimB is not associated with a Runner until you have done this).


    You might consider putting your class with the static RPC on the NetworkRunner prefab (that will find the SimulationBehaviour automatically), or you may want to bootstrap any SimB's from an INetworkRunnerCallbacks interface on your NetworkRunner prefab.

  • Thanks for the reply,

    But from your answer there are some things I don't understand:

    "You might consider putting your class with the static RPC on the NetworkRunner prefab (that will find the SimulationBehaviour automatically)"

    If I spawn a prefab with a class containing static RPC --> I will use instance RPC(Because in the document: don't need to spawn a network object containing this class)

    "you may want to bootstrap any SimB's from an INetworkRunnerCallbacks interface on your NetworkRunner prefab"

    I really don't understand. Could you give me an example?

  • emotitron
    emotitron ✭✭✭
    Answer ✓

    I would just put your SimulationBehaviour with the static RPCs on your NetworkRunner prefab for simplicity here.