Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Display Found Master Server Games

Discussion in 'Scripting' started by nickavv, Nov 21, 2007.

  1. nickavv

    nickavv

    Joined:
    Aug 2, 2006
    Posts:
    1,801
    How should I display in UnityGUI the games that are found by the master server. Ideally, each would be its own button. Here's what I have:

    Code (csharp):
    1.  
    2. var redPrefab : Transform;
    3. var bluePrefab : Transform;
    4. var redSpot : GameObject;
    5. var blueSpot : GameObject;
    6. var gamesName = "Enter the name of your game!";
    7. var gamesDesc = "Enter a short description of your game!";
    8. var gameMaster = false;
    9. var gameJoiner = false;
    10. var gameString = "";
    11.  
    12. function OnGUI () {
    13.     if (!gameMaster  !gameJoiner) {
    14.         if (GUILayout.Button("Create Game")) {
    15.             gameMaster = true;
    16.         }
    17.         if (GUILayout.Button("Join Game")) {
    18.             gameJoiner = true;
    19.         }
    20.     }
    21.     if (gameMaster == true){
    22.         gamesName = GUILayout.TextField(gamesName);
    23.         gamesDesc = GUILayout.TextArea(gamesDesc);
    24.         if (GUILayout.Button ("Create Game")) {
    25.             Network.useNat = !Network.HavePublicAddress();
    26.             Network.InitializeServer(32, 25002);
    27.             MasterServer.RegisterHost("RandomBoxWithCubesGame",gamesName,gamesDesc);
    28.         }
    29.     }
    30.     if (gameJoiner == true) {
    31.         GUILayout.Button(gameString);
    32.     }
    33. }
    34.  
    35. function Awake () {
    36.     MasterServer.ClearHostList();
    37.     MasterServer.RequestHostList("RandomBoxWithCubesGame");
    38. }
    39.  
    40. function Update () {
    41.     if (gameJoiner == true) {
    42.         if (MasterServer.PollHostList().length != 0) {
    43.             var hostData: HostData[] = MasterServer.PollHostList();
    44.             for (var i:int=0; i<hostData.length; i++) {
    45.  
    46.                 gameString += hostData[i].gameName+": "+hostData[i].comment;
    47.             }
    48.             MasterServer.ClearHostList();
    49.         }
    50.     }
    51. }
    52.  
    There are two obvious problems here. The first is that when there are multiple games, there will still just be one button. The other problem is that if a game goes away, the GUI won't reflect the change. You'd think I could just do:

    Code (csharp):
    1. var redPrefab : Transform;
    2. var bluePrefab : Transform;
    3. var redSpot : GameObject;
    4. var blueSpot : GameObject;
    5. var gamesName = "Enter the name of your game!";
    6. var gamesDesc = "Enter a short description of your game!";
    7. var gameMaster = false;
    8. var gameJoiner = false;
    9.  
    10. function OnGUI () {
    11.     if (!gameMaster  !gameJoiner) {
    12.         if (GUILayout.Button("Create Game")) {
    13.             gameMaster = true;
    14.         }
    15.         if (GUILayout.Button("Join Game")) {
    16.             gameJoiner = true;
    17.         }
    18.     }
    19.     if (gameMaster == true){
    20.         gamesName = GUILayout.TextField(gamesName);
    21.         gamesDesc = GUILayout.TextArea(gamesDesc);
    22.         if (GUILayout.Button ("Create Game")) {
    23.             Network.useNat = !Network.HavePublicAddress();
    24.             Network.InitializeServer(32, 25002);
    25.             MasterServer.RegisterHost("RandomBoxWithCubesGame",gamesName,gamesDesc);
    26.         }
    27.     }
    28.     if (gameJoiner == true) {
    29.         if (MasterServer.PollHostList().length != 0) {
    30.             var hostData: HostData[] = MasterServer.PollHostList();
    31.             for (var i:int=0; i<hostData.length; i++) {
    32.  
    33.                 GUILayout.Button( hostData[i].gameName+": "+hostData[i].comment);
    34.             }
    35.             MasterServer.ClearHostList();
    36.         }
    37.     }
    38.  
    39. }
    40.  
    41. function Awake () {
    42.     MasterServer.ClearHostList();
    43.     MasterServer.RequestHostList("RandomBoxWithCubesGame");
    44. }
    But that sadly displays nothing. O-o This has been confusing me for days now, with no good results. Help a guy out! :)
     
  2. nickavv

    nickavv

    Joined:
    Aug 2, 2006
    Posts:
    1,801
    *pokes- No! KICKs thread*

    >.> Oy this is tough. StarManta, how did you display games in the GUI in Castles?
     
  3. larus

    larus

    Unity Technologies

    Joined:
    Oct 12, 2007
    Posts:
    277
  4. nickavv

    nickavv

    Joined:
    Aug 2, 2006
    Posts:
    1,801
    o_O How on Earth did I miss THAT!

    >.< Don't you love that feeling of mixed relief and embarrassment when you just did something really stupid?

    Thanks larus!