Networking Example levelload - 2 yields ?

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?

Well, think if a yield as a break. It stops the function for a certain period of time, and then starts back up where it left off.

yield WaitForSeconds(1); waits for one second.

I think that a plain yield (“yield;”) only waits for one frame. Those yields are in there to wait for the world to load.

Likewise, the load is sent, and then the system waits for two frames - by then, the world will have loaded.

Ok. So a solo ‘yield’, yields for 1 frame. Go that. Thanks :slight_smile:

It certainly seems that way, I don’t see it waiting for anything else.

I think they did two yields just to make shure everything else was loaded - perhaps one extra yield to wait for all Awake() calls or sth like that :wink:

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:

http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.OnLevelWasLoaded.html

Yes, I think that’s a cleaner way of doing it. Thanks :slight_smile:

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.

how to implement yield in c#? tried yield return with IEnumerable but not same result as in javascript.

is there any example of network level loading in c#?