Search Unity

Calling a function @ server from client ?

Discussion in 'Multiplayer' started by Muckel, Sep 25, 2015.

  1. Muckel

    Muckel

    Joined:
    Mar 26, 2009
    Posts:
    471
    Hello,
    i just try to figure out how to call a function from a client on the server?
    to call a function on a client is not a problem with [ClientRpc]...
    but how to do the opposite ?
    call a function on the server from client?
    maybe it's simple but i can not find a answer on iNet...
    many thx
    M.
     
  2. Iron-Warrior

    Iron-Warrior

    Joined:
    Nov 3, 2009
    Posts:
    838
    You use a [Command]. Bear in mind only scripts with local player authority are allowed to call Commands.
     
  3. Muckel

    Muckel

    Joined:
    Mar 26, 2009
    Posts:
    471
    Hi,
    thx for quick reply but that doesn't work...
    that was what i tried the day...
    here is the function:
    Code (CSharp):
    1.  
    2. //call this on the client
    3. public void setBtn(){
    4.     CmdCall();
    5.     }
    6. //nothing happen @ the server...
    7.     [Command]
    8.     private void CmdCall() {
    9.     print("CmdCall !!!");
    10. }
    11.  
    thx
    M.
     
  4. Huntibia

    Huntibia

    Joined:
    Feb 12, 2014
    Posts:
    14
    Did you attach this script to a gameobject with client authority (usually the player prefab where you checked "Local Player Authority" in the NetworkIdentity)?

    I wrote an example of spawning with client authority for another guy that might help you as well. See the attached zip.
     

    Attached Files:

  5. pakfront

    pakfront

    Joined:
    Oct 6, 2010
    Posts:
    551
    To keep things simple, if you can, have all the [Command] methods on a NetworkBehaviour attached to the same GameObject as the player. That way you have client authority by default for that NetworkBehaviour.

    This is also recommend in the docs:
    Commands are sent from player objects on the client to player objects on the server. For security, Commands can only be sent from YOUR player object.
    http://docs.unity3d.com/Manual/UNetActions.html
     
    Last edited: Sep 26, 2015
  6. Muckel

    Muckel

    Joined:
    Mar 26, 2009
    Posts:
    471
    Hello @ all,
    many thx for the quick reply's!
    Respect.
    Well maybe i was not clear enough...
    I have a Button on Server and Client... now if i press that btn on the Server it change the color to red also on client.
    So [ClientRpc] works!
    Now if i push that Button on the Client it should also change the state of the btn and color on the Server!
    That doesn't work and i can't get it to run...
    I gave the client isAuthority but nothing happened... The command doesn't get executed on the Server...
    So basically if i push the btn on server it reacts also on client but if i push the btn on client with the [Command] nothing happen on Server... hmmm is this a bug?
    Like i can switch the light on from server to client but i can not switch the light from client to server...
    So if i'm a client i cannot switch out the light on the Server?
    many many thx
    M.

    Hm maybe it is a issue still...
    http://issuetracker.unity3d.com/iss...-for-local-client-for-networked-scene-objects
     
    Last edited: Sep 26, 2015
    petrbnv likes this.
  7. pakfront

    pakfront

    Joined:
    Oct 6, 2010
    Posts:
    551
    I do have something similar working, at least in the case that the NetworkBehaviour is part of the "PlayerPrefab."
     
  8. haouari

    haouari

    Joined:
    Jun 1, 2016
    Posts:
    3
    i think i know how to fix it
    give that btn a script witch has [Varsync(hook="RpcOnvaluechange")] public bool isred or change it to something more open if you want . but you can't sync gameobjects , anyways.. make it monobehaviour and attach if statment in the update function that says if its true than change color . now.. go to your player script and everytime you click on that btn tell it to give athority to the btn and change the color by command then take the athority away ... after that
    "RPcOnvaluechange" function with [ClientRpc] before it .. says that RpcOnvaluechange(newvalue){isred=newvalue;}
    this function gonna exucate on every client changing the value when it changes in the server

    sorry for my bad writting skills .. ik its a little bit late . but i hope it helps somone else