Network Level Load question

Hello,

I have a virtual world with multiple players. I am developing the networking part in which the player joins the game and is instatiated in my level.

I am using the NetworkLevelLoad script from the tutorials. But in that script we connect to the actual level by clicking on a Play button like so:

function OnGUI ()
{
	// When network is running (server or client) then display the level "MainScene"
	if (Network.peerType != NetworkPeerType.Disconnected)
	{
		if (GUI.Button(new Rect(350,10,100,30),"Play"))
		{		
			// Make sure no old RPC calls are buffered and then send load level command
			Network.RemoveRPCsInGroup(0);
			Network.RemoveRPCsInGroup(1);

			// Load level with incremented level prefix (for view IDs)
			networkView.RPC( "LoadLevel", RPCMode.AllBuffered, "MainScene", lastLevelPrefix + 1);
		}
	}
}

However I want the player to immediately join the level without clicking on the Play button so I moved the code in the OnConnectedToServer function like so:

function OnConnectedToServer()
{
    // Make sure no old RPC calls are buffered and then send load level command
    Network.RemoveRPCsInGroup(0);
    Network.RemoveRPCsInGroup(1);
	
     // Load level with incremented level prefix (for view IDs)
     networkView.RPC( "LoadLevel", RPCMode.AllBuffered, "MainScene", lastLevelPrefix + 1);
}

And when I tested it with one player already in the scene ( the server ) when another player tries to join the “LoadLevel” RPC is being called twice!

I don’t understand why that is because OnConnectedToServer is only being called once!

Anyone knows what is happening here?

If I were to hazard a guess… I am hazarding a guess here, and am thinking that it might be a problem with the ‘LoadLevel’ function.
Try doing a Debug.log, or Print command and see if it fires twice.

Yes that’s what I did in the first place in order to find out what’s wrong. LoadLevel fires twice but OnConnectedToServer only fires once in the client and zero times on the server… Weird…

Very strange. So, when you put either of those functions in your loadlevel function, they fire twice?

Ok you just lost me here with your phrasing… Let me explain what I’m doing more clearly so that we’re on the same page:

We have a client and a server. The server is in a level that we’ll call MainScene.

Now the client tries to connect to the server. The client succesfully connects so OnConnectedToPlayer is being called on the client. So far so good.

In the current version, we check if the client is connected and if he is we display a button “Play” that loads the MainScene level when it is pressed by calling an RPC called LoadLevel. So in our example the client connects to the server, sees the Play button, presses it and fires the LoadLevel RPC to get into the MainScene. That works.

However as I said I dont want the client to click on a button to get in the level. So I moved all the code from the Play button in the OnConnectedToServer function. So in this scenario the client connects, OnConnectedToServer is being called and for some weird reason I see the RPC LoadLevel called twice even though there was only one call to OnConnectedToServer.

Indeed, I understand the problem. God networking is annoying. I shall get back to you.

What does lastLevelPrefix start at as a value? Maybe try decreasing that value by one to start. Then see what happens. I am not saying it will work, but I found that when I dabbled with networking, some things were fixed by stupid things that don’t make sense.

It starts as 0 so the level is that is loaded first has prefix = 1. But I tried decreasing that value and it made no difference. I mean as you said it wouldn’t make any sense. I also tried disabling sending network messages when the client is connected also to no avail.

It would be funny if it worked if you used this inside the onconnected function: if (Network.peerType != NetworkPeerType.Disconnected) and it then worked. By no means would it make sense.

Nope! That didn’t work either… Frustrating…!

I had a similar issue yesterday.

Turned out that I had another script that also had the OnConnectedToServer function defined, but without a Debug.Log entry so in my debugger the OnConnectedToServer appeard to run once, but it actually ran twice.

This resulted in a player being added twice because both OnConnectedToServer functions had the AddPlayer RPC but only one of them logged it to the debugger. After realizing this and removing the OnConnectedToServer from the script I had previously used for testing it all worked fine.

Hope something like this is the cause of your issue and that you can locate / fix it =)

Thanks for the reply. Actually I have now stopped using the built-in Networking and switched to SmartFoxServer2X. I needed to do this anyway so I’m no longer trying to fix that issue…

Thanks a lot anyway!

Ok, np =) … and “oh no” - another one to evaluate… I just spent two hours looking at uLink. Are you happy with SmartFox so far?

You should first decide what kind of networking you need. uLink is not comparable to SmartFox / Photon, they have totally different requirements and capabilities and are not replaceable through the other

So far I’m happy because it supports the features that I was looking for and it has a lot of community support. Also it has a very cool web admin tool to manage the server!

The only thing that bothers me is that I have to write Java for server-side logic which also means that I cannot use the Unity API for server-side logic…

But what are you gonna do…

@dreamora - ok, thanks. uLink seems to fit the bill for me so far and I haven’t looked at SmartFox / Photon yet. Funny enough I’ve spent the last 4 day and nights developing stuff that I see uLink comes with out of the box, doh =)

@vkalpias - thanks for the info =)