Example scenario : there are 4 seats on the stage, and I have an int value named x. The x value increases by 1 each time the player sits in the chair. When you get up from the seat, the x value decreases by 1. I send this value to all players with the help of rpc. When all the players in the game are seated (when the number of players in the game is equal to x) I load a photon object into the scene. The last person sitting on the seat uploads this object to the stage. Because when the last player sits on the seat, the value of x becomes equal to the number of players in the game. In part of the game I have to completely destroy this object. Only the last player in the seat can destroy it because the owner of the object is himself. What I want is this: Even if the masterclient is the first player to take the seat, it waits for the other players to take the seat, and when the x value equals the number of players in the game (when the last player is seated), the masterclient loads the item. So the owner of the object becomes the masterclient in the game. And masterclient can destroy the object at will. How can I do that. Code file I use:
public class control : MonoBehaviourPunCallbacks{
private int x;
private GameObject nesne;
public void sit()
{
x = PlayerPrefs.GetInt("kaçoldu");
photonView.RPC("KacOldu", RpcTarget.OthersBuffered,x);
KacOldu(x);
if (PhotonNetwork.CurrentRoom.PlayerCount == x)
{
nesne = PhotonNetwork.Instantiate("nesne", Vector3.zero, Quaternion.identity, 0, null);
}
}
public void up()
{
x = PlayerPrefs.GetInt("kaçoldu");
photonView.RPC("KacOldueksi", RpcTarget.OthersBuffered, x);
KacOldueksi(x);
PhotonNetwork.Destroy(nesne);
}
[PunRPC]
private void KacOldu(int x)
{
x = PlayerPrefs.GetInt("kaçoldu");
x ;
Debug.Log("kaç?" x);
PlayerPrefs.SetInt("kaçoldu", x);
}
[PunRPC]
private void KacOldueksi(int x)
{
x = PlayerPrefs.GetInt("kaçoldu");
x--;
Debug.Log("kaç?" x);
PlayerPrefs.SetInt("kaçoldu", x);
}
}
The masterclient in the game cannot destroy the object named "nesne", because the owner of the "nesne" is another player. If I try this: if (PhotonNetwork.CurrentRoom.PlayerCount == x && PhotonNetwork.IsMasterClient) { nesne = PhotonNetwork.Instantiate("nesne", Vector3.zero, Quaternion.identity, 0, null); }
I still can't succeed because if the last sitting player is not the masterclient, the owner of the "nesne" still cannot become the masterclient. I want the masterclient to load the "nesne" when the number of players in the game is equal to x. Can you help me?
CodePudding user response:
To be honest I don't really understand your logic behind those PlayerPrefs
and what x
is supposed to be exactly since every player seems to be able to overrule it completely ...
But for what you ask for I would
- a) simply use
RpcTarget.AllBuffered
instead so that you also get the call yourself as - b) check the amount int the RPC instead
so something like e.g.
public void sit()
{
x = PlayerPrefs.GetInt("kaçoldu");
photonView.RPC(nameof(KacOldu), RpcTarget.AllBuffered, x);
}
public void up()
{
x = PlayerPrefs.GetInt("kaçoldu");
photonView.RPC(nameof(KacOldueksi), RpcTarget.AllBuffered, x);
}
[PunRPC]
private void KacOldu(int x)
{
x = PlayerPrefs.GetInt("kaçoldu");
x ;
Debug.Log("kaç?" x);
PlayerPrefs.SetInt("kaçoldu", x);
// This happens now regardless of whether you or anyone else is the last sitting client
if (PhotonNetwork.IsMasterClient && PhotonNetwork.CurrentRoom.PlayerCount == x)
{
// I would actually use this instead in case the Masterclient role is shifted to someone else
nesne = PhotonNetwork.InstantiateRoomObject("nesne", Vector3.zero, Quaternion.identity, 0, null);
}
}
[PunRPC]
private void KacOldueksi(int x)
{
x = PlayerPrefs.GetInt("kaçoldu");
x--;
Debug.Log("kaç?" x);
PlayerPrefs.SetInt("kaçoldu", x);
// This happens for any standing up client including the masterclient himself
if (PhotonNetwork.IsMasterClient && nesne)
{
PhotonNetwork.Destroy(nesne);
}
}