- Home /
How to make GUI update for all players in a networked game?
So i'm working on a board game, like axis and allies or total war, and at the beginning of the game, the players choose their country, the option to choose that country is then removed, leaving text saying the country had been chosen, forcing the rest of the players to choose a different country. All of this works fine until i test it over the network. One of the players will choose britain, the option then becomes grayed out on that computer, however, the option remains on all other instances of the game. How can i update some GUI classes, but not others, like, i only want to update/change for all players the options for choosing a country, not things like the create game button.
function OnGUI()
{
if(britainChosen)
{
GUI.Label(Rect(510, 80, 280, 30), "Britain Chosen");
}
if(showLobby)
{
GUI.Box(Rect(500, 25, 300, 25), lobbyName);
GUI.Box(Rect(500, 55, 300, 300), "Country");
if(britainChosen == false)
{
if(GUI.Button(Rect(510, 80, 280, 30), "Britain"))
{
britainChosen = true;
Debug.Log("Chose Britain");
//setup britain
}
}
if(germanyChosen == false)
{
if(GUI.Button(Rect(510, 120, 280, 30), "Germany") && germanyChosen == false)
{
germanyChosen = true;
Debug.Log("Chose Germany");
//setup germany
}
}
}
if(!Network.isClient && !Network.isServer)
{
if(GUI.Button(Rect(btnX, btnY, btnW, btnH), "Create Game"))
{
startServer();
}
if(hostData)
{
for(var i : int = 0; i<hostData.length; i++)
{
if(GUI.Button(Rect(btnX * 1.5 + btnW, btnY * 1.2 + (btnH * i), btnW * 3, btnH * 0.5), hostData[i].gameName))
{
Network.Connect(hostData[i]);
}
}
}
}
}
What your looking for is called an RPC call using the NetworkView component attached to the game object. Use it like so:
this.GetComponent().RPC( "DisableButton", RPC$$anonymous$$ode.ALL_BUFFERED, arg );
This allows you to call methods globally, as the rpcmode is ALL. Read the docs for more info.
NOTE: What you need here though is what's called an Authoritative Server. One of the players or an external Unity scene needs to act as the Server. All players ask the Server what they can do, Server verifies and replies yes or no. In your case, Player tries to take a country, player client asks Server if country is available. If it is, Server makes it unavailable and responds to all clients telling all of them that country is taken. This is a necessary setup to avoid 2 players taking the same country at the same exact time.
Hope this helps!
Answer by GodClaw · May 29, 2013 at 07:16 PM
I'm pretty sure you'd have to use an RPC call to everyone to let them know if britainChosen == true