Simple Unity Server for Storing Data?

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)

If (button1clicked == true) {
     //s Add one to the number of people who have clicked Button1
     ServerVariable++;
}

THANKS!

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.

For clarification, uNET can be gotten by going to Window > Services and enabling Multiplayer, correct?

Also, where is the uNet manual?

Thanks!

Do you know any tutorials for Syncvars?

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.

using UnityEngine;
using UnityEngine.Networking;

namespace Testing {
    class ExampleNet : NetworkBehaviour {
        [SyncVar] // This will cause exampleInt to be updated on clients
        public int exampleInt;

        // This will call the mentioned function with the new value as an argument on clients
        [SyncVar(hook = "SyncUpdateVariable")]
        public float exampleFloat;

        // This can only be triggered by the "own"ing player. i.e. Local Player Authority
        [Command]
        public void CmdDoServerStuff(int exampleArg) {
            // Do something on the server
            Debug.Log("Client says Hi");

            exampleInt = exampleArg; // This will set this value on all clients
            exampleFloat = 42f; // This will call SyncUpdateVariable on all clients
        }

        // This can only be triggered by the server
        [ClientRpc]
        public void RpcDoClientStuff(string exampleArg) {
            //Do something on each client
            Debug.Log("Server says " + exampleArg);
        }

        // This function gets called on the clients when exampleFloat gets modified on the server
        public void SyncUpdateVariable(float newValue) {
            Debug.Log("Old Value:" + exampleFloat + " New Value:" + newValue);
            // ExampleFloat is still set to its previous value
            // We can use this function to perform logic when updating the variable
            // e.g. We could set it to a different value or keep the current value
            exampleFloat = newValue;
        }

        int myValue;
        // Called on local player once network starts
        public override void OnStartLocalPlayer() {
            // Calls function on server which calls function on all clients and updates myValue
            CmdExampleFunc(42);
        }

        // A [Command] function must start with Cmd
        [Command]
        public void CmdExampleFunc(int newValue) {
            RpcExampleFunc(newValue);
        }
        // A [ClientRpc] function must start with Rpc
        [ClientRpc]
        public void RpcExampleFunc(int newValue) {
            myValue = newValue;
        }
    }
}

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!

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.

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?

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.

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

@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:

Yep, will do. Do the same for me :wink:

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.