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\$ Commented Jan 18, 2021 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\$ Commented Jan 18, 2021 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\$ Commented Jan 18, 2021 at 22:05
  • \$\begingroup\$ Thanks for your input. \$\endgroup\$ Commented Jan 18, 2021 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\$ Commented Jan 19, 2021 at 7:55

0

You must log in to answer this question.

Browse other questions tagged .