0
\$\begingroup\$

Since you can not pass actions as parameters to commands and rpc calls, how would you sync actions ? My main goal is to find an easy and generic way for objects to easily do actions from their local player. Something like:

_localPlayer.RunFromLocalToAllPlayers(()=>
{
    //MyAction
});

Local Player having these methods:

private Action _callbackForAllPlayers;
public void RunFromLocalPlayerToAllPlayers(Action callback)
{
    _callbackForAllPlayers = callback;
    Debug.Log(_callbackForAllPlayers);
    CmdRunFromLocalPlayerToAllPlayers();
}

[Command]
        private void CmdRunFromLocalPlayerToAllPlayers()
        {
            Debug.Log($"Running on server");
            RpcRunFromLocalPlayer();
        }

[ClientRpc]
        private void RpcRunFromLocalPlayer()
        {
            Debug.Log($"Running on client");
            _callbackForAllPlayers?.Invoke();
            Debug.Log(_callbackForAllPlayers);
        }

and of course that local Action variable is being null on other clients since i can not sync it or pass it as parameter.

\$\endgroup\$
9
  • \$\begingroup\$ Why don't you just broadcast a RPC to all players? \$\endgroup\$ Jan 18 at 21:57
  • \$\begingroup\$ Of course i can go straight and use different Commands and different RPC calls for every action but i am looking for an easy and generic solution to this thats why. \$\endgroup\$ Jan 18 at 22:01
  • \$\begingroup\$ I don't think there is a better way to do that than broadcasting a RPC. That seems to be exactly the purpose of RPC's and where they should be used. Could you please expand more on why you want to avoid using RPCs in this case? \$\endgroup\$ Jan 18 at 22:05
  • \$\begingroup\$ Thanks for your input. \$\endgroup\$ Jan 18 at 22:05
  • \$\begingroup\$ I mean the question has my purpose and i already wrote my purpose with my first comment but here it is again, i am trying to find a generic solution for those kinda Command and RPC calls because otherwise whenever i would like to do something i have to write different command/rpc calls each time for every different action in the game. \$\endgroup\$ Jan 19 at 7:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.