Search Unity

Proper way to update a non-local player object/Possible bug...

Discussion in 'Multiplayer' started by Deibu, Jun 30, 2015.

  1. Deibu

    Deibu

    Joined:
    Feb 24, 2013
    Posts:
    34
    Hi,

    I am having trouble updating a non-local player object.

    Inside my object, I have a function call that I want to be propagated from a client to the server and then to all the clients.

    In a script attached to my player object, I have this Command:
    Code (CSharp):
    1. [Command]
    2. public void CmdTest () {
    3.     FooBar foo = GameObject.FindObjectOfType<FooBar > ();
    4.     foo.doSomething ();
    5.     foo.RpcdoSomething  ();
    6. }
    This seems very inefficient, but it works. The weird part is when I'm inside my FooBar object, I can have a function that calls that command and it still works, but I can't have that Command inside my FooBar object. Is this expected behavior?

    How do I properly call functions on non-local player objects?

    Thanks!
     
  2. Deleted User

    Deleted User

    Guest

    I think Commands can only be in something with local authority, like the player object.

    In the command you have, you could change a SyncVar (that has a hook function) in the same script. In the hook, which runs on clients, you could make whatever change you want to happen to the player, based on the new value of the SyncVar.
     
  3. Deibu

    Deibu

    Joined:
    Feb 24, 2013
    Posts:
    34
    I'm not trying to change the player though. I'm trying to change an object that has server authority by an action that is caused by the player.
     
  4. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    if the object has a networkidentity, you can just pass it to the command. But make sure you have validation that this player owns the object.

    Code (CSharp):
    1. sing UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Networking;
    4.  
    5. public class args : NetworkBehaviour{
    6.  
    7.     [Command]
    8.     void CmdOther(GameObject other, int value)
    9.     {
    10.         // validation here
    11.         other.GetComponent<args>().RpcStuff(42);
    12.     }
    13.  
    14.     [ClientRpc]
    15.     void RpcStuff( int value)
    16.     {
    17.             Debug.Log("stuff  " + value + " isLocalPlayer:" + isLocalPlayer);
    18.     }
    19.  
    20.     void Update ()
    21.     {
    22.         if (Input.GetKeyDown(KeyCode.Space))
    23.             CmdOther(gameObject, 76545);
    24.     }
    25. }