- Home /
Combination of Instance and random in a network game
Hi mates!.
Why in server and client the instances appear with different colors? . What I'm instantiating are a random ballons with different colors. The instant and position and the falling over the ground are well synchronized.
function Update ()
{
if(Network.isServer)
{
var viewID2 = Network.AllocateViewID();
networkView.RPC("SpawnBox",RPCMode.All,viewID2,transform.position);
@RPC
function SpawnBox (viewID2 : NetworkViewID, location : Vector3)
{
// Instantate the prefab locally
var clone : GameObject;
clone = Instantiate(array_of_balloons[Random.Range(0,array_de_globos.Length)], transform.position+Vector3 (Random.Range(0, gridX),0, Random.Range(0, gridY))*spacing , Quaternion.identity);
var nView : NetworkView;
clone.AddComponent(NetworkView);
nView = clone.GetComponent(NetworkView);
nView.viewID = viewID2;
}
}
}
Thank you very much.
Please, help me.
Answer by syclamoth · Nov 30, 2011 at 12:33 PM
What exactly is the problem here? I mean, obviously the boxes won't instantiate in the correct positions because the Random gets called on the clients upon instantiation, but you tell me that's working- what's the issue then? I would do it something like this-
@RPC
function SetColour(float r, float g, float b, float a)
{
var recievedColor : Color = new Color(r, g, b, a);
// from here, do what you like
}
Then, when they get instantiated, after the networkView gets set-
if(networkView.isMine)
{
networkView.RPC("SetColour", RPCMode.All, Random.value, Random.value, Random.value, Random.value);
}
Otherwise, you shouldn't be calling random things on clients if you expect them to act sensibly. You should do the random stuff on the server (the one that's instigating the instantiation), and send it with the position and rotation info.
HEY!. Thank you!!. I think this is the idea. The idea would be to create a random number and choose one of the three elements of my set of prefabs from which I choose the instance via the random command, and then build a function like yours but sending the client the same RANDO$$anonymous$$ INDEX for both: client and server...IT $$anonymous$$UST WOR$$anonymous$$!!. Let me try this and I will tell you!! . IT $$anonymous$$UST WOR$$anonymous$$, IT $$anonymous$$UST WOR$$anonymous$$... ;-) . I'm happy!.
Yes, that would work! It's actually more efficient than sending 3 floats over the network, too.
Answer by jorgon · Dec 02, 2011 at 01:36 PM
I've solved the problem generating a random number which I use as an index to select one of the three elements of my array of ballons. I generate the number out of the " if(Network.isServer)" and then I send the index
if(Network.isServer)
{
networkView.RPC("SpawnBox",RPCMode.All,viewID2,transform.position,index);
and inside of my RPC function:
clone = Instantiate(array_de_globos[num], transform.position+Vector3 (Random.Range(0, gridX),0, Random.Range(0, gridY))*spacing , Quaternion.identity);
The problem I've got now is that I receive two messages continuosly (due to the update loop):
View ID AllocatedID: 6316 not found during lookup. Strange behaviour may occur. Couldn't perform remote Network.Destroy because the network view 'AllocatedID: 6316' could not be located.
The problem now is that after the right instantiation the destruction is made in another script...I don't know if this is related to the error messages which don't disturb for the execution.
Thank you in adavance.
Please, open a new question for this since it's a different problem only tangentally related.