- Home /
The question is answered, right answer was accepted
How fast do Syncvars synchronize?
I'm making an online 2-player Trading Card game.
At the start of the actual cardgame scene, the GameController sends an Rpc to both clients, asking them to send it their playerName and decklists, which are both saved locally as a simple string.
My Question is basically, when
myGui.SetPlayerData(resourceContainer.GetPlayerData());
is in the code, right below
clientController.RpcClientSendPlayerData();
will the function GetPlayerData() already return the data that is sent by the clients when RpcClientSendPlayerData is called?
Are Syncvards synched immidiately? Or will program wait until everything is synched before calling RpcClientSendPlayerData ?
Code Extracts:
(GameController, Server-only)
clientController.RpcClientSendPlayerData();
myGui.SetPlayerData(ResourceContainer.GetPlayerData());
(ClientController)
[ClientRpc]
public void RpcClientSendPlayerData()
{
localPlayer.CmdPlayerSendPlayerData();
}
(Script on the local player, local player authority)
[Command]
public void CmdPlayerSendPlayerData()
{
resourceContainer.SetPlayerData("host", myName, myDeckString);
}
(ResourceContainer)
[SyncVar]
string hostName;
[SyncVar]
string hostDeckString;
public void SetPlayerData(string player, string playerName, string deckString)
{
if (player == "host")
{
hostName = playerName;
hostDeckString = deckString;
}
}
Thanks in advance!
Answer by Bunny83 · Jan 26, 2017 at 05:26 AM
I have barely worked with the new networking system. However syncvars are just "syntactical sugar". Unity actually creates RPC methods to sync the content of those variables when you set them. So in your case i would say; no, you won't see the update unless you have a damn fast connection and a slow PC ^^.
To address your question "How fast do Syncvars synchronize?", well, how fast does a car go? The answer is: it depends on the car and on the road it drives on ^^.
However you can specify a "hook" in your syncvar attribute. Basically just the name of a method that should be executed whenever the value of the syncvar has changed on the client.
Something like that:
[SyncVar(hook="OnHostNameChanged")]
string hostName;
void OnHostNameChanged()
{
// "hostName" has just received an update
}
Okay, so it is as I expected, thanks bunny!
You say it depends on the car and the road, and that makes sense of course, but what kind of range could I expect, anything between 0.1 and 1 seconds, or longer?
The game will be on android, and 3 instances of it will run over a local hotspot I create with my own phone probably, the 3 devices are relatively old, between three and six years old.
If you could give me a general idea of what kind of delay I should expect, that would be appreciated!
The range you can expect can reach from 1ms to "infinity"(if the connection is interrupted). The latency depends completely on the connection. If someone in europe is connected to someone in the usa you can expect at least 25ms+ as that's the time a signal would take at the speed of light. In most cases it's way higher since there will be several routing nodes in between. Common values would be 70ms - 300ms. Though it's never guaranteed.
If two people are on the same local network you usually get latencies below 10ms, usually even < 2ms. Though wlan is more vulnerable to interference and generally a little bit slower, though in most cases you wouldn't see any difference.
$$anonymous$$obile internet connections (GS$$anonymous$$ / GPRS, U$$anonymous$$TS, LTE, ...) have in general a slightly higher latency.
okay, that's also somewhere in range of what I expected I've introduced a fixed two second delay, on top of the 5 second delay I allow for both players connecting to and loading the scene. Ideally something would happen on each players' screen for the duration, but for now there's a loading screen.
Thanks again bunny, this question can be closed :)
Follow this Question
Related Questions
SyncVar works with disabled components? 0 Answers
[SyncVar] protect Cheat Engine 0 Answers
Networking Sync SetActive Not Working 0 Answers
Synchronizing game object across clients 2 Answers