- Home /
How to search for multiplayer games
Hi, I am trying to make a list of games pop up when I press a button that refreshes the list, and it works fine and so, but there's a problem with the game names. So my problem is that if anyone hosts a game with a different name than the default one, you'd have to enter the name of that game in order for it to pop up, and this is really annoying, how do I fix this so that it shows any game even if the name is different?
Here's my script:
var SwatPlayer : GameObject;
var SoldierPlayer : GameObject;
var SwatSpawn : Transform;
var SoldierSpawn : Transform;
var gameName : String = "Game";
private var refreshing : boolean = false;
private var hostData : HostData[];
var ChoosingTeams : boolean = false;
var EmptyName : boolean = false;
var TeamsScript : ChooseTeams;
var Online : boolean = false;
private var RespawnText : GUIText;
function OnGUI () {
if(!Network.isClient && !Network.isServer) {
GUI.Box (Rect (Screen.width/2 - 25,Screen.height/2 - 55,150,115), "Multiplayer [Alpha]");
gameName = GUI.TextField(Rect(Screen.width/2,Screen.height/2 -10,100,20),gameName);
GUI.Label(Rect(Screen.width/2,Screen.height/2 -30,150,20),"Host a game");
if(EmptyName){
GUI.Label (Rect (Screen.width/2, Screen.height/2 -12, 430, 20), "Please enter a game name, or your game won't register");
}
if (GUI.Button(Rect(Screen.width/2,Screen.height/2 + 10,100,20),"Start Server")) {
TeamsScript.enabled = true;
Screen.lockCursor = false;
}
if (GUI.Button(Rect(Screen.width/2,Screen.height/2 + 30,100,20),"Refresh List")) {
Debug.Log("Refresh");
refreshHostList();
}
if(hostData) {
for(var i:int = 0; i<hostData.length; i++) {
if(GUI.Button(Rect(Screen.width/2 - 20,Screen.height/2 + 80,270,20),"Game: " + hostData[i].gameName)) {
Network.Connect(hostData[i]);
TeamsScript.enabled = true;
Screen.lockCursor = false;
}
}
}
}
}
function Update () {
if(String.IsNullOrEmpty(gameName)){
EmptyName = true;
}
else
EmptyName = false;
if(refreshing) {
if(MasterServer.PollHostList().Length > 0) {
refreshing = false;
Debug.Log(MasterServer.PollHostList().Length);
hostData = MasterServer.PollHostList();
}
}
}
function startServer () {
var UseNat = !Network.HavePublicAddress();
Network.InitializeServer(32,25001, UseNat);
MasterServer.RegisterHost(gameName, gameName, "This is a beta server");
Online = true;
}
function OnConnectedToServer () {
if(Network.isClient == true){
TeamsScript.enabled = true;
}
}
function OnServerInitialized () {
Debug.Log("The server is setting up...");
}
function OnMasterServerEvent(mse:MasterServerEvent) {
if(mse == MasterServerEvent.RegistrationSucceeded) {
Debug.Log("Registered Server with name " + gameName);
}
}
@RPC
function spawnSwat () {
Network.Instantiate(SwatPlayer, SwatSpawn.position, Quaternion.identity, 0);
Debug.Log("Spawned a player in the team S.W.A.T");
}
@RPC
function spawnSoldier () {
Network.Instantiate(SoldierPlayer, SoldierSpawn.position, Quaternion.identity, 0);
Debug.Log("Spawned a player in the team Soldiers");
}
function spawnSwatAndServer () {
startServer();
if(Online == true){
Network.Instantiate(SwatPlayer, SwatSpawn.position, Quaternion.identity, 0);
Debug.Log("Spawned a player in the team S.W.A.T and started the server");
}
}
function spawnSoldierAndServer () {
startServer();
if(Online == true){
Network.Instantiate(SoldierPlayer, SoldierSpawn.position, Quaternion.identity, 0);
Debug.Log("Spawned a player in the team Soldiers and started the server");
}
}
function refreshHostList () {
MasterServer.RequestHostList(gameName);
refreshing = true;
}
@RPC
function ReSpawn () {
Debug.Log("A player died, the player will respawn once he/she has chosen a team");
TeamsScript.enabled = true;
}
a little advice, probably best not to paste 220 lines of code onto your question, leave out the parts that don't affect you question, most people won't want to read through all of your code, and will probably stop reading it halfway through because they are just too lazy, while, granted, there are those awesome people who are willing to read through it.
feel free to not really explain your code too much. for instance :
if (GUI.Button(Rect(10,10,50,50),btnTexture)){
//code
}
one can assume that the code is in the OnGUI function even though the code doesn't say so. on first glance people generally don't assume you're stupid and that variables in your code have been defined already.
when you do this just be sure to say that this is not all of your code.
Your answer
Follow this Question
Related Questions
Multiplayer Script error 2 Answers
how to add option to buy ingame items on my unity game 0 Answers
How to spawn a player in multiplayer on Android with Photon? 1 Answer
Bullet Fire script not working 1 Answer
How to make server files 1 Answer