Hi, I’m working myself through the networking example, and have a question about the loadlevel script:
@RPC
function LoadLevel (level : String, levelPrefix : int)
{
lastLevelPrefix = levelPrefix;
// There is no reason to send any more data over the network on the default channel,
// because we are about to load the level, thus all those objects will get deleted anyway
Network.SetSendingEnabled(0, false);
// We need to stop receiving because first the level must be loaded first.
// Once the level is loaded, rpc's and other state update attached to objects in the level are allowed to fire
Network.isMessageQueueRunning = false;
// All network views loaded from a level will get a prefix into their NetworkViewID.
// This will prevent old updates from clients leaking into a newly created scene.
Network.SetLevelPrefix(levelPrefix);
Application.LoadLevel(level);
yield;
yield;
// Allow receiving data again
Network.isMessageQueueRunning = true;
// Now the level has been loaded and we can start sending out data to clients
Network.SetSendingEnabled(0, true);
for (var go in FindObjectsOfType(GameObject))
go.SendMessage("OnNetworkLoadedLevel", SendMessageOptions.DontRequireReceiver);
}
What do the two yield statements do exactly? For what I understand yield suspends executement of the function, untill the function is called again. So is this function called twice again?
Instead of throwing in a couple of yields ( which may or may not be enough, who knows… ), an alternate way to handle this would be to use the following callback:
Is that right that the yields just pause the function for a couple of game loops?
According to the Unity documentation, “The yield statement is a special kind of return, that ensures that the function will continue from the line after the yield statement next time it is called.”
That sounds more like it waits until the function is called again.