Jump to content
Sign in to follow this  
BEAKSBY

Array problem ... help please

Recommended Posts

Hi All,

Need help with array problem. I'm trying to reduce the value of the first element in by 10% for each of the nested arrays, for both arrays below.

_BLUveh = [[1200, "B_MBT_01_TUSK_F"],[900, "B_APC_Tracked_01_rcws_F"],[750, "B_APC_Tracked_01_AA_F"],[800, "I_APC_Wheeled_03_cannon_F"],[600,"B_APC_Wheeled_01_cannon_F"],[300,"B_MRAP_01_gmg_F"],[150,"B_MRAP_01_hmg_F"],[100,"B_G_Offroad_01_armed_F"]];
_BLUamo = [[200,"B_static_AT_F"],[200, "B_static_AA_F"],[150,"B_soldier_LAT_F"],[100,"B_G_Soldier_LAT_F"],[150,"B_GMG_01_high_F"],[100,"B_HMG_01_high_F"]];

//for Missile Specialist reduce asset costs by 10%
if (player hasWeapon "launch_B_Titan_short_f" or player hasWeapon "launch_O_Titan_short_f") then {
{
_x = {_x set [_x, _x (select 0) * 0.9]} forEach _x;
} forEach _BLUveh + _BLUamo;
};

So the result should be:

_BLUveh = [[1080, "B_MBT_01_TUSK_F"],[810, "B_APC_Tracked_01_rcws_F"],[675, "B_APC_Tracked_01_AA_F"],...

_BLUamo = [[180,"B_static_AT_F"],[180, "B_static_AA_F"],[135,"B_soldier_LAT_F"],...

Share this post


Link to post
Share on other sites

if (player hasWeapon "launch_B_Titan_short_f" or player hasWeapon "launch_O_Titan_short_f") then { 
   {_x set [0, (_x select 0) * 0.9]}  forEach _BLUveh + _BLUamo; 
};

Does that not do the trick?

Share this post


Link to post
Share on other sites
if (player hasWeapon "launch_B_Titan_short_f" or player hasWeapon "launch_O_Titan_short_f") then { 
   {_x set [0, (_x select 0) * 0.9]}  forEach _BLUveh + _BLUamo; 
};

Does that not do the trick?

Thanks, but still not working.

Share this post


Link to post
Share on other sites

The assignment _x = ... in a forEach loop does not make that element reference/hold the new value. You must use the set command to explicitly assign to an index:

_BLUveh = [[1200, "B_MBT_01_TUSK_F"],[900, "B_APC_Tracked_01_rcws_F"],[750, "B_APC_Tracked_01_AA_F"],[800, "I_APC_Wheeled_03_cannon_F"],[600,"B_APC_Wheeled_01_cannon_F"],[300,"B_MRAP_01_gmg_F"],[150,"B_MRAP_01_hmg_F"],[100,"B_G_Offroad_01_armed_F"]];
_BLUamo = [[200,"B_static_AT_F"],[200, "B_static_AA_F"],[150,"B_soldier_LAT_F"],[100,"B_G_Soldier_LAT_F"],[150,"B_GMG_01_high_F"],[100,"B_HMG_01_high_F"]];

//for Missile Specialist reduce asset costs by 10%
if ((toLower secondaryWeapon player) in ["launch_b_titan_short_f", "launch_o_titan_short_f"]) then {
   {
       _x set [0, (_x select 0) * 0.9];
   } forEach (_BLUveh + _BLUamo);
};

You also don't need nested loops, each _x in the forEach loop is '[#, ""]', so _x[0] == #. I also changed the if statement to make it easier if there are many things to check.

Share this post


Link to post
Share on other sites

OK, thanks.

SO the "0" is in _x set [0, (_x select 0) * 0.9]; is the first element in each sub-array and not the first element in the array _BLUveh or _BLUamo.

I think I got it?

Share this post


Link to post
Share on other sites

Almost, but you've got it. "_x" is the current sub-array in the forEach loop. "_x select 0" is the first element in the sub array, so "0" is the index of the first element in the sub-array. "1" is the second, "2" the third and so on...

Btw, in a forEach loop, the magic variable "_forEachIndex" is the index of "_x". So say you had and array ["a", "b", "c", "d", "e"] and you wanted to replace every element by something else, you'd do it that way

_myArray = ["a", "b", "c", "d", "e"];
{
   _myArray set [_forEachIndex, format ["%1%2", "var_", _x]];
} forEach _myArray;

The result will be ["var_a", "var_b", "var_c", "var_d", "var_e"].

Edited by waltenberg

Share this post


Link to post
Share on other sites

Out of interest would it be more efficient to have had two separate Arrays.

One containing the cost and the other the item or one array without sub arrays and just use a stepped index or is the original method the best way to do it.

Share this post


Link to post
Share on other sites

I personally don't see a difference whether you iterate through one array and manipulate a specific value in a sub array - or you iterate through an array and manipulate the corresponding value in another one telling from the index.

Probably, the "select 0" might be an additional action to be taken compared with the two array solution, but I'm not yet very experienced with code optimisation as I don't know the quirks or SQF. Probably, you just have to test it using BIS_fnc_codePerformance.

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  

×