loadLevelSystem

CURRENT PROBLEM FROM POST 5 ON…

hey I’m trying to give my server the option to choose the level to play, which works. don’t know if it is the best way to do it before I actually start the server or if i better do it after the server is up… anyway that’s not my problem.
the client doesn’t have the map loaded if i connect to the server. i use 1 build and so off course the level isn’t loaded if i connect to the server… so my Question is what would be the best way to update my client with what is on the server once it connects?
I’m a noob programmer and tried RPC’s but it so confusing.

this how i load the level on the server and start the server:

...
if (iWantToSetupAServer  !picked)
		{
			GUILayout.Label("Select a Level");
			scrollPosition=GUILayout.BeginScrollView (scrollPosition);
			foreach(string level in levels)
			{
				if (GUILayout.Button(level))
				{
					Application.LoadLevelAdditive(level);
					picked=true;
					levelOnServer=level;
				}
			}
    		GUILayout.EndScrollView();
			
		}
if(iWantToSetupAServer == true  picked)
		{
			GUILayout.Label("Enter a name for your server");
			serverName = GUILayout.TextField(serverName);
			GUILayout.Space(5);
			GUILayout.Label("Server Port");
			connectionPort = int.Parse(GUILayout.TextField(connectionPort.ToString()));
			GUILayout.Space(10);
			if(GUILayout.Button("Start my own server", GUILayout.Height(30)))
			{
				Network.InitializeServer(numberOfPlayers, connectionPort, useNAT);
				PlayerPrefs.SetString("serverName", serverName);
				iWantToSetupAServer = false;
			}
			if(GUILayout.Button("Go Back", GUILayout.Height(30)))
			{
				iWantToSetupAServer = false;	
			}
		}
...

this is the code in the same script i use to connect to the server:

...
if(iWantToConnectToAServer == true)
		{
			GUILayout.Label("Enter your player name");
			playerName = GUILayout.TextField(playerName);
			GUILayout.Space(5);
			GUILayout.Label("Type in Server IP");
			connectToIP = GUILayout.TextField(connectToIP);
			GUILayout.Space(5);
			GUILayout.Label("Server Port");
			connectionPort = int.Parse(GUILayout.TextField(connectionPort.ToString()));
			GUILayout.Space(5);
			if(GUILayout.Button("Connect", GUILayout.Height(25)))
			{
				if(playerName == "")
				{
					playerName = "Player";	
				}
				if(playerName != "")
				{
					Network.Connect(connectToIP, connectionPort);
					print (levelOnServer);// off course this stays null because the level button was never clicked as a client
					PlayerPrefs.SetString("playerName", playerName);
				}
			}
			GUILayout.Space(5);
			if(GUILayout.Button("Go Back", GUILayout.Height(25)))
			{
				iWantToConnectToAServer = false;
			}
		}
	}
...

if I have to use RPC (which i guess i will) where and how? :-s
thanks
J.

ok this did the trick! http://docs.unity3d.com/Documentation/Components/net-NetworkLevelLoad.html
rewrote the RPC in there to C# and implemented it my script… also change to load a map after the server is up.
now on to changing the map without shutting the server down… :-s

half way there…
i get 3 error when changing a level:

View ID AllocatedID: 3 not found during lookup. Strange behaviour may occur

Couldn’t perform remote Network.Destroy because the network view ‘AllocatedID: 3’ could not be located.

NullReferenceException
UnityEngine.GameObject.GetComponent[NetworkView] () (at C:/BuildAgent/work/b0bcff80449a48aa/Runtime/ExportGenerated/Editor/UnityEngineGameObject.cs:18)
MultiplayerScript.resetLevel (NetworkViewID viewID) (at Assets/MyAssets/scripts/multiplayerScripts/MultiplayerScript.cs:385)

the server works fine it deletes the needed GameObjects from the hierarchy and loads the newly needed additive
but the client doesn’t delete only loads and gives the errors…

this is the RPC I’m using:

(GUILayout.Button("Change Level"))

        {

            NetworkViewID viewID = Network.AllocateViewID();

            networkView.RPC( "resetLevel", RPCMode.AllBuffered, viewID);

            connected = true;

        }

    

    [RPC]

    void resetLevel (NetworkViewID viewID)

    {
        //Network.RemoveRPCs(viewID);
        print (levelOnServer);

        GameObject currentLevel = GameObject.Find(levelOnServer);

        NetworkView nView;

        nView = currentLevel.GetComponent<NetworkView>();// last error brings me to this

        print (nView);

        nView.viewID = viewID;

        //if(networkView.isMine)

        Network.Destroy (viewID);

}

i read somewhere i have to use remove RPC but I’m not sure how to or where…
any help is welcome…

arrrg i was making it way to difficult. i just needed this:

if(GUILayout.Button("Change Level"))
		{
			//NetworkViewID viewID = Network.AllocateViewID();
			//Network.RemoveRPCs(viewID);
			//networkView.RPC( "resetLevel", RPCMode.All, viewID);
			GameObject currentLevel = GameObject.Find(levelOnServer);
			Network.Destroy(currentLevel);
			connected = true;
		}

Ok so it all works in a certain way, but i want it to work properly…

atm if a new players joins after a game or two he has all the played levels on top of each other in his game… i found this about that:

(posted by apples)
Maybe it would be even better to do it like this :

NetworkViewID Vid = GameObjectThatWillBeDestroyed.networkView.viewID;
Network.Destroy(GameObjectThatWillBeDestroyed);
Network.RemoveRPCs(Vid);

but I’m not sure where to place it and how to make sure the last level loaded by the RPC isn’t deleted…?

an other problem is that if i start a server, connect, shut down the server and start one up again with the same game instance I get this: RPC “LoadLevel” failed because networkView: “SceneID: 1”, “LevelPrefix: 0” don’t exist.

so besides from these two things it working quit well.

I don’t seems to get alot of answers here (as usual ;)) but please if some one could help me out here I can finish up my game. :slight_smile: