How to send command from a non player object

So i have a class called PlayersManager she is suppose to assign a material to each player on the game.

Something like:

  1. Client choose a character from a GUI
  2. The GUIManager will request to the PlayersManger to execute this processs.
  3. The PlayersManager will call a CmdSetMaterial();
  4. The server will recieve this request and call RpcSetMaterial(); setting it on all clientes.

On PlayersManager i will have i list with all characters from the players connected.

void RpcSetMaterial(int p_id)
{
_allCharacters[p_id].meshrenderer.material = Color.Blue;
}

I’ve uploaded a img of a “UML” if it helps.

The problem is that i can’t send Cmd from a non player object (PlayersManager).

I think i’ve found a better way to explain: Basic what i want is that when a client clicks a button the action from this button to be executed on this client and all other clientes.

How about sending NetworkMessage instead ???

I’ve thought about it, but my attempts to implement it until now have been catastrophic XD, i was hoping “perhaps there’s a easier way to do this” çç

Thanks for the reply.

Mind if you posted your netMsg code? might be able to give you a hand with it, took me a while to understand it but I could implement a login system with it, which was a trip to hell back and forth when trying to implement it with commands.

I Was trying to implemente the tutorial one from the documentation, but i can’t figure out how to get my client to send as parameter to Init();

The ideia would be to have NetworkInvoke() that would call the desired Rpc.

The SendReadyToBeginMessage() would be called when the player clicked the Select character button.

const short MyBeginMsg = 1002;

    NetworkClient m_client;

    public void SendReadyToBeginMessage()
    {
        var msg = new StringMessage("SetMaterial");
        m_client.Send(MyBeginMsg, msg);
    }

    public void Init(NetworkClient client)
    {
        m_client = client;
        NetworkServer.RegisterHandler(MyBeginMsg, NetworkInvoke);
    }

    public void NetworkInvoke(NetworkMessage netMsg)
    {
        var beginMessage = netMsg.ReadMessage<StringMessage>();

        if (beginMessage.value == "SetMaterial")
        {
            RpcSetMaterial();
        }
    }

    [ClientRpc]
    public void RpcSetMaterial()
    {
        //Set Material.
    }

Not sure that I got your point, but why not do something like m_client = new client() and initialize it there instead of sending it as a parameter from somewhere else??
Also you don’t need to register the handler on client, you only register it on the server, you can also use networkMessage instead of the RPC to set the material, in the server code you’d do something like NetworkServer.SendToAllConnections(netMsg); and don’t forget to register the handler on client.connection.RegisterHandler();

I managed to make it work, thanks!!! :slight_smile: