Jump to content
Sign in to follow this  
eagledude4

Using vehicles in an array

Recommended Posts

When I load the mission, only the _Heli is being given the cargo.

_heli = "yup_MH60J";

{
_veh = "_x" createVehicle getMarkerPos "heleSpawn");
_veh = "_x" createVehicle (getMarkerPos "heleSpawn2");
_veh = "_x" createVehicle (getMarkerPos "heleSpawn3");

clearWeaponCargo _veh; 
clearMagazineCargo _veh; 

_veh addWeaponCargo ["ACRE_PRC117F_ID_2",1];
_veh addWeaponCargo ["ACE_Stretcher",1];
_veh addMagazineCargo ["ACE_Bandage",10];
_veh addMagazineCargo ["ACE_Epinephrine",5];
_veh addMagazineCargo ["ACE_Bodybag",5];
_veh addMagazineCargo ["ACE_Morphine",5];
_veh addMagazineCargo ["ACE_Splint",5];
_veh addMagazineCargo ["ACE_Tourniquet",5];

} forEach _Heli, _Heli2, _Heli3;

I've even tried

} forEach [_Heli, _Heli2, _Heli3];

Edited by eagledude4

Share this post


Link to post
Share on other sites

_vehicles = [heli1, heli2];

{
_x addWeaponCargoGlobal ["ACRE_PRC117F_ID_2",1];
} forEach _vehicles;

Give the vehicles a name in the mission editor like heli1 or heli2 whatever, just make sure the names are identical to the names in the _vehicle array. Then call the script. Not tested.

Share this post


Link to post
Share on other sites

I'm going to be spawning the vehicles through a script, so the vehicles don't actually exist in the mission at the start.

Edited by eagledude4

Share this post


Link to post
Share on other sites

You really should learn some scripting basics instead of creating your own scripting language... ;)

A look into my signature may help a little...

Share this post


Link to post
Share on other sites

Believe it or not, I've had my share of digging into the wiki, but the examples aren't quite as to the point as I need them to be.

I don't understand what I should substitute for "_x".

I'm also not sure how I could use the nearestObjects command to return all objects since that command needs a radius to work, unless I could use the map name as a radius.

Edited by eagledude4

Share this post


Link to post
Share on other sites

You don't substitute anything for _x, _x itself is the substitution.

So in this example:

_guys = [guy1, guy2];

{_x setDamage 1} forEach _guys;

The setDamage code would run two times. The first time _x would be replaced with the first value of the array _guys, so:

guy1 setDamage 1

Second time through it would be the second value from _guys so:

guy2 setDamage 1

So the literal _x is what will be replaced in turn by each value of the array you pass forEach. It's a placeholder.

The other confusion you're having is with classnames vs objects. Classnames, the yup_HH60 are always strings, so need to be in quotes like this: "yup_HH60"

Also a classname by itself isn't an object, so you can't do anything to it, it's just words. You'd need to use the classname in a createVehicle command to create an object which you can than use commands on. So something like this:

_heliClassnames = ["yup_HH60", "yup_MH60J"];

{
_veh = createVehicle [_x, position player, [], 0, "NONE"];

clearWeaponCargo _veh; 
clearMagazineCargo _veh; 

_veh addWeaponCargo ["ACRE_PRC117F_ID_2",1];
_veh addWeaponCargo ["ACE_Stretcher",1];
_veh addMagazineCargo ["ACE_Bandage",10];
_veh addMagazineCargo ["ACE_Epinephrine",5];
_veh addMagazineCargo ["ACE_Bodybag",5];
_veh addMagazineCargo ["ACE_Morphine",5];
_veh addMagazineCargo ["ACE_Splint",5];
_veh addMagazineCargo ["ACE_Tourniquet",5];

} forEach _heliClassnames;

That would run the createVehicle and all the addMagazine code twice, once for each classname you fed it. Creating each of those types of helicopters are close to the player as possible and with your assigned cargo. _x would be replaced with the classnames from the array at the top in each passthrough. _veh would be the object of the helicopter created which allows you to add cargo to it.

This isn't really an ideal way of doing it since _veh, the reference to the object isn't really recorded anywhere (it's local, so will be "forgotten" once the script is run) but that might not matter. :)

Does that help understand how _x, forEach and all of that works?

Share this post


Link to post
Share on other sites

Pretty much so. What I still don't get is how _x is automatically replaced with the classnames. What if I had multiple arrays? How would it know which one to choose?

I'd like to create the vehicle on a marker, is this how I need to do it?

_veh = "_x" createVehicle getMarkerPos "heleSpawn");

I used

_heli = "AH1Z" createVehicle (getMarkerPos "hspawn");

as a guide

Also, since this is for a server, should I be using: addWeaponCargoGlobal?

Edited by eagledude4

Share this post


Link to post
Share on other sites

The ArmA2 Scripting Commands page is full of explanations what _x is used for... there is also a search function available... :protest:

Description:

Executes the given command(s) on every item of an array.

The array items are represented by _x. The array indices are represented by _forEachIndex.

In ArmA2 & VBS2, the variable _x is always local to the forEach block so it is safe to nest them.

greetz

Share this post


Link to post
Share on other sites

I forgot that I'm not using one of the helcopters, so I don't need the _x anymore (I don't think).

_Heli = "yup_MH60J" createVehicle (getMarkerPos "heleSpawn");
_Heli2 = "yup_MH60J" createVehicle (getMarkerPos "heleSpawn2");
_Heli3 = "yup_MH60J" createVehicle (getMarkerPos "heleSpawn3");

clearWeaponCargo _Heli; 
clearMagazineCargo _Heli; 

_Heli addWeaponCargo ["ACRE_PRC117F_ID_2",1];
_Heli addWeaponCargo ["ACE_Stretcher",1];
_Heli addMagazineCargo ["ACE_Bandage",10];
_Heli addMagazineCargo ["ACE_Epinephrine",5];
_Heli addMagazineCargo ["ACE_Bodybag",5];
_Heli addMagazineCargo ["ACE_Morphine",5];
_Heli addMagazineCargo ["ACE_Splint",5];
_Heli addMagazineCargo ["ACE_Tourniquet",5];

} forEach _Heli, _Heli2, _Heli3;

Only the _Heli is being given the cargo, so I'm guessing my forEach line is wrong somehow. I've even tried

} forEach [_Heli, _Heli2, _Heli3];

Edited by eagledude4

Share this post


Link to post
Share on other sites

_Heli = "yup_MH60J" createVehicle (getMarkerPos "heleSpawn");
_Heli2 = "yup_MH60J" createVehicle (getMarkerPos "heleSpawn2");
_Heli3 = "yup_MH60J" createVehicle (getMarkerPos "heleSpawn3");

[color="#FF0000"]{[/color]	
clearWeaponCargo _x; 
clearMagazineCargo _x; 

_x addWeaponCargo ["ACRE_PRC117F_ID_2",1];
_x addWeaponCargo ["ACE_Stretcher",1];
_x addMagazineCargo ["ACE_Bandage",10];
_x addMagazineCargo ["ACE_Epinephrine",5];
_x addMagazineCargo ["ACE_Bodybag",5];
_x addMagazineCargo ["ACE_Morphine",5];
_x addMagazineCargo ["ACE_Splint",5];
_x addMagazineCargo ["ACE_Tourniquet",5];

} forEach [color="#FF0000"][[/color]_Heli, _Heli2, _Heli3[color="#FF0000"]][/color];

Marked in red the parts you were missing. Also all the _Heli inside the forEach was replaced with _x to it'll happen for each of your helis.

Anything inside a [ ] is an array.

Share this post


Link to post
Share on other sites

Having issues with a similar script:

_CG = [CG, CG2, CG3, CG4, CG5];

{
removeAllWeapons _x;
removeAllMagazines _x;

_x addMagazine ["30Rnd_556x45_Stanag",6]; 
_x addMagazine ["PipeBomb",2]; 
_x addMagazine ["SmokeShell",2]; 
_x addMagazine ["IR_Strobe_Target",2]; 
_x addMagazine ["ACE_SSWhite_FG",2]; 

_x addWeapon ["RH_acrbaim",1]; 
_x addWeapon ["ACE_Flaregun",1];
_x addWeapon ["NVGoggles",1]; 
_x addWeapon ["Binocular",1];
_x addWeapon ["ItemMap",1];
_x addWeapon ["ItemCompass",1];
_x addWeapon ["ItemWatch",1];
_x addWeapon ["ItemRadio",1];
_x addWeapon ["ACRE_PRC148_UHF_ID_263",1];
_x addWeapon ["ACE_Earplugs",1];
_x addWeapon ["ItemGPS",1];
} forEach [_CG];

Edited by eagledude4

Share this post


Link to post
Share on other sites

Two things.

} forEach _CG;

_CG is already an array, so forEach _CG is fine, it would be the same as forEach [CG, CG2, CG3, CG4, CG5].

Second thing I actually just re-discovered last night. You'd expect this to add 6 magazines, since that's how addMagazineCargo works:

_x addMagazine ["30Rnd_556x45_Stanag", 6];

However the new addMagazine array feature would add just 1 magazine with 6 rounds in it now. So to add multiple mags you'd wanna do:

for "_i" from 1 to 6 do {_x addMagazine "30Rnd_556x45_Stanag"};

Share this post


Link to post
Share on other sites

What do you mean by "_CG is already an array". Are you saying I'm redefining it? You also said "forEach _CG is fine", but that's what I have (except I have brackets), and it isn't working. I'll PM you so we don't spam this thread.

Edited by eagledude4

Share this post


Link to post
Share on other sites
Two things.

} forEach _CG;

_CG is already an array, so forEach _CG is fine, it would be the same as forEach [CG, CG2, CG3, CG4, CG5].

Second thing I actually just re-discovered last night. You'd expect this to add 6 magazines, since that's how addMagazineCargo works:

_x addMagazine ["30Rnd_556x45_Stanag", 6];

However the new addMagazine array feature would add just 1 magazine with 6 rounds in it now. So to add multiple mags you'd wanna do:

for "_i" from 1 to 6 do {_x addMagazine "30Rnd_556x45_Stanag"};

Noticed this aswell :) Was confused at first.

Share this post


Link to post
Share on other sites
You also said "forEach _CG is fine", but that's what I have (except I have brackets), and it isn't working.

So that's not what you have. Do you realize we're talking about scripting here, and we don't just put brackets here and there to make it look pretty, but because they serve a purpose?

Share this post


Link to post
Share on other sites

Kylania initially told me to use brackets there, so I was confused when he didn't use them the second time.

Also, being sarcastic isn't going to help anyone.

Edited by eagledude4

Share this post


Link to post
Share on other sites

In scripting when something is put into [] brackets it becomes an array. In your example you already created an array by doing

_CG = [CG, CG2, CG3, CG4, CG5];

therefore, _CG is an array. So you don't need to put it into brackets unless you're making a complicated 2d array, so forEach _CG is correct.

Share this post


Link to post
Share on other sites

eagledude, I think you're taking mamb's joke a little too harsh. But just as a clarifier, anything inside of brackets {} is a function. So basically what you're telling arma is {do this} forEach this. Which I'm sure you already know, just helping clarify. :)

Oh, another clarification. Whenever you're using a formated variables (such as _x in this case). You can't use it inside a string. If you use "_x" the literal string is "_x". But say the entry is like "M4A1" and you use _x. _x would equal "M4A1".

Here's an example

a = 1;
hintSilent "a";

^ Would hint "a".

However,

a = 1;
hintSilent (str a);

^ Would hint "1".

str is telling arma to compile the value into a string.

;)

Edited by Horner

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  

×