We have been testing some MasterServer implementation on my local network.
We have configured a MasterServer, and created some Hosts.
We have successfully received the server list using the MasterServer.PollHostList method and so on…
The problem is we want the client to connect automatically to the best suitable server, taking into account the server load (users connected and ping), and we are unable to get the ping value correctly.
If we use this piece of code:
var ServerPing : Ping;
function OnGUI()
{
ServerPing = Ping("192.168.1.59");
while(!ServerPing.isDone) {}
var Latency : String = (ServerPing.time).ToString();
guiText.text = ("Server latency: " + Latency);
}
Unity almost freezes on mac, and totally freezes or even do not load on windows.
On the other hand, trying to do something similar to this (deleted other code to go straight to the bone):
var ServerPing : Ping;
var PingTime = -2;
function Awake()
{
ServerPing = Ping("192.168.1.59");
}
function OnGUI()
{
if (PingTime < -2)
{
var Latency : String = (ServerPing.time).ToString();
guiText.text = ("Server latency: " + Latency);
}
}
function Update()
{
if(ServerPing.isDone)
PingTime = ServerPing.time;
}
But it seems that ServerPing is not accessible from Update…
I don’t know, I think this should be easier than it seems. Am I doing something terribly wrong? I have been searching throughout all the forum with no success…
This “while(!ServerPing.isDone) {}” is really not a good idea because it will stop Unity while it’s trying to execute OnGUI. What you should do instead is start somewhare (e.g. in the in GUI) and then in each call to OnGUI check if ServerPing.isDone. Obviously you can’t start Ping(…) each time OnGUI is called so one option is using a flag and once this is set, you don’t call Ping(…) anymore - but instead check for ServerPing.isDone and when that’s the case, calculate your latency.
… it comes up with a NullReferenceException error, and i don’t understand why…
May it be possible that, when we perform a PollHostList we could be losing all references that were inside the Ping array?..
I am completely stuck here, and can’t understand why a single variable works and an array don’t…