- Home /
RPC with Random Numbers!
Here is my code using UnityEngine; using System.Collections;
public class StarCreate : Photon.MonoBehaviour {
public GameObject prefab;
public int StarCount;
// Use this for initialization
void Start () {
GetComponent<PhotonView> ().RPC (
"UniCreate",
PhotonTargets.All);
}
[RPC]
void UniCreate (){
StarCount = Random.Range (100, 200);
int i = 0;
while (i < StarCount) {
Instantiate(prefab, new Vector3 (Random.Range (-10000,100000), Random.Range (-10000,100000),Random.Range (-100000,100000)), Quaternion.identity);
i++;
}
}
so I was wondering how you sync Random Numbers over the network though rpc! The problem is that every client gets the prefab instantiated in a different place! I want the Prefab to be instantiated in a random area but the same for every client though! That's all thanks for the help (BTW I did search the web for solutions to this but could not find any similar to my problem, so I I'm not that lazy XD).
Answer by Owen-Reynolds · Jul 19, 2014 at 08:21 PM
Roll the dice in Start, before you make the RPC, and send the Vector3 result. In other words, one person is responsible for the randomness. Everyone else just gets an RPC "make a new thing at this position."
How can I send the actual random number variable to all of the other players?
ziggle: Thinking of a "random number variable" isn't really helpful here.
Once you roll a random number, and put it in a variable, it's just a regular variable with a regular value. You can send it like anything else.
Now, if you're thinking of a way to send a "dice" variable, which will automatically roll the same numbers as the first "dice" variable, that's possible using seeds (look up random number seeds.) But it's more complicated and I don't think as reliable (they can get out-of-synch if you aren't careful.)
Sorry, what I mean is I would like player 1 to make a random int, and then send this as an int variable to all other players, who can then use that variable. Just not sure what the code would like like for this. Thanks
Answer by Runalotski · Dec 06, 2015 at 05:34 PM
You would add an argument to the method that you pass a value to
public class StarCreate : Photon.MonoBehaviour {
public GameObject prefab;
public int StarCount;
// Use this for initialization
void Start () {
//I create a random Number
float rnd = Random.Range(0.0f, 100.0f);
//Rpc has thee parts to the method (name of RPC, Targets, Arguments[])
GetComponent<PhotonView> ().RPC (
"UniCreate",
PhotonTargets.All,
rnd); //now the rnd value is sent to the rpc
}
[RPC]
//Here I make an argument called rnd of type float
void UniCreate (float rnd){
//rnd will now be the same for all clients
StarCount = Random.Range (100, 200);
int i = 0;
while (i < StarCount) {
Instantiate(prefab, new Vector3 (Random.Range (-10000,100000), Random.Range (-10000,100000),Random.Range (-100000,100000)), Quaternion.identity);
i++;
}
}
?? UniCreate never uses rnd. Yours works the same as original version.
Also, please check dates -- this is over a year old. It was brought up by ziggl's comment. There are tabs to select New questions, or Active questions. You have to be careful when you're in the Active tab.
I didn't check how old it was. and yes uniCreate does not use rnd, i did not know the specific location it was needed so left that for the user to implement, it was to show to to pass a value to an rpc, which i felt answers the question.
Your answer
Follow this Question
Related Questions
Spawning In Photon C# 1 Answer
Photon/Network - Killing A Client Enemy Unit 3 Answers
Changing the parent of a transform via RPC 0 Answers
Synchronize array with simultaneous access via Photon Network RPC 1 Answer
PhotonView with ID has no method marked with the [PunRPC](C#) property! BUT IT HAS ??? 1 Answer