Using RPCs to update a boolean

Options
I'm using unity and PUN.

I have the following script for objects. I used the tutorial to make a really simple multiplayer game (cubes moving around). Now I'm trying to make a turn based structure using the player IDs and an RPC to tell which player to go. Pressing space disables your controls (ends the turn). However, if someone else pressed space the RPC signals all players and their controls get enabled.

I've put in debug scripts and everything does happen as it should, but the check in update() doesn't seem to change. How to I make the script realize the variable was changed?

EDIT: cleaner code viewing with syntax highlighting: http://pastebin.com/bT9WRMHf
using UnityEngine;
using System.Collections;

public class HostScript : Photon.MonoBehaviour 
{
	public float speed = 10f;
	volatile bool isMyTurn = true;

	// Use this for initialization
	void Start () {
		isMyTurn = true;
	}
	
	// Update is called once per frame
	void Update()
	{
		if (photonView.isMine && isMyTurn)
		{
			InputMovement();
		}
	}
	
	void InputMovement()
	{
		if (Input.GetKey(KeyCode.W))
			rigidbody.MovePosition(rigidbody.position + Vector3.forward * speed * Time.deltaTime);
		
		if (Input.GetKey(KeyCode.S))
			rigidbody.MovePosition(rigidbody.position - Vector3.forward * speed * Time.deltaTime);
		
		if (Input.GetKey(KeyCode.D))
			rigidbody.MovePosition(rigidbody.position + Vector3.right * speed * Time.deltaTime);
		
		if (Input.GetKey(KeyCode.A))
			rigidbody.MovePosition(rigidbody.position - Vector3.right * speed * Time.deltaTime);
		
		if (Input.GetKey(KeyCode.Space))
		{
			photonView.RPC("nextTurn", PhotonTargets.All, PhotonNetwork.player.ID);
		}
	}

	[RPC] void nextTurn(int id)
	{
		// I pressed the button
		if (id == PhotonNetwork.player.ID) 
		{
			isMyTurn = false;
		}

		// Another player called it
		else
		{
			isMyTurn = true;
		}
	}
}

Comments

  • So a quick update. I put some more debug code in the update method, pressed space to end my turn and then pressed space on another instance of the game to make it my turn again. A few seconds later here's part of the debug log:

    HxKheQV.png

    So for some reason I've lost control of the photonview and when I regain it, the boolean is flipped for whatever reason. During this time there was no input.
  • Ok so I've managed to narrow it down to losing control of the photon view... or I think. Basically when the other player presses space photonview.ismine is false.
  • vadim
    Options
    Hi,

    It would be more useful to log nextTurn() calls with parameter and resulting isMyTurn variable
  • I've been tinkering with it over the last few days trying to figure it out and this is my current version. The logic is the same but I just moved how the turn ends and debug lines around: http://pastebin.com/hjauiNyn

    This debug code, upon joining a room with someone already in it, pressing space and then having the other person pressing space (should be my turn) generates this as a debug log:

    JCFp6Kp.png
  • Tobias
    Options
    I didn't read all at the moment but does this help? Check the answer here.
    http://answers.unity3d.com/questions/87 ... issue.html
  • Potentially, I'll check it out. I did find that photon has player properties, which I think will fix things.
  • Thank for the help though.