Search Unity

[Command] on networked scene objects

Discussion in 'Multiplayer' started by JoRouss, Jul 27, 2015.

  1. JoRouss

    JoRouss

    Joined:
    Apr 1, 2014
    Posts:
    53
    I was trying to use [Command] on my Chat (Scene object that has a NetworkIdentity) but unfortunatly, I can't call those [Command] from clients since only the HOST can.
    I tought that checking "Local Player Authority" would allow me to send [Command] from any client, but it's not the case...

    In my example, I have a Chat on which I want to call "Cmd_AddChatMessage", that would then send the message to all clients with "Rpc_AddChatMessage"

    Code (CSharp):
    1.  
    2.     [Command]
    3.     void Cmd_AddChatMessage(string text)
    4.     {
    5.         Rpc_AddChatMessage(text);
    6.     }
    7.  
    8.     [ClientRpc]
    9.     public void Rpc_AddChatMessage(string text)
    10.     {
    11.         Debug.Log("I received a message");
    12.     }
    I saw on the forum someone saying it was working in the current beta...

    Right now I decided to call a command on my PlayerInstance, that is calling the Rpc on the Chat object... It's dirty but I want to find the good way to do this before changing it.

    Should I wait for the "Local Player Authority" to work on Scene Objects and keep my dirty code until then? Should I use messages? Or is there something I missed?

    Thank you very much!
     
    Last edited: Jul 27, 2015
  2. Piflik

    Piflik

    Joined:
    Sep 11, 2011
    Posts:
    292
    Commands can only be sent from GameObjects with Local Authority. Don't know if this will be changed in future releases, but currently you need a player object to send Commands.
     
  3. JoRouss

    JoRouss

    Joined:
    Apr 1, 2014
    Posts:
    53
    Yes, I know that, the problem is that it seems impossible to give local authority to all the clients when using scene objects with NetworkIdentity on them. It looks like only the first one to spawn it (the host) can call commands from it.
     
  4. Piflik

    Piflik

    Joined:
    Sep 11, 2011
    Posts:
    292
    I struggled with interacting with Scene Objects a couple of weks ago. The way I solved the problem was using a Command on my Player Object (via an 'Interact' script and a corresponding 'IUseable' interface) that tells the server that I want to use the object. The object in question is identified via its NetworkIdentity. (The reference to the using player is not always neccessary, but in some cases really useful)

    To be clear: You don't call commands on the scene object, you call a command on your player that tells the server to use the object. Only the Host can directly call a command on a scene object, because it belongs to the Server and the Host can communicate with the Server directly via function cllas and messages. Remote Clients can't.

    Code (CSharp):
    1.  
    2. [Command]
    3. void CmdUse(NetworkInstanceId playerID, NetworkInstanceId objectID) {
    4.     IUseable useable = NetworkServer.FindLocalObject (objectID).GetComponent<IUseable> ();
    5.     if (useable != null) {
    6.         useable.use (playerID);
    7.     }
    8. }
     
  5. JoRouss

    JoRouss

    Joined:
    Apr 1, 2014
    Posts:
    53
    Yes that's what I did too, but I find it a bit dirty. It would be so simple if the "Local Player Authority" was working on scene objects... So we can call Commands from any client.
     
  6. Piflik

    Piflik

    Joined:
    Sep 11, 2011
    Posts:
    292
    Well, Local Player Authority tells the Server, which Player this Object belongs to. If it was possible for multiple players to take ownership over an object, this could lead to contradictory information sent to the server.
    In the current implementation of UNet, Local Player Authority objects are controlled by the respective player, this information is sent to the client, which in turn relays it to the remaining clients. Scene Objects are owned by the server, so that it has the last say on what is to happen to them. (This is a mixture between a fully authorative server architecture, where the server owns every object, and one, where the server only acts as a relay to pass information between the clients)
     
  7. JoRouss

    JoRouss

    Joined:
    Apr 1, 2014
    Posts:
    53
    I think its better to make this possible, as we are juste using a dirty workaround to achieve our objective.