currently I am converting my scripts from old Unity Networking to UNET. I have an array on server side where all player information is stored. I used to identify the player (when he was sending an RPC) with Networkmessageinfo.sender, so I can find the stored variables for that player within the array. But currently I can`t figure out how to identify the sender when performing a [Command] in UNET. What would be the way to identify the sender within UNET now? Any ideas?
You can have a class Messenger attached to the player controlled gameobject through which you would send commands. This already lets you identify who sent the command.
Then if you need to find the connection of the person who sent it, you can have a dictionary of players’ gameObjects and their connections. A simple comparison in the Command code easily identifies the connection from which the command was sent.
Or instead of keeping a dictionary, you can run this code to find the owner of the gameobject though which the command was sent:
public static NetworkConnection FindConnectionForPlayer(GameObject go)
{
foreach (NetworkConnection connection in NetworkServer.connections)
{
if (connection != null && connection.playerControllers.Count > 0 && connection.playerControllers[0].gameObject == go)
return connection;
}
return null;
}
So there is no way when a Client starts a Command on the server to “easily” see the sender? I dont like the idea to have this information bound to gameobjects … there must be a way to just see the sender information (for example IP) for a command call, not only for the object that is doing something.
Commands are run on player objects. The Client that sent a command is implicit in the player game object. There is a “connectionToClient” member variable on the NetworkBehaviour base class, which is the NetworkConnection to that client.
I just wanted to test this by connecting to a game and printing the connectionToClient id to the console. For connecting I am using a Networkmanager object that has no networkidentity. As far as I know it does not need one, correct? As soon as I add one it disables itself (the whole object) when pressing the play button in the editor.
public class TestNetworkManager : NetworkManager {
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
}
void OnPlayerConnected()
{
Debug.Log("Player connected");
}
}
No matter how many clients are connecting, the console never says anything about a player connecting. Mhhh any ideas?
With the help of “NetworkConnection conn” I am able to get the id of each client. Is there a way to use “NetworkConnection conn” within a [Command] as well? Would be an easy solution to my problem / question.