- Home /
Sending local configuration data from Client to the Server
I'm setting up, what I first thought would be a pretty simple Server-Client network system, is now a pretty complex networking system that breaks a few rules.
What I'm creating is a multi-system "Tour" simulator, where we have multiple clients running multiple screens that all are children to a single Server-client (That I call "The Supervisor"), that will control all the clients. The Supervisor is the only client with a joystick attached, and moves itself (and the client children) around.
The Supervisor has only one camera - orthographic, looks top-down on the world. The clients support up to 8 different screens with 8 cameras in the scene. These cameras are set up with a configuration file that is read in on the local client host.
What I need help with is this: How do I send that local configuration data from the client to the server, in such a way that the server can store that data in a list for each individual client?
Answer by njfed · Dec 19, 2018 at 06:58 PM
Alright! I figured it out! Turns out it was just a careful game of TargetRpc and Command attributes! In the NetworkManager on the server, when OnServerAddPlayer is called, we call our TargetRpc function, which will call the function only on the newly connected client. In there, we call our Command function, passing in data from that particular client back to the server, where we can process it.
Here's a small example of the code for anyone to use in the future:
MyNetworkManager
public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId)
{
// Intentionally not calling base here - we want to control the spawning of prefabs
Debug.Log("OnServerAddPlayer");
MyNetworkController newPlayer = Instantiate<MyNetworkController >(this.myControllerPrefab);
DontDestroyOnLoad(newPlayer);
newPlayer.transform.localPosition = Vector3.zero;
NetworkServer.AddPlayerForConnection(conn, newPlayer.gameObject, playerControllerId);
// Call the TargetRpc fuinction on that new client
newPlayer.TargetSetupClient(conn);
}
MyNetworkController
[TargetRpc]
public void TargetSetupClient(NetworkConnection conn)
{
if (MyNetworkManager.IsServer) // Custom property for MyNetworkManager
{
...
}
else
{
...
foreach(var cam in camData) // camData is a SyncList of CameraData structs with details about... the cameras.
{
Cmd_SendCameraFrustumData(m_PlayerId, cam);
}
}
...
}
[Command]
public void Cmd_SendCameraFrustumData(int playerID, Messages.CameraData data)
{
// Set data from the client on the server! done!
...
}