Search Unity

Simple Unity Server for Storing Data?

Discussion in 'Multiplayer' started by ExbowFTW, Feb 7, 2016.

  1. ExbowFTW

    ExbowFTW

    Joined:
    May 2, 2015
    Posts:
    281
    I would like to try to make a simple server using something like UNET or PUN (Photon Unity Networking, which is on the Asset Store) so that if Player 1 clicks Button 1, then Player 2 will suddenly know that another person clicked the Button 1.

    Is there some sort of "Server Variable" so that if it is changed, the variable on all other player's games will also change? Here is an example script: (fake code)
    Code (csharp):
    1.  
    2. If (button1clicked == true) {
    3.      //s Add one to the number of people who have clicked Button1
    4.      ServerVariable++;
    5. }
    6.  
    THANKS!
     
  2. Ashkan_gc

    Ashkan_gc

    Joined:
    Aug 12, 2009
    Posts:
    1,124
    uNet has commands and syncvars to do it and you can read about them in the manual, client sends a command or message to server to change value and after server changes the syncvar's value , everyone will be updated.
     
  3. ExbowFTW

    ExbowFTW

    Joined:
    May 2, 2015
    Posts:
    281
    For clarification, uNET can be gotten by going to Window > Services and enabling Multiplayer, correct?

    Also, where is the uNet manual?

    Thanks!
     
  4. ExbowFTW

    ExbowFTW

    Joined:
    May 2, 2015
    Posts:
    281
    Do you know any tutorials for Syncvars?
     
  5. Oshroth

    Oshroth

    Joined:
    Apr 28, 2014
    Posts:
    99
    http://docs.unity3d.com/Manual/UNetStateSync.html has information on using SyncVars. There is a whole section on UNet in the manual like Askan mentions.

    Quick Summary: [Command]s can be called by clients but only on an object they "own" (e.g. Player Character) which will call that function on the server's copy of the object. [ClientRpc]s can only be called from the server and will call that function on all clients. [SyncVar] variables will update value changes on the server to each client and can call a hook function for custom logic.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Networking;
    3.  
    4. namespace Testing {
    5.     class ExampleNet : NetworkBehaviour {
    6.         [SyncVar] // This will cause exampleInt to be updated on clients
    7.         public int exampleInt;
    8.  
    9.         // This will call the mentioned function with the new value as an argument on clients
    10.         [SyncVar(hook = "SyncUpdateVariable")]
    11.         public float exampleFloat;
    12.  
    13.         // This can only be triggered by the "own"ing player. i.e. Local Player Authority
    14.         [Command]
    15.         public void CmdDoServerStuff(int exampleArg) {
    16.             // Do something on the server
    17.             Debug.Log("Client says Hi");
    18.  
    19.             exampleInt = exampleArg; // This will set this value on all clients
    20.             exampleFloat = 42f; // This will call SyncUpdateVariable on all clients
    21.         }
    22.  
    23.         // This can only be triggered by the server
    24.         [ClientRpc]
    25.         public void RpcDoClientStuff(string exampleArg) {
    26.             //Do something on each client
    27.             Debug.Log("Server says " + exampleArg);
    28.         }
    29.  
    30.         // This function gets called on the clients when exampleFloat gets modified on the server
    31.         public void SyncUpdateVariable(float newValue) {
    32.             Debug.Log("Old Value:" + exampleFloat + " New Value:" + newValue);
    33.             // ExampleFloat is still set to its previous value
    34.             // We can use this function to perform logic when updating the variable
    35.             // e.g. We could set it to a different value or keep the current value
    36.             exampleFloat = newValue;
    37.         }
    38.  
    39.         int myValue;
    40.         // Called on local player once network starts
    41.         public override void OnStartLocalPlayer() {
    42.             // Calls function on server which calls function on all clients and updates myValue
    43.             CmdExampleFunc(42);
    44.         }
    45.  
    46.         // A [Command] function must start with Cmd
    47.         [Command]
    48.         public void CmdExampleFunc(int newValue) {
    49.             RpcExampleFunc(newValue);
    50.         }
    51.         // A [ClientRpc] function must start with Rpc
    52.         [ClientRpc]
    53.         public void RpcExampleFunc(int newValue) {
    54.             myValue = newValue;
    55.         }
    56.     }
    57. }
     
  6. ExbowFTW

    ExbowFTW

    Joined:
    May 2, 2015
    Posts:
    281
    So all you literally have to do is Enable Multiplayer with Windows > Services, say "using UnityEngine.Networking;", and then say
    "[SyncVar]
    public int exampleInt;"

    and then say "exampleInt = 2" when you want it to be updated to a certain value?

    Thanks!
     
  7. Ashkan_gc

    Ashkan_gc

    Joined:
    Aug 12, 2009
    Posts:
    1,124
    And the Window/Services window is required if you want to use the Match Making capabilities and uNet's relay server to connect users to each other as client and server without having a public IP or dedicated server.

    Otherwise uNet is a part of the engine just like built-in old networking.
     
  8. ExbowFTW

    ExbowFTW

    Joined:
    May 2, 2015
    Posts:
    281
    But if all I want to do is be able to make a "Server PlayerPref variable", I guess you could call it, that any player can access, do I just have to do the things I listed before? And uNet is already installed in Unity?
     
  9. Ashkan_gc

    Ashkan_gc

    Joined:
    Aug 12, 2009
    Posts:
    1,124
    Well yes uNet is pre-installed.
    if you want to be able to change the variable from all clients then either each client should have one object which sends command to it's instance on the server and the sahred variable is on a separate object which is synced to everyone or use messages to send change commands since unity's [Command} RPC functions (both clientRpc and Command are remote procedure calls with different names), can only be sent to objects you own and one object can not be owned by everyone. Messages are not bound to objects.

    If you read the uNet manual for a day or two, you'll get most of it.
     
  10. ExbowFTW

    ExbowFTW

    Joined:
    May 2, 2015
    Posts:
    281
    Oh okay. Do you know of any tutorials that teach how to do instances and sync things using Command RPC for uNET? I've done this before but for Photon Unity Networking, but I forget how to do it all :3
     
  11. mbills2

    mbills2

    Joined:
    Jan 25, 2016
    Posts:
    25
    @ExbowFTW

    I'm trying to do basically the same thing. I've been trying for days to get a variable to update on the server. If you find out how will please post how to do it?

    I don't know if this will help but it's a good networking starter tutorial:
     
  12. ExbowFTW

    ExbowFTW

    Joined:
    May 2, 2015
    Posts:
    281
    Yep, will do. Do the same for me ;)

    It should be really simple... Just add a gameobject and make it a Network Identity so that the server can always send variable value information to the gameobject, and the gameobject can then update the game's variable with the value that the server gave it... I just don't know how you would do it.