Show photonView.Owner variable

Options
Hello guys, I started a simple test here to lern how to use photon, I have a doubt,

When I connect to room and load the game I want that each player click on a cube on the center of screen, and a GUIBox showing how much clicks each player done.
The problem is when I tried that the variable click was just for my character, so I'm trying to use Photonview to get each player int value.

Here is my script:

using UnityEngine; using System.Collections; public class ClickToCount : MonoBehaviour { int clicks; public void Awake() { PhotonNetwork.automaticallySyncScene = true; } void OnMouseDown () { print("foAi"); if (transform.gameObject.tag == "Player") { clicks++; print("foi"); } } void OnGUI() { PhotonView photonView; foreach (PhotonPlayer bucetao in PhotonNetwork.playerList) { GUILayout.Box(photonView.owner.name + " = " + clicks); } } }</b>

Comments

  • vadim
    Options
    If you need to show clicks count not only for local player but for others in the room as well, you need to synchronize click count for every player across network. The simplest way to do so is storing count in player or room custom properties.
  • This is just an example that what I need, its a test to I use on all my elements, to Update each player action, I dont know if storing count in player or room properties will help me in this case, because I'm doing this for learning how to share informations between players.

    But I will try this method
  • That was my last try, but info and stream are aways null

    using UnityEngine; using System.Collections; public class ClickToCount : MonoBehaviour { int clicks; int todosClicks; PhotonView photonView1; public PhotonMessageInfo info; public PhotonStream stream; public void Awake() { PhotonNetwork.automaticallySyncScene = true; print(info); print(stream); } void OnMouseDown() { if (transform.gameObject.tag == "Player") { clicks++; } } public void OnPhotonSerializeView() { if (stream.isWriting) { stream.SendNext((int)clicks); } else { this.clicks = (int)stream.ReceiveNext(); } } void OnGUI() { foreach (PhotonPlayer bucetao in PhotonNetwork.playerList) { GUILayout.Box(bucetao.name + ":" + info); } } }
  • vadim
    Options
    You never initialize stream. That's why it's null. But you do not need this var.
    OnPhotonSerializeView method should have 2 parameters: OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    As you can see, one of them is stream passed by PUN to your method.
    See tutorial https://doc.photonengine.com/en/pun/current/tutorials/tutorial-marco-polo and demos included in PUN package for OnPhotonSerializeView usage details.
  • Oh thanks, but I solved that, worked partially, because it dos only update when the room owner click, it update the variable to other clients, but the other players never update the variable to another clients.

    For example: the Room owner click on the scene, the variable change on all clients, but if other player clicks the variable just change on his own client.

    This is my actual code:
    using UnityEngine; using System.Collections; public class ClickToCount : MonoBehaviour { int clicks; int todosClicks; public PhotonView photonView1; PhotonPlayer player; public void Awake() { PhotonNetwork.automaticallySyncScene = true; } [PunRPC] void OnMouseDown() { if (transform.gameObject.tag == "Player") { clicks++; } } public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info) { if (stream.isWriting) { stream.SendNext(clicks); } else { clicks = (int)stream.ReceiveNext(); } } void OnGUI() { foreach (PhotonPlayer bucetao in PhotonNetwork.playerList) { GUILayout.Box(bucetao.name + ":" + clicks); } } }
  • vadim
    Options
    Only object owner (master client for scene objects) can update property so that it broadcast to other clients. On other clients, use RPC call to owner to ask owner to increment clicks.

    Room properties can be updated from any client and are synchronized automatically on all clients. But it's possible to run in race condition when client increments property before previous update reached this client.

    With RPC's routed via owner, you will never lose a click.