x


How do I create a server selection menu?

Hello everyone! Been a while since i've been on here. Unity has kept me busy ;).

Now to business:

I have been knocking up a prototype for a game, all coming along fine. But as a little learning project for myself I want to implement some basic networking. Namely, create a MasterServer type connection and see each other running about. Now everything has been going smoothly, until I tried to create the menu.

Now the specific issue i'm having is with creating the "Server selection menu". You have all seen these (I would expect). You are presented with a list of servers, you can select one and press "Join" or "Connect" or what-have-you.

Here's my most recent attempt:

//creating server selection menu element
var hostdata : HostData[] = MasterServer.PollHostList();
for (var element in hostdata)
{
    GUILayout.BeginHorizontal();
        var connections = element.connectedPlayers + "/" + element.playerLimit;
        GUILayout.Label(element.gameName);
        GUILayout.Space(5);
        GUILayout.Label(connections);
        GUILayout.Space(5);
    GUILayout.EndHorizontal();  
    }

Now to me, that says: "Grab the hostdata from the function PollHostList, for each entry in that array create 2 labels in a horizontal layout".

Unity seems to think this means: "Sit here idly with a blank face".

I'm guessing I'm missing some fundamental here. The "for(var element in hostdata)" I'm not entirely sure on, it's something I picked up on from the docs and examples. I'm guessing "elements" means... an element in the array, but I'm probably wrong.

Could someone slap me round the face, tell me to stop being an idiot and point me in the right direction please? (the slap is optional).

Thanks for reading everyone, I hope you can help me.

more ▼

asked Jan 07, 2012 at 10:57 PM

timsk gravatar image

timsk
375 12 11 17

Just a quick update, I've made a bit of progress, here's my latest attempt:

//creating server selection menu element
if(MasterServer.PollHostList().Length !=0)
{

    var hostdata : HostData[] = MasterServer.PollHostList();
    for (var element in hostdata)
    {
        GUILayout.BeginHorizontal();
        var connections = element.connectedPlayers + "/" + element.playerLimit;
        GUILayout.Label(element.gameName);
        GUILayout.Space(5);
        var hostInfo;
        hostInfo = "[";
        for (var host in element.ip)
            hostInfo=hostInfo + host + ":" + element.port + " hostInfo = hostInfo + "]";
        GUILayout.Label(connections);
        GUILayout.Space(5);
        GUILayout.EndHorizontal();  
    }
}

This displays the server name and players / max players as labels. Now I am totally lost as to how I would "select" a server and then join it. Here is my attempt at the code for the "Join" button:

if(GUILayout.Button ("Join Game"))
{
    JoinGame();
}

and the JoinGame() function:

function JoinGame()
{
    Network.Connect(element.ip);
}

Of course, this doesn't work. Would really appreciate some help here.

Jan 08, 2012 at 12:26 PM timsk

Network.Connect can take a 'HostData' object- you can call it just with

Network.Connect(element);

and it will manage all the rest automatically! In any case, this will never work the way you have it, because 'element' exists only within the scope of that for loop (the bits inside the curly brackets after the 'for (var element in hostdata)' line).

You need to put the 'connect' button somewhere inside the loop (maybe just before the EndHorizontal), and have it reference the hostdata made available by the loop. Otherwise, you could use a selectionGrid, and query that.

Have you been able to use the MasterServer at all? Is your server registering properly?

Jan 08, 2012 at 12:32 PM syclamoth

Thanks a lot for replying

My server is registering properly (I can see the server, with the name I set, on a dual-client test). A selection grid seems like the way to go. I'm guessing to do this, I would have to pass the HostData[] array and then tell it how to display the data in the array on the buttons in the grid...

Could you shine some light on how I would pass the information into the grid?

Edit: I have tried the following bit of code:

    var hostdata : HostData[] = MasterServer.PollHostList();
    for(var i = 0; i < hostdata.length; i++)
    {
    selectedServer = GUILayout.SelectionGrid(selectedServer, hostdata, 1);
    }

I need to pass an array of strings to the second paremeter in SelectionGrid(). Will this involve converting the HostData[] into String[] ? Or am I going about this the wrong way?

Jan 08, 2012 at 01:36 PM timsk

I'm going all over the place with this. It's driving me crazy! Here's my most recent attempt involving the selection grid:

//creating server selection menu element
if(MasterServer.PollHostList().Length !=0)
{
    var hostdata : HostData[] = MasterServer.PollHostList();
    for(var element in hostdata)
    {
        serverName.Add(element.gameName);
        connections.Add(element.connectedPlayers + "/" + element.playerLimit);
        selectedServer = GUILayout.SelectionGrid(selectedServer,serverName + connections, 1);
        if(GUILayout.Button("Join Game"))
        {
            Network.Connect(element);
        }

Now I'm getting an error saying I can only call PollHostList from Awake or Start functions? Seems very strange as it didn't have a problem before I tried adding a selection grid.

I'm hoping i'm missing something stupid here, this shouldn't be such a hard task!

Jan 08, 2012 at 10:49 PM timsk

It seems this question has boiled down to. How do I display HostData from PollHostList() in a GUI. Would really appreciate some help on this, been stuck on it for over a week now.

Jan 09, 2012 at 05:57 PM timsk
show all comments (comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

To give an example of what @syclamoth said:

function JoinGame(element : HostData)
{
    Network.Connect(element);
}

And in your loop just add the button:

    [...]
    GUILayout.Label(connections);
    GUILayout.Space(5);
    if(GUILayout.Button ("Join"))
    {
        JoinGame(element);
    }        
    GUILayout.EndHorizontal();  
}
more ▼

answered Jan 08, 2012 at 12:39 PM

Bunny83 gravatar image

Bunny83
75.1k 25 91 322

Thanks bunny, I always forget I can make a function return stuff and use it later. I'll have a play around with this, I do think a selection grid would be easier to setup and easier for the user to use though.

Jan 08, 2012 at 01:37 PM timsk
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1170
x675
x598
x64
x4

asked: Jan 07, 2012 at 10:57 PM

Seen: 1893 times

Last Updated: Mar 02, 2012 at 05:03 PM