Network.isServer and -batchmode issue

I’m new too coding so would appreciate some help on this.

put simply when I run Unity i have to click the GUI button for it to run.

when i run Unity in batchmode, I believe it would run (play) but would still require the user input to initialize the server (if im correct that is).

So my guess is you would have to code it to…

if Network.isServer
Network.InitializeServer(32, connectPort, useNat);
else
if Network.isClient
Network.Connect(connectToIP, connectPort);

But where and how?

The code below is something else i tried which kind works but keeps disconnecting/reconnecting:

function OnGUI ()
{

	if (Network.isServer == Network.isServer){
        Network.InitializeServer(32, connectPort, useNat);
        		GUILayout.Label("SERVER connected");

	}  if (Network.peerType == NetworkPeerType.Disconnected){
	//We are currently disconnected: Not a client or host
		GUILayout.Label("SERVER disconnected");
		
	}else{
		//We've got a connection(s)!
		
                Network.Connect(connectToIP, connectPort);

		if (Network.peerType == NetworkPeerType.Connecting){
		
			GUILayout.Label("Connection status: Connecting");
			
		} else if (Network.peerType == NetworkPeerType.Client){
			
			GUILayout.Label("Connection status: Client!");
			GUILayout.Label("Ping to server: "+Network.GetAveragePing(  Network.connections[0] ) );		
			
		} else if (Network.peerType == NetworkPeerType.Server){
			
			GUILayout.Label("Connection status: Server!");
			GUILayout.Label("Connections: "+Network.connections.length);
			if(Network.connections.length>=1){
				GUILayout.Label("Ping to first player: "+Network.GetAveragePing(  Network.connections[0] ) );
			}			
		}

		if (GUILayout.Button ("Disconnect"))
		{
			Network.Disconnect(200);
		}
	}
	

}

Another thing i tried was a application start method but the server and client loaded 1 instead of 0.

All I want is the server to run in a headless mode. I don’t have the pro version, I don’t think this is whats stopping me though.

For debugging I ran without the batchmode command just to see.

Just so you know what it is I’m trying to do;

I set up 4 scenes, one is mainly for the server and the rest for the player.
The server is running - scene 0

a player loads up his web player “scene[1]” and clicks continue (eventually I get stats and spawn position passed into the game)

this loads the correct level “scene[2]” ( and their last spawn position)

player dies and presented with scene[3] (You died…(spawn saved))
count down to load

Load scene[1] / end of loop

This is where I ran into problems, it was like the clients browser did not know the server was running
I’ve been putting bits and pieces and trying things out for days now, my head will explode soon…please any help in any way :frowning:

even a brief moment of clarity would stop this head ache

I don’t know anything about batch mode but some logic errors jump out at me in your script.

if (Network.isServer == Network.isServer){
...

This is a useless test. Network.isServer is always equal to itself, so the expression always evaluates to true. Currently, your script is initializing a server once per frame (!) and if it succeeds (which I guess it will, at least the first time) then Network.peerType will be NetworkPeerType.Server. So, in the next instant it will try to also connect to the server at “connectToIP”.

You need to rethink your code.

//We've got a connection(s)!
		
Network.Connect(connectToIP, connectPort);

If you have a connection why are you trying to connect? :stuck_out_tongue:

You can also execute a function when you start bacthmode : -executeMethod ClassName.MethodName
So the easiest would be start the server function when you enter batchmode.

this is/was for when the client connects. When their browser loads (it should know the server is running) they click continue they execute Network.Connect(connectToIP, connectPort);

AH YES! this could be what im looking for, is that for pro unity though???

so in the command add -batchmode - -executeMethod ClassName.MethodName

??? Could I see an example of this

That will not work. This form of batchmode is only for the editor.

On the client its really only -batchmode and you would use System.Environment.CommandLine.Contains(“-batchmode”) to find out if its a server or not. (unless you do it through a const / define)

So the editor would run with just the batchmode command

and then in the script put something like
if System.Environment.CommandLine.Contains(“-batchmode”) {
applicationLoad[0];
} else {
applicationLoad[1];
}

Correct? If so where should I put this, in a function, should I put a Start() function for it?

Thanks to all so far btw most helpful. I’m determined to get this up and running how I want for a MMO i’m planning - yes much to to learn and do

edit; not applicationload more like Network.InitializeServer(32, connectPort, useNat);
???

ah yes, Dreamora is right that it’s for the editor but there are other options that would also work.

ok I got

function Start()
{
    if (System.Environment.CommandLine.Contains("-batchmode"))
    {
        Network.InitializeServer(32, connectPort, useNat);
        } else {
		Application.LoadLevel (1);
    }  
}

I run unity in batchmode, load up the webplayer and get the blue screen, server screen but no load.

it loads when running it in unity. am i doing something wrong

If you build webplayers you must rewrite it cause then the System.Environment.xxxx stuff must be within

#if !UNITY_WEBPLAYER
 ...
#endif

webplayers can’t access anything on the system.

see the manual page on conditional / platform dependent compilation for more information

Ah I see so like this?

   function Awake() {

            #if UNITY_EDITOR
              Debug.Log("UNITY_EDITOR");
        Network.InitializeServer(32, connectPort, useNat);
             #endif

          #if UNITY_WEBPLAYER
    Debug.Log("UNITY_WEBPLAYER");
		Application.LoadLevel (1);
          #endif
}

I just tried it with the ! but doesn’t run at all. what you think I should do
edit: ah forgot the System.Environment.xxxx ill play some more - or how would i implement this?

MUCH THANKS

If you only want to allow the editor to start servers then yes.
But I assumed you want to allow clients built from pro, started in headless mode (using the -batchmode command line argument) to host the servers, in that case you want to use the if that I provided

Sorry i think i explained it bad. No my editor would be the server. I am thinking large scale MMO mind - so not sure if its the best approach.

I have a domain and can provide my own hosting for testing purposes, but when it goes live, I don’t quite know yet…I’ll have to look into it

This will be a pain but once I’m over most of the functionality the rest will be down to level design. I have the ORK RPG kit too.

aha BINGO, the following seems to have worked

   function Awake() {

            #if UNITY_EDITOR
              Debug.Log("UNITY_EDITOR");
        Network.InitializeServer(32, connectPort, useNat);
             #else
              #if UNITY_WEBPLAYER
		Application.LoadLevel (1);
          #endif
           #endif
}

Except the client doesn’t seems to have connected in the browser - hmm