The Photon Forum
is Closed Permanently.

After many dedicated years of service, we have made the decision to retire our Forum and switch to read-only: we´ve saved the best to last! Your search result can be found below. Plus, we offer support via these channels:

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).

Write Us
an E-Mail

Feel free to send your question directly to our developers.

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

S_Oliver
2018-08-18 10:19:44

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.

abidal
2018-08-18 13:48:38

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.

sadiq
2020-11-25 11:07:03

@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

S_Oliver
2021-01-01 19:51:55

You could also just use the same seed on all clients, then all clients should generate the same random numbers.

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

Back to top