How to send an integer value from client to the server?

i’ve been doing my research all day and nothing seems to work properly… is there a short efficient way to just send an integer value to the server? i mean, like in the normal way of work within methods, like:

public void SendInteger(int value){
myServerScript.GetInteger(value);
}

something short and simple as that? because i find absolutely ridiculous to have a whole script full of classes and methods for send just an integer number…

so i am trying to send it directly to my custom NetworkManager (to spawn a custom playerprefab). But i need just an integer to choose between all my playerprefabs…

You can use a command. I don’t have Unity on this computer, but I think this is correct syntax.

private void someMethodCalledOnClient()
{
    CmdGiveMeInt(47);
}

[Command]
void CmdGiveMeInt(int someValue)
{
    Debug.Log("I got an int! " + someValue);
}

So the above code would produce a log entry of “I got an int! 47” in the server’s log whenever a client called the method someMethodCalledOnClient().

Commands are special UNET methods that you call on the client but are only actually run on the server. Don’t forget to start the name of your command method with Cmd.

https://docs.unity3d.com/Manual/UNetActions.html

i tried that, NetworkManager does not accept [Command] nor [SyncVar] and a bunch of other stuff… i did something tricky, a workaround using another object, with another script which comes from Networkbehaviour, “varManager.cs” and there i putted my “SyncListInt playerPrefabToLoad” but now when i try to add a new value to the list i get a warning that i can not access that because i dont have authority… what a mess…

Put what I wrote on another script, and inside that command instead of that debug line you push your value over to NetworkManager. SyncVar’s SyncLists, etc, are ways to sync values from the server to the client, but don’t go from the client to the server.

but if i cant go client → server, i dont get how to send that int value TO the server (if im a client), anyway i will try what u told me :slight_smile:

@Joe-Censored nope, it does not work :frowning:

3024978--226106--upload_2017-4-7_21-25-39.png

it does not sync my “syncListInt” and unity shouts that warning…? what can i do :c ? this is my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;

public class VarManager : NetworkBehaviour {
   
  
    public SyncListInt ConnList = new SyncListInt();
    public SyncListInt playerType = new SyncListInt();

    public static VarManager instance;

    // Use this for initialization
    void Awake () {
        instance = this;
    }
    public void SelectElement(int i){
        CmdCallElement(i);
    }
    [Command]
    public void CmdCallElement(int i){

        if (LevelManager.instance.isConnected)
        {
            if (ConnList.Count > 10)
            {
                ConnList.Clear();
                playerType.Clear();
            }

            ConnList.Add(LevelManager.instance.connId);
            playerType.Add(i);

            //-----------------------------------------

            ClientScene.AddPlayer(0);
        }
       
    }
}

PD: what is being called when you press any UI button (to select a player) is “SelectElement”

The client sending the command either needs client authority assigned, or the script needs to be attached to that client’s player object. It is explained in more detail in the link I sent earlier. Sorry I guess I’ve only been using commands on player objects as I didn’t think to mention it.

3025006--226107--upload_2017-4-7_21-55-9.png

already with local authority checked, of course in the same object that is the networkidentity :s

Yeah but are you sure that specific client has the authority? This is a feature to prevent other clients from sending commands for gameobjects they shouldn’t be controlling, as commands are often used to send key or button presses to control a player object. So only the specific client with authority over that gameobject can issue commands.

Take a look at the bottom answer here for a better explanation than I’m apparently able to give.

http://stackoverflow.com/questions/36107855/unity3d-trying-to-send-command-for-object-without-authority

well i think i cant understand… iam too used to the way simple programming works, like the example i putted at the begining, so this is beyond what i know about networking, thats why i am asking how the heck can i do that, because it seems the only way to do it is across network messages, which is something i dont understand too… i cant find any sense in this… a whole script full of voids and classes to just send a tiny harmless basic “int”… :confused:

1 Like

You can Call Commands only from PlayerPrefab.

omg… so is there any way possible to do what i want to do with UNET ?

1 Like

you can send a message from a class extend from Network Behavior
ex:

can you point in a more specific direction? because i just see a big script full of “onmessageThis” “onMessagethat”“myMessage”, “netMsg”, etc… and no clue what is the actual message and how i spawn my player object with the int passed through all those messages? because the spawning is done in the NetworkManager script, more specific, there → “OnServerAddPlayer”, so i have no clue even if i get the message thing working on the other NetworkBehaviour script how to use that in the NetworkManager :frowning: no one explain that :frowning:

  1. learn unet basic tutorial
  2. download tank project Unity Asset Store - The Best Assets for Game Making. Open this source code and see class extend from NetworkManager
    3.read document https://docs.unity3d.com/ScriptReference/Networking.NetworkManager.html
  3. download source code unet hight level api and read to know what class NetworkManger do https://bitbucket.org/Unity-Technologies/networking/src/bfdfc58bb61bfdd7d49ceb8c69583482febadc84/Runtime?at=5.6

wao! it’s not easy -_-

thanks! i will try to look into it :slight_smile:
though i believe would be more easy to use photon or forge, don’t you agree?

i looked into the tanks example… is far more complicated than the docs(a loot more code to understand) and doesn’t even work at all… just errors and “fails” :confused:

1 Like

yes ! but unet is free, i think unet is good for starter. Remember make a multi player game is not easy :expressionless: . Pass it and open your mind.
Exactly you down load “Tanks!!! Reference Project” from here ? And run it on Unity 5.6 ?
https://www.assetstore.unity3d.com/en/#!/content/80165

yep, i did just that, and it seems is not working at all, cant create a game in any way or mode :confused: