Jump to content
Sign in to follow this  
Eclipse4349

How to initialize local dynamic variables so they work throughout script?

Recommended Posts

How can I initialize a dynamic variable (created in a for-do loop with Call Compile Format) in the beginning of the script so that I can use only local variables? I need the script to be able to run several instances simultaneously, with no interference from the other instances.

I have written a script that runs via addactions on a helo which only show up if the "pilot" position of the target helo is empty. The helo can be either a Chinook or a Blackhawk. The script is an automated transport script, which lets the user designate up to 5 waypoints, including an LZ.

The script:

  1. Identifies the Helo as a Chinook or Blackhawk and creates the proper number of Crewmen (1 pilot and 2 turret gunners for Blackhawk, 3 for Chinook) after creating a group for them, and then boards them into the appropriate places in the Helo.
  2. opens the map, prompts user to click at each waypoint position, places a uniquely numbered marker at each (WP 1, WP2, etc, ending with last waypoint labeled LZ)
  3. Helo takes off, follows path to LZ, drops units from any groups other than its own crew off, then takes off and follows the same path in reverse order back to the point it started at
  4. Helo lands, crew gets out, moves back to the barracks where they were created, and is deleted, along with their group

The script uses a lot of Call Compile Format lines along with for-do loops, iterating the number of times necessary based on which action the user chose. I have 5 actions attached to each helo to be used as a transport, with a number 1 passed to the script if the user wants a direct flight to LZ, a number 2 passed to the script for 1-waypoint and the LZ, 3 passed to the script for 2-waypoint and the LZ, and so on.

These loops create everything from the crew to the empty Hpad and the markers, and are created with for-do loops and Call Compile Format, such as:

_helonumber = (_this select 3) select 0;
_helocrewnumber = 0;
Call Compile Format ["Transport%1 = createGroup west",_helonumber];

if (_cargopositions < 15) then {
_helocrewnumber = 3;

} else {
_helocrewnumber = 4;
//identifies Helo as either a Chinook (more than 15 cargo spaces) or a Blackhawk

for "_x" from 1 to _heloCrewNumber do {
Call Compile Format ["'US_SOLDIER_PILOT_EP1' createunit [_barracks,Transport%1,'Helo%1Crewman%2 = this']",_helonumber,_x];
};

Some of this is out of order and not EXACTLY as it appears in my script, but it illustrates the point. I currently add the 5 actions to each helo, and, to get around having to initialize a local dynamic variable, I use all global variables for the units, objects, markers, etc, that the script creates. Which means that I must use unique names, so that this script can run on several helo's at once with no interference from each other. Which then means I must pass unique information to each script instance, so I pass the "helonumber" to the script, and use it to name everything. So, every helo I paste these addactions into the init line of, I must then edit so that each helo's addaction passes a difference set of identifiers (_this select 3). I use one item in the "_this select 3" array to pass the total number of waypoints, and the other to pass the number of the helo so that I can uniquely name each global variable.

I would like to simplify the use of this script on multiple helo's, and make it faster and easier to add the actions to each helo that will have the option of automated transport. Using all local variables is the obvious answer, but then I have to initialize the variables before using them or "creating their real value" inside a for-do loop or an if-then statement. However, I do not know the exact name of the variable, as it is based on information that has not been established yet, as in:

HeloCrewman%1 (script would create a HeloCrewman1,HeloCrewman2,HeloCrewman3, and maybe HeloCrewman4 (only if it is a Chinook.) via Call Compile Format in a for-do loop

Likewise, for markers and positioning objects, "HeliHEmpty"

These must be numbered, but I would not know how many there would be, as it is based on the number of waypoints the user selects.

HeloWPMarker%1 (script would create up to 5 waypoints, including the LZ, through Call Compile Format in a for-do loop

How can I initialize a dynamic variable in the beginning of the script so that I can use all local variables? Is there some "placeholder" that the engine allows as a wildcard that would allow the variable to be initialized without knowing all of it's characters? (not knowing the number at the end)

Right now, I am converting to local variables and defining all possible values, one by one, as in:

_HeloWPMarker1 = 0;
_HeloWPMarker2 = 0;
_HeloWPMarker3 = 0;
_HeloWPMarker4 = 0;

This is long and tedious, and is useless if I later write a script that uses much larger possible values!

Sorry the post is so long, but I wanted to provide enough information that I wouldn't have to keep adding to it.

The script is 100% working, and able to be used over and over in the same mission. I would like to rewrite using only local variables, though. Anyone know how, without having to define every possible dynamic variable individually?

Edited by Eclipse4349

Share this post


Link to post
Share on other sites

why not just post the full script as that would be easier to comment on.

Anywhooo... maybe im not getting this at all and im not the right one to answer your question, but...

using text and format commands to create dynamic variables.

_DynamicVariable = text format["_HeloWPMarker%1",_someThingSpecialToSeperateThemAllFromOthersLikeAName];
_DynamicVariable = 0;

Share this post


Link to post
Share on other sites
why not just post the full script as that would be easier to comment on.

Anywhooo... maybe im not getting this at all and im not the right one to answer your question, but...

using text and format commands to create dynamic variables.

_DynamicVariable = text format["_HeloWPMarker%1",_someThingSpecialToSeperateThemAllFromOthersLikeAName];
_DynamicVariable = 0;

So, if I understand you correctly, if I wanted to initialize a soldier created in a for-do loop, it would be something like:

_NumberOfCrewmen = 4
_HeloCrewman = text format["_HeloCrewman%1",_NumberOfCrewman];

for "_x" from 1 to _NumberOfCrewmen do {Call Compile Format ["'US_SOLDIER_PILOT_EP1' createunit [_location,_group,'_HeloCrewman%1 = this'",_x];

How would I then refer to each of the created soldiers? _HeloCreman1, _HeloCrewman2, etc.

Does initializing

_HeloCrewman = text format["_HeloCrewman%1",_NumberOfCrewman];

make each of _HeloCrewman1, _HeloCrewman2, etc initialized for use other than in the scope they were created in (Able to be referenced outside the for-do loop that creates them)? If I understand correctly, now that I think about it, it would... right? Because after

_HeloCrewman = text format["_HeloCrewman%1",_NumberOfCrewman];

, the script sees anything _HeloCrewman(whatever) as having been previously initialized as _HeloCrewman, correct?

So,

_var = text format ["sometext%1",MeaningPlaceholderText];

or

_var = text format ["sometext%1",MustBeTheRealDifferentiatingVariableToBeUsed];

THANKS!!

Apparently there are still other uses of format I hadn't though of or heard of!

Any link to more info on using format, formatted text, etc, you could provide would be appreciated also!!!!

Edited by Eclipse4349

Share this post


Link to post
Share on other sites

One way would be to not use global or dynamic variables at all. And instead of using number of crew positions it might be better to use classnames. Based on what you shown me I would do something like this:

//The chopper
_heli = _this select 0;

//If we don't know the chopper type then just give a pilot
_numCrew = 1;

//Else check if we are a blackhawk or chinook
if (_heli isKindOf "UH60_Base") then {
   _numCrew = 3;
};
if (_heli isKindOf "CH_47F_EP1") then {
   _numCrew = 4;
};
//After you can make check to see if a specific variant of blackhawk or chinook etc. if you want.

//Create crews
_crewGroup = createGroup west;
for "_i" from 1 to _numCrew do {
   _newCrewman = "US_SOLDIER_PILOT_EP1" createUnit [getPos _barracks, _crewGroup];
   //First will be pilot - rest will get in as gunners
   if (_i == 1) then
       _newCrewman assignAsDriver _heli;
   } else {
       _newCrewman assignAsGunner _heli;
   };
};
//Now give them the order to board
(units _crewGroup) orderGetIn true;

//Step 2.
//Step 3.
//Step 4.

EDIT:

As kylania suggest you may be better off using spawnVehicle instead. One single call to spawnVehicle will replace your step #1. You will have to put the chopper on the ground though as I believe it starts in the air then.

Edited by Muzzleflash

Share this post


Link to post
Share on other sites

Doesn't the spawnVehicle function create a vehicle and all proper crew positions already?

Share this post


Link to post
Share on other sites
Doesn't the spawnVehicle function create a vehicle and all proper crew positions already?

I wanted a more lifelike scenario, which is why I have the crew created at a Barracks, traveling to the helo, boarding, then the user gets to pick waypoints and LZ, then boards, gets dropped off, helo goes back in reverse order, lands, crew gets out, goes back to barracks, and is deleted.

Thanks for the replies, guys! I didn't think of "iskindof", so I did it the hard way, lol...

Also, I had trouble getting the first created unit to get in as pilot and the rest to actually get in the turrets, which is why I named them and used

_x action [getinturret [%#]];

to force them into the proper spots. I will try again with assigning and ordergetin.... Thanks for the insights!

Also, was I right about how I took the first responder's reply? If I initialize a variable:

Variable = text format ["Variable%1",SomeOtherMeaninglessThing];

does that mean that from that point on, anything I create using the format

for "x" from 1 to _var do{Call Compile Format ["Variable%1 = blahblah",_x];};

would be initialized already and be usable outside the for-do loop that just created Variable%1?

Thanks again, all!

Share this post


Link to post
Share on other sites

Also, was I right about how I took the first responder's reply? If I initialize a variable:

Variable = text format ["Variable%1",SomeOtherMeaninglessThing];

does that mean that from that point on, anything I create using the format

for "x" from 1 to _var do{Call Compile Format ["Variable%1 = blahblah",_x];};

would be initialized already and be usable outside the for-do loop that just created Variable%1?

Yes that is correct, that is if you can decide on "x" or "_x" in you for and format statements. A variable name that does not start with an underscore is a global variable.

Share this post


Link to post
Share on other sites
Yes that is correct, that is if you can decide on "x" or "_x" in you for and format statements. A variable name that does not start with an underscore is a global variable.

Yeah, I mistyped that, it should have been _x...

And I never knew that "istypeof" returned the actual classname of the object. When I came across that listing in the scripting commands Biki, I thought (from the name) that it would return the class as in, helicopter, man, tank, etc... Makes me wonder what else I don't know about! Thanks guys!

Those are good suggestions for not needing to uniquely name each crewman, but I have objects and markers involved too. The helo needs to follow an exact path the the LZ, and then the same path in reverse order. Otherwise, the caller could choose 3 waypoints and an LZ to get around the back side of a mountain, shielded from fire, just to have the helo shot down on the return flight back to the hpad where it started! Using actual waypoints didn't work well, since the AI doesn't go exactly where intended in a helo. So I create a placeholder "heliHEmpty" at an offmap marker position and use the OnMapSingleClick to setpos it elsewhere, which then creates a marker and ends that iteration of the loop. The addaction passes a number representing the chosen number of waypoints, and so, the total number of loops. I need these "waypoint" objects that the script commands the pilot to "domove" to to ONLY be recognized by the helo running the script instance that created them, as there will be multiple helo's running this script at times. So, initializing a local variable is necessary, unless there is some other way to do it.

I will try using the text format deal. Thanks for the help!

HA! Just figured out why my previous attempts to use ordergetin failed.... I wasn't putting the units into an array... oops...

Edited by Eclipse4349

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  

×