Search Unity

Local server/client test is buggy?

Discussion in 'Multiplayer' started by Crey, Nov 24, 2009.

  1. Crey

    Crey

    Joined:
    Nov 24, 2009
    Posts:
    16
    I prototyped my game using c++/opengl/raknet and just today started to translate it into Unity (very excited!) because of it's media tools.

    I tried to set up a simple server/client and print a message to the log of each when the connection is accepted. The scripting is easy but the configuration is confusing me.

    I created an empty game object and attached a network view component and script component. Script component looks like this:

    Code (csharp):
    1.  
    2. function OnGUI ()
    3. {
    4.     if(isServer != true  isClient != true)
    5.     {
    6.    
    7.          if(GUI.Button(Rect(Screen.width/2 - 50, Screen.height/2 + 5, 120, 30), "Join Game"))
    8.         {
    9.             if (Network.peerType == NetworkPeerType.Disconnected)
    10.             {
    11.                 Network.useNat = false;
    12.                 Network.Connect("127.0.0.1", 25000);
    13.                 isClient = true;
    14.             }
    15.         }
    16.    
    17.         if(GUI.Button(Rect(Screen.width/2 - 50, Screen.height/2 + 40, 120, 30), "Create Game"))
    18.         {
    19.             if (Network.peerType == NetworkPeerType.Disconnected)
    20.             {
    21.                 Network.InitializeServer(32, 25000);
    22.                 isServer = true;
    23.             }
    24.         }
    25.     }
    26. }
    27.  
    28. function OnPlayerConnected(player : NetworkPlayer)
    29. {
    30.     Debug.Log("Someone has joined your game");
    31. }
    32.  
    33. function OnConnectedToServer()
    34. {
    35.     Debug.Log("You have joined someone's game");
    36. }
    37.  
    So I build/run and then open the executable so that I have 2 instances of the game running. On one I set up the server, on the other I join the server.

    Sometimes the log doesn't print anything, sometimes it prints just "Someone has joined your game" and sometimes it doesn't print anything until I exit the program and then it pukes both messages up. This seems like a simple example, what gives?

    Thanks guys
     
  2. Crey

    Crey

    Joined:
    Nov 24, 2009
    Posts:
    16
    anybody? It seems like it has something to do with window focus. After joining the game, I have give the server window focus and then it will print the connection succeeded, and then I have to give the client focus again for it to have connection succeeded. Is this just how it works?
     
  3. psx

    psx

    Joined:
    Aug 25, 2005
    Posts:
    80
    You have to turn on "run in background" in the player settings. At least, I think that's where it is, I'm not in front of my computer :)