Looking for some help on a script [RPC] Networking

I have been learning C# and Unity on my own for a little while and am having trouble understanding why the following code isn't working properly.

public class PlayerTurn : MonoBehaviour {

GameObject gameManager;
PlayerDatabase dataScript;

public bool isMyTurn = false;

void Start () {

	gameManager = GameObject.Find ("GameManager");
	dataScript = gameManager.GetComponent<PlayerDatabase>();

	if(networkView.isMine == true)
	{
		if( dataScript.PlayerList[0].networkPlayer == int.Parse (Network.player.ToString ()))
		{
			isMyTurn = true;
		}
	}
	else
	{
		enabled = false;
	}
}

void OnGUI()
{
	if(isMyTurn == true)
	{
		if(GUI.Button (new Rect(50,50,130,50), "End Turn"))
		{
			isMyTurn = false;

			for(int i = 0;i<dataScript.PlayerList.Count;i++)
			{
				if(dataScript.PlayerList[i].networkPlayer == int.Parse (Network.player.ToString ()))
				{
					networkView.RPC ("UpdateWhoIsPlayer", RPCMode.AllBuffered, i);
					Debug.Log ("RPC from button, int: " + i);
				}
			}
		}
	}
}

[RPC]
void UpdateWhoIsPlayer(int player)
{
	++player;
	Debug.Log ("RPC Ran");
	if (player > dataScript.PlayerList.Count)
	{
		player = 0;
		Debug.Log ("Set player to 0");
	}
	if(dataScript.PlayerList[player].networkPlayer == int.Parse (Network.player.ToString ()))
	{
		isMyTurn = true;
		Debug.Log ("isMyTurn is: " + isMyTurn);
	}
}

}

The first player to join the game does get isMyTurn set to true and the button appears, when clicked you can see the debug commands show in the console. Here is the screen shot of the inspector/console after the RPC is recieved. http://i.imgur.com/A1cvfb7.png. It appears to set the bool true, but the GUI never shows up on the next player. Any tips are appreciated, I am very new to understanding RPC's and networking. Thanks.

Posted Jun 25, 2014, 6:03 AM UTC

44
combatdave•

You have two player objects with two separate copies of this script, I think. When you do an RPC from player 1, it updates player 1 correctly... But player 3 never recieves an RPC to know that it should be updated.

You could move the player turn id variable into a manager object and do the RPC there and then just have this script compare that ID to its own every frame.

2
spicynuggetsOP•

Thanks for the help.

1
taibi7Hobbyist•

Have you debug logged which networkplayer actually get the isMyTurn bool set to true? I can't seem to find a problem in your code..

edit: I think i see it. You have both scripts on the controlling players right? But the thing with RPC's is that you send the RPC to the other players with the same networkview! This means when you have GameObject with name 3 send the RPC all buffered to the other remote client that is running the game. It will do the method on GO with name 3 and I think you wanted it to do the method UpdateWhoIsPlayer() on player GO with name 1 I assume?

You disabled the scripts on clients that don't control that object but the thing with RPC's is that it still does the RPC despite being disabled. That's why it still does the logs.

You seem to have a GameManager why don't you put your logic into that central object and let the players listen to the Gamemanager?

2
spicynuggetsOP•

I took both of your advice and placed an RPC and int to hold the player turn in my script attached to the game manager. I am not sure why I didn't do that in the first place, I just got lost off in a tangent of not being able to figure it out. I had is solved in less that 5 mins after putting it in the game manager. Thanks guys.

2
[deleted]•

[deleted]

1