- Home /
How do I use uNET to send client data to server (SyncListBool goes the wrong way)?
OK, so I've looked for an answer to this, but they're all using the old networking system. I know that SyncListBool gives me my array of bools, and when I use them in the server, the client gets updated. What I need is the opposite. I need the client to own, keep track of, and send those values to the server.
I have a client that has 8 switches. The the client changes those 8 switches values on clicks. The server needs to know values of those switches. I suppose I could continue using the SyncListBool on the server, and have the client call a function that the server listens to in order to change the servers values. But I'd rather just have data from the server sent to the client, and data from the client (including the values in the SynList) sent to the Server.
public SyncListBool ButtonState = new SyncListBool();
That gives the client the value of those Buttons, but I want to set those values from the client. Any ideas?
In case this is too much, in short. I want ONE list of Booleans that is set by the Client, and shown in the server.
Answer by seanr · Jan 11, 2016 at 03:46 PM
use a command and pass the index of the switch
Yeah, I was thinking this is the way to go, but I hadn't gotten it working. And I definitely don't want to look through my controls and send them all during the FixedUpdate event. I guess I can just write a command function, and only send it when that switch is clicked on, or the actual value changes, right?
@seanr For some reason, that doesn't seem to be working. What I want is to have a set of toggle switches, owned by the server, but set by the client. Both the client and server are displaying them in the GUI event, and changing them when clicked on. With my code below, changes set on the server are seen on the client, but changes made on the client do not get reflected on the server.
public SyncListBool ButtonState = new SyncListBool();// 9 virtual buttons... for now
public Texture btnOnTexture;
public Texture btnOffTexture;
void OnGUI()
{
// On server show local player (server). On client show NOT local Player (server)
for (int i = 0; i < ButtonState.Count; i++)
{
GUI.TextArea(new Rect(350, 100 + (i * 25), 150, 20), "Button " + i.ToString() + ": " + ButtonState[i].ToString());
if (ButtonState[i])
{
if (GUI.Button(new Rect(10 + ((i % 2) * 180), 10 + ((i % 5) * 80), 150, 60), btnOnTexture))
{
ButtonState[i] = !ButtonState[i];
Debug.Log("(Locally) Turned off button " + i.ToString());
CmdSetButton(i, false);
}
}
else
{
if (GUI.Button(new Rect(10 + ((i % 2) * 180), 10 + ((i % 5) * 80), 150, 60), btnOffTexture))
{
ButtonState[i] = !ButtonState[i];
Debug.Log("(Locally) Turned on button " + i.ToString());
CmdSetButton(i, true);
}
}
}
}
[Command]
void CmdSetButton(int index, bool value)
{
Debug.Log("(Remotely) Setting " + index + " to " + value);
ButtonState[index] = value;
}
This is almost straight from the code example for [Command], yet that debug statement never gets tripped. Changes made on the server show on the Client. Changing values on the client show on the client, but do not show on the Server (where I need them).
What's wrong with my code?
Your answer
Follow this Question
Related Questions
Unity networking tutorial? 6 Answers
Multiplayer syncronisation 1 Answer
Local Server and Client 1 Answer
Do i need to have 2 seperate apps communicating for server/client relationship? 2 Answers