- Home /
Photon PUN2, Rpc score can't correctly.
Hi all , I have two ways now. But they can't send correct. I'm very confuse.
I want MasterClient send score to the other client ,and the other client send score to MasterClient.
Way1: It looks have no wrong, but it can't work
[PunRPC]
void MasterCount()
{
masterCount = gameCon.playerScore;
}
[PunRPC]
void OtherCount()
{
otherCount = gameCon.playerScore;
}
void Update()
{
if(PhotonNetwork.IsMasterClient)
{
if (gameObject.GetComponent<PhotonView>().IsMine)
{
if (gameCon.isClickButton)
{
masterCount = gameCon.playerScore;
scoreText.text = Mathf.Floor(masterCount).ToString("0");
gameObject.GetComponent<PhotonView>().RPC("MasterCount", RpcTarget.Others);
}
}
}
else
{
if (gameObject.GetComponent<PhotonView>().IsMine)
{
if (gameCon.isClickButton)
{
otherCount = gameCon.playerScore;
scoreText.text = Mathf.Floor(otherCount).ToString("0");
gameObject.GetComponent<PhotonView>().RPC("OtherCount", RpcTarget.Others);
}
}
}
}
Way2: I change the RPC , it looks very weird . The other can send othercount to Master , and Master still can't send .
[PunRPC]
void MasterCount()
{
masterCount = gameCon.playerScore;
}
[PunRPC]
void OtherCount()
{
otherCount = gameCon.playerScore;
}
void Update()
{
if(PhotonNetwork.IsMasterClient)
{
if (gameObject.GetComponent<PhotonView>().IsMine)
{
if (gameCon.isClickButton)
{
masterCount = gameCon.playerScore;
scoreText.text = Mathf.Floor(masterCount).ToString("0");
gameObject.GetComponent<PhotonView>().RPC("OtherCount", RpcTarget.Others);
}
}
}
else
{
if (gameObject.GetComponent<PhotonView>().IsMine)
{
if (gameCon.isClickButton)
{
otherCount = gameCon.playerScore;
scoreText.text = Mathf.Floor(otherCount).ToString("0");
gameObject.GetComponent<PhotonView>().RPC("MasterCount", RpcTarget.Others);
}
}
}
}
I don't know where is the problem , help me ,thank you !
Answer by Captain_Pineapple · Nov 12, 2019 at 08:56 AM
Hey there,
you really have to learn to understand the documentation of photon... This is a really basic mistake you do there...
A RPC like any other function needs arguments you actually pass. Your current functions never transfer any data.
Let me put it like this: you have this function:
[PunRPC]
void OtherCount()
{
otherCount = gameCon.playerScore;
}
then when you call
photonView.RPC("OtherCount", RPCTarget.Others);
at player A; then there will only be a message outgoing from player A to call this function at all other players. Which means that player B will call OtherCount
with values that are set in his own instance.
To actually send Data you need arguments:
[PunRPC]
void OtherCount(int count)
{
//do something with the value "count" here
}
where you then can call
photonView.RPC("OtherCount", RPCTarget.Others, valueYouWantToTransferToOtherPlayers);
Please read the documentation!
As like you say , after I post the question , I found the problem. I really read the documentation of photon many times , because my bad English ,so there have something I can't understand. In my country, the information of photon , is not to clear .
Thank you for your reply :)