Telling connected client to load the hosts lobby.

sigh!

i have been struggling with this…

i have a networklevelload script in my HostLobby scene and in my JoinALobby Scene. The server is in the host lobby and when a client joins, it trys to send a RPC LevelLoad to the client in the joinALobby scene to load the hostlobby.

i have seen all over the forums that you cant have network views from different scenes talk to each other… so how SHOULD i be doing this? i saw SetScope but that is not what i want i think. I want a way to have the client load the same scene as the server.

you can have network views from different scenes talking to each other so long as you don’t do the Network.SetLevelPrefix. Which I think is a bad idea anyway.

If you’re so curious, this is my script for network level loading. the one in the reference stuff never worked for me. You’ll need to change a few lines, probably ones related to debug logging:

//
// IMPORTANT: THIS SCRIPT SHOULD BE ON A GAMEOBJECT BY ITSELF, ONLY IN THE LEVEL WHERE A USER CONNECTS TO THE SERVER
//
static var thisScript : NetworkLoadLevel; //used to make sure there's not duplicates of this object, and for static function access

//these are used for preventing network.instantiate items from being destroyed because they have been recieved 
//before loading the current level.
//to use this effectively, a script on that object needs to check to make sure that dontDestroy is true.
//truthfully this is really only a problem upon first connecting to a server where a game is in progress, not while already connected
public static var dontDestroy : boolean = true;

var netView : NetworkView; //assign the networkview to this

function Awake(){
	//make sure this gameobject isn't already in the scene
	netView = networkView;
	if (thisScript){  //there's already one of these! destroy
		Destroy(gameObject);
		return;
	}
	else{
		//we don't have one these, assign to thisScript
		thisScript = this;
		DontDestroyOnLoad(this);
		//If we don't use this in it's own group that isn't blocked during level loading, it is possible 
		//for a player to connect, and while loading the level, the server switches levels, causing the 
		//connected player to stay on the wrong level. So put it in its own non-blocked networkview group
		networkView.group = 31; //we use this group for level loading. 
		netView.viewID = NetworkViewID.unassigned;
	}
}

function Start(){
	
}

//use this to load a level, not Application.LoadLevel
//static for easy access from other scripts
static function LoadLevel(levelInt : int){
	thisScript.LoadLevelOf(levelInt);
}

function LoadLevelOf(levelInt : int){
	if (Network.isServer){
		Network.RemoveRPCsInGroup(31); //remove any old load level rpcs (keep connecting players from loading one level after another
		//clean up network instantiates
		Network.RemoveRPCsInGroup(NetX.playerGroup);
		Network.RemoveRPCsInGroup(NetX.lfoGroup);
		Network.RemoveRPCsInGroup(NetX.projGroup);
		
		netView.RPC("NetLoad", RPCMode.AllBuffered, levelInt); //sent to all currently connected players, and any new ones that might connect
	}
}

@RPC
function NetLoad (levelInt : int)
{
	dontDestroy = true;
	// 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
	if (DebugConsole.debugMode)DebugConsole.Message("loading a new level, sending disabled");
	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(levelInt); //I might need this. but it might also bork how network level loading works
	//cause a barely connected client will recieve it and then discard it cause it's not the same level prefix
	Application.LoadLevel(levelInt);
	dontDestroy = false; //we've started loading the level. allow objects to be destroyed now.
}

function OnLevelWasLoaded () {
	if (Network.peerType == NetworkPeerType.Disconnected){
		//we aren't actually connected to a server. 
		//Probably because we just barely started the game is how we got here. don't do anything
		return;
	}
	//we finished loading the level
	// 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);
	netView.viewID = NetworkViewID.unassigned;
	if (DebugConsole.debugMode)DebugConsole.LogMessage("Level finished loading. Sending enabled again");


	//for (var go in FindObjectsOfType(GameObject))
		//go.SendMessage("OnNetworkLoadedLevel", SendMessageOptions.DontRequireReceiver);	
}
@script RequireComponent (NetworkView)

So far my understanding is that if NetworkView(s) have the same ViewID no matter what scene they are in (and you’re not setting LevelPrefix) they should talk to each other. I haven’t tested this though.

You might also want to look into the groups mechanism more. Some of the reference material uses groups to maintain communication between player for chatting while levels are being loaded. Just and idea.

I’m also trying to learn this Unity network stuff so feel free to shoot me down if I am wrong in any. It will probably save me hours of work. :wink:

ah man i thought i replied with thank you but i guess it didnt post a while back… Thanks a bunch for the help.

what is NetX in your script btw?

Oh, it’s a network helper script that I have. It happens to have some static integer variables that I use to keep track of network groups.

So you can probably remove those lines for your project.

ah i see, cool