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…
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.
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.
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.
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.
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”…
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 no one explain that
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”
yes ! but unet is free, i think unet is good for starter. Remember make a multi player game is not easy . 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