Photon Network RPC Random Range Problem
The whole answer can be found below.
Try Our
Documentation
Please check if you can find an answer in our extensive documentation on PUN.
Join Us
on Discord
Meet and talk to our staff and the entire Photon-Community via Discord.
Read More on
Stack Overflow
Find more information on Stack Overflow (for Circle members only).
Photon Network RPC Random Range Problem
abidal
2018-08-17 18:59:49
Photon Network RPC Random Range Problem
Hi, I'm working on a project. I'm using this script for bot money;
using UnityEngine;
using System.Collections;
public class Botmoney : MonoBehaviour {
public int BotMoney;
public bool botStealable = true;
PhotonView photonView;
// Use this for initialization
void Start () {
photonView = GetComponent ();
photonView.RPC ("RandomMoneyFBot", PhotonTargets.All);
}
[RPC]
void RandomMoneyFBot(){
BotMoney = Random.Range (12,60);
}
}
But every client gets diffrent numbers. How I can solve this?
Also;
When I want RPC to a string with PhotonNetwork.playerName every player gets their own player name. I want to just write RPC sender's name to string. How can I do this?
Comments
The RPC is sended to all Clients and the Content of the RPC is executed on all Clients, this means the Random.Range result is on all Clients a different.
A better approach is to generate a Random Number on one Client and send this Number to all other clients.
Same Answer for the other Topics you opened.
I dont get the second Question but, it is the same problem and same approach to solve ur problem.
thanks s_oliver but how to generate random one one client and how to send it help please
Samuelroos
2018-08-18 15:21:03
I changed your code to represent what you wanted if I understood right:
using UnityEngine;
using System.Collections;
public class Botmoney : MonoBehaviour {
public int BotMoney;
public bool botStealable = true;
PhotonView photonView;
private void Start ()
{
photonView = GetComponent();
int randomMoney = Random.Range (12, 60);
photonView.RPC ("ReceiveRandomMoneyFromBot", PhotonTargets.All, randomMoney);
}
[RPC]
private void ReceiveRandomMoneyFromBot(int moneyAmount)
{
BotMoney = moneyAmount;
}
}
So basically, what happens now is the random money amount is created before we send the RPC to all clients and we send the random amount as a parameter to all players, which means that the number for everybody will be equal to the amount that was created by the client who sent the RPC.
@Samuelroos wrote: »
I changed your code to represent what you wanted if I understood right:
using UnityEngine;
using System.Collections;public class Botmoney : MonoBehaviour { public int BotMoney; public bool botStealable = true; PhotonView photonView; private void Start () { photonView = GetComponent(); int randomMoney = Random.Range (12, 60); photonView.RPC ("ReceiveRandomMoneyFromBot", PhotonTargets.All, randomMoney); } [RPC] private void ReceiveRandomMoneyFromBot(int moneyAmount) { BotMoney = moneyAmount; } } So basically, what happens now is the random money amount is created before we send the RPC to all clients and we send the random amount as a parameter to all players, which means that the number for everybody will be equal to the amount that was created by the client who sent the RPC.
I am getting different numbers on each clients. How can i solve this
suhailhassan
2020-11-25 12:12:51
@Samuelroos wrote: »
I changed your code to represent what you wanted if I understood right:
using UnityEngine;
using System.Collections;public class Botmoney : MonoBehaviour { public int BotMoney; public bool botStealable = true; PhotonView photonView; private void Start () { photonView = GetComponent(); int randomMoney = Random.Range (12, 60); photonView.RPC ("ReceiveRandomMoneyFromBot", PhotonTargets.All, randomMoney); } [RPC] private void ReceiveRandomMoneyFromBot(int moneyAmount) { BotMoney = moneyAmount; } } So basically, what happens now is the random money amount is created before we send the RPC to all clients and we send the random amount as a parameter to all players, which means that the number for everybody will be equal to the amount that was created by the client who sent the RPC.
bujt wouldnt this code (int randomMoney= ...) get executed on ALL clients because it's in a start method ? where do we specify that the code should ONLY be run on 1 client ? and then the result sent to all .... ?
should we maybe use the List of Players and make the random generation happen only in Player[0] for example, or a way to choose the Master Client ?
Gamerninja
2020-12-24 18:37:25
Samuelroos Thanks it Works
You could also just use the same seed on all clients, then all clients should generate the same random numbers.
var seed = 12532434; //some custom made seed that must be the same on all clients, use what ever is available for all clients and is the same
var rnd = new Random(seed);
var rndValue = rnd.Next(0, 10);
Now if all clients call rnd.Next(0,10) the first time it generates 4, next time it generates a 2, then a 6,9...
of course all clients have to call the rnd.Next the same amount