- Home /
The question is answered, right answer was accepted
Var change between 2 RPC calls / Coroutine problem
Hello everyone !
I encountered a network problem :
I have 2 RPC calls following each other, with a yield between them. One of their attributes is a public boolean variable contained in the same script. In some cases, this boolean changes while the yield is happening. My problem is that the second RPC call does not take in account this change ...
Here is the code to be more clear :
var minigunFireAborted:boolean = false;
function Update() {
if (Input.GetButton("Fire1") && (currentWeapon == Weapon.Minigun || currentWeapon==Weapon.LanceFlammes) )
{
RafaleFire();
}
if (Input.GetButtonUp("Fire1"))
{
minigunFireAborted = true;
networkView.RPC("NetworkAnimateMinigun", RPCMode.All, 3, minigunFireAborted);
networkView.RPC("NetworkAnimateMinigun", RPCMode.All, 4, minigunFireAborted);
}
}
function RafaleFire()
{
networkView.RPC("NetworkAnimateMinigun", RPCMode.All, 1, minigunFireAborted);
yield WaitForSeconds(1.0f);
networkView.RPC("NetworkAnimateMinigun", RPCMode.All, 2, minigunFireAborted);
}
@RPC
function NetworkAnimateMinigun (anim:int, aborted:boolean) {
var audios:Component[]= instanceMinigun.GetComponentsInChildren(AudioSource);
switch (anim) {
case animationMinigun.MinigunIdle:
instanceMinigun.animation.PlayQueued("idle");
break;
case animationMinigun.MinigunStart:
instanceMinigun.animation.Play("start");
MakeNoiseFromMinigun("rotation_minigun_start", audios, true);
break;
case animationMinigun.MinigunShoot:
instanceMinigun.animation.PlayQueued("shoot");
if (!aborted)
{ // HERE IS THE PROBLEM, ABORTED IS ALWAYS FALSE EVEN IF THE PLAYER RELEASED FIRE1
MakeNoiseFromMinigun("rotation_minigun_shoot", audios, true);
MakeNoiseFromMinigun("shoot_minigun", audios, true);
}
break;
case animationMinigun.MinigunStop:
instanceMinigun.animation.Play("stop");
MakeNoiseFromMinigun("rotation_minigun_shoot", audios, false);
MakeNoiseFromMinigun("rotation_minigun_stop", audios, true);
break;
case animationMinigun.MinigunWithoutAmmo:
MakeNoiseFromMinigun("shoot_minigun", audios, false);
break;
}
}
Thanks by advance for the help !
EDIT : this appears to be a problem with the coroutine RafaleFire(). If someone has an idea ... It seems that since RafaleFire() is a coroutine, then changes that appear in the 1 secon lap time in the Update() method are not taken in account. The coroutine continues with the "previous" state of variables when it was firstly launched ...
Damn ! I was sure of the opposite, a boolean var is so basic... Thanks, it works perfectly, if you want to change the comment as an answer to be validated ...
I know... I usually sent an int as 0/1 for false/true ins$$anonymous$$d. It's a bit annoying, but nothing we can do about it :)
Answer by asafsitner · Dec 23, 2012 at 11:24 PM
Seems to me the problem is with your RPC declaration; RPCs cannot have a boolean parameter!
See here for more details.
Follow this Question
Related Questions
www class blocks script execution 1 Answer
Figuring out Coroutines 3 Answers
How to start at a specific coroutine? 0 Answers
Trouble Resuming after Yielding while Inside Coroutine 1 Answer
WaitForSeconds Question 2 Answers