Search Unity

Change variable on all clients from server

Discussion in 'Multiplayer' started by kigm, Jun 27, 2016.

  1. kigm

    kigm

    Joined:
    May 13, 2016
    Posts:
    29
    Hi! I need have to a variable on server that clients consults and in function of its value do something.

    I had been trying with a [SyncVar] but this is not what I want. Because I want that if one client connects to server, change this variable on the server and the rest of clients that connect later check this variable and work in function on that.
    And now if I use syncvar each client has his own variable, synced with the server yes, but it is his own variable, isn't shared between clients.

    What can I do?
     
  2. kigm

    kigm

    Joined:
    May 13, 2016
    Posts:
    29
  3. kigm

    kigm

    Joined:
    May 13, 2016
    Posts:
    29
    Really I think even more yesterday that doc is not so good... Because I read that this thing (Syncvar) updates a value from a server to all clients, and this is what I want. But doing tests I see that each client has his own variable, synced with the server yes, but if we have client A and B and a variable health of type Syncvar client A can have a value of 60 in server and client, but client B has a different value. And this is reasonable.

    Really.. doesn't exist something like what I am searching? just a simple variable where you can consult from a client and share with all clients this variable with the same value... In Java and RMI you can have on the server side variables and methods, and you can use them. But I find Unity so limited than always I am struggling to make progress in my game. Is true that I am new on this, but I had been coding before to know Unity.
     
  4. voltage

    voltage

    Joined:
    Nov 11, 2011
    Posts:
    515
  5. kigm

    kigm

    Joined:
    May 13, 2016
    Posts:
    29
    Thank you for your reply. But this is not work for me because I don't have "players" when someone connects to server, my player's character is empty. I just have a host (first client that connects to server) who can move a 3D model (loaded dynamically after first client connection in a new prefab with authority for this client) and other clients that connect later can only see how this first "player" move the model. But the rest of client don't have a character just like that.

    The post you sent me change the variable myColor from server to the script saved on client (which is attached to player) and is a random value, but I need check what is the real value (a client could have changed this variable) and copy the value to local variable on client.

    I don't know if you understand, if so I'll explain better
     
  6. Severos

    Severos

    Joined:
    Oct 2, 2015
    Posts:
    181
    Your problem is more of c# problem than Unity.
    The reason each client is getting it's own variable is because you're defining it somehow like this:
    Code (CSharp):
    1. private int val;
    that means, this is instance variable, each instance will have it's own variable.
    If you want all instances of this class to share same variable then you use the static modifier like this:
    Code (CSharp):
    1. private static int val;
    All objects of this class type now will have same value, when one of them changes it, then all instances will see the new value.
     
    BlackHawkG likes this.
  7. kigm

    kigm

    Joined:
    May 13, 2016
    Posts:
    29
    But I am using Syncvar and you can not use static type.. And in the case if you could the variable will be static in the context of the instance of unity. If one player is connected in other PC/mobile the scope of static is reduced to the instance of the "game" in this player, not other players connected trough network
     
  8. Severos

    Severos

    Joined:
    Oct 2, 2015
    Posts:
    181
    Then just use a simple client RPC to update the variable on all clients. when value changes an RPC call will change it on all connected clients, new clients will send a request (command/netmessage or whatever you want) to get the variable value on loading.
     
  9. kigm

    kigm

    Joined:
    May 13, 2016
    Posts:
    29
    This is what I said above. You can call a Rpc method and change the value on all clients, but ONLY THE VALUE THAT YOU OWN, only in your instance of your player on the other players.
    For example, if the server have 3 connected clients and one of them change the value of the variable, this value is changed on the instance of THIS client on other clients, but other clients still having the same value on HIS instance of the player object.

    Context of A:
    A.variable = 1;
    B.variable = 2;

    Context of B:
    A.variable = 1;
    B.variable = 2;

    "A" calls Rpc method changing variable from 1 to 10. Then now we have this:

    Context of A:
    A.variable = 10;
    B.variable = 2;

    Context of B:
    A.variable = 10;
    B.variable = 2;

    Do you see that? this is what I said before. And I need all clients have the same value on the same variable
     
  10. kigm

    kigm

    Joined:
    May 13, 2016
    Posts:
    29
    I think that getting the gameobject of the first player and consulting the variable I want it will be ok... Im on it
     
  11. Severos

    Severos

    Joined:
    Oct 2, 2015
    Posts:
    181
    I was talking about using RPC to change the static variable.
     
  12. kigm

    kigm

    Joined:
    May 13, 2016
    Posts:
    29
    Sure, and I just could use in a no multiplayer game. Thank you anyway
     
  13. kigm

    kigm

    Joined:
    May 13, 2016
    Posts:
    29
    I am done this on the following way:
    - Considering in our instance of unity are the players connected to server, I saved them in a list.
    - Accessed the first player (is the first that connected to server) and get the data I needed.
     
    pjkokane21 likes this.
  14. fedd93

    fedd93

    Joined:
    Oct 15, 2018
    Posts:
    1
    Hey,Please share the code :)
     
    Abdulrahman_alhmdani likes this.
  15. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Kigm hasn't logged into the forums in over 2 years, so don't get your hopes up.
     
    Abdulrahman_alhmdani likes this.
  16. ebk611

    ebk611

    Joined:
    Jul 30, 2021
    Posts:
    1
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Networking;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. public class Player : NetworkBehaviour {
    6.  
    7.     //If you need to work with indices, you can use a List like that. (ie ActivePlayers[0].DoBackflip())
    8.     // Also you can change variables of other player objects with cmd function with (requiresAuthroity=false)
    9.  
    10.     public static List<Player> ActivePlayers = new List<Player>();
    11.     protected virtual void Awake()
    12.     {
    13.         //Add me when I awake - this is called on both the clients and the host, so everyone will know me
    14.         ActivePlayers.Add(this);
    15.     }
    16.     protected virtual void OnDestroy()
    17.     {
    18.         //Cleanup - this is important since static members will persist until application is quit
    19.         ActivePlayers.Remove(this);
    20.     }
    21. }