Jump to content
Sign in to follow this  
thirith

Variables in scripts using SLEEP

Recommended Posts

I've run into problems with scripts running in parallel that use SLEEP, and I'm wondering whether it's due to the way scripts use variables.

 

In particular, I'd scripted a function that's called with one parameter - something along these lines:

variable = this select 0;
[] spawn {
     sleep 10;
     do something with variable;
     }

My question is this: is the <do something with variable> set at the moment the spawn command is run, and it doesn't matter if the variable is changed afterwards? Or does Arma basically wait those ten seconds and only then check the variable and act accordingly, i.e. if the variable were to change in the meantime it'll use the changed value? The latter might explain the problems I've had, with only the first part of the script firing but not the one after the sleep command.

Share this post


Link to post
Share on other sites

I've run into problems with scripts running in parallel that use SLEEP, and I'm wondering whether it's due to the way scripts use variables.

 

In particular, I'd scripted a function that's called with one parameter - something along these lines:

variable = this select 0;
[] spawn {
     sleep 10;
     do something with variable;
     }

i think Spawn code isn't needed there. So this code...

_variable = _this select 0;  // (Select 0) position from the last script or PLAYER used.
sleep 10;
//do something with _variable;

Share this post


Link to post
Share on other sites

My question is this: is the <do something with variable> set at the moment the command is run, and it doesn't matter if the variable is changed afterwards?

Hi thirith, it's important to figure out the scope of the variables you use - simply put, global variables are accessible to all scripts at all times. This reading might clarify the matter: Killzone_Kid: Variables, Community Wiki: Variables and Script (File).

 

Nikander

Share this post


Link to post
Share on other sites

Thanks. I've been looking at those explanations, but I'm not sure they're the key to my specific problem here: I've had the same issue with the script working some times but not others regardless of whether I've used variable or _variable. The reason might be elsewhere, of course - and, to be honest, I don't really know if each time I run the function it basically counts as its own script.

 

I'm not at home right now, so I can't paste the actual function, but basically it works like this: when a helicopter enters a trigger area, it calls the function with two parameters, namely the helicopter in question (helired, heliblue, heliyellow, heligreen) and the passenger in question (red_a, red_b, red_c, red_d, blue_a etc.). The function creates a smoke grenade at the position of the passenger, and ten seconds later the passenger is told to board the helicopter. (Without the ten-second wait, the smoke grenade becomes pointless, as the passenger immediately starts running towards where the helicopter is.)

 

The smoke grenade is always created as needed, but the passenger doesn't always run to board the helicopter - and I'm thinking that the reason is this: there are four helicopters (red, blue, yellow, green) and four sets of passengers. Let's say helired triggers the function, which results in red_a dropping that smoke grenade, but during the ten-second wait heliblue also triggers the function, which changes the parameters from helired and red_a to heliblue and blue_a. Passenger red_a is never told to board helired.

 

As I've mentioned, this happened whether I used _variable or variable. I've now changed the function in that I created four different functions (for each colour) with their own variables, though I'll still have to test this. In any case, I only have a hunch concerning what happened and why the function didn't always complete, but just elaborating on it does make it clearer to me. 

Share this post


Link to post
Share on other sites

Nah, the mission itself is pretty simple and shaping up to be pretty good fun: four teams (pilot, co-pilot) have to pick up four passengers each and return to base. Whoever does so first wins.

 

There are a couple of wrinkles, mostly to do with issues of locality and/or the possibility that some units may be deactivated, i.e. they don't exist and therefore return nil when I least expect it, but generally it's nothing fancy.  :D

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×