- Home /
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.
Just a quick update, I've made a bit of progress, here's my latest attempt:
//creating server selection menu element
if($$anonymous$$asterServer.PollHostList().Length !=0)
{
var hostdata : HostData[] = $$anonymous$$asterServer.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.
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 $$anonymous$$asterServer at all? Is your server registering properly?
Thanks a lot for replying
$$anonymous$$y 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[] = $$anonymous$$asterServer.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?
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($$anonymous$$asterServer.PollHostList().Length !=0)
{
var hostdata : HostData[] = $$anonymous$$asterServer.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!
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.
Answer by Bunny83 · Jan 08, 2012 at 12:39 PM
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();
}
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.
Your answer
Follow this Question
Related Questions
Players can't connect to my server 0 Answers
Networking - MasterServer [ Connect on Lan or Wan without Internet ] 3 Answers
Best way to handle Sockets? 0 Answers
How do network function? 1 Answer
Best practice for a turn-based game 1 Answer