Jump to content
Sign in to follow this  
Jigsor

How to select last element from array and add it to another array.

Recommended Posts

How do you select last element from array and add it to another array.

In this case a position element.

In this next working script section, each time a town is activated it adds the town position to an array named jig_ammo_cache.

//server_fncs.sqf
jig_ammo_cache = [];
_condition = true;
while (_condition) do
{
if (_city getVariable "BTC_city_act" == 1) then
{				
	if (!(_city_pos in jig_ammo_cache)) then {jig_ammo_cache = jig_ammo_cache + [_city_pos];};
};
sleep 20;
};

Is something is wrong in the way I refference the last element in the next section?

// init_ammocache.sqf
if (!isServer) exitWith{};
_current_cache_pos = [];
jigxcoor = nil;
jigycoor = nil;
jigzcoor = nil;
_arrayLength = count jig_ammo_cache;

waitUntil {sleep 3; count jig_ammo_cache > 0};
while {count jig_ammo_cache > 0} do
{
if (!(count _current_cache_pos == 0)) then {_current_cache_pos set [];};	
_current_cache_pos = jig_ammo_cache select _arrayLength;	
jigxcoor = _current_cache_pos select 0;
jigycoor = _current_cache_pos select 1;
jigzcoor = _current_cache_pos select 2;
null0 = [[jigxcoor,jigycoor,jigzcoor],[300,300,0]]; execvm "scripts\ammo_placement.sqf";
sleep 20;
};

It errors out after this next script runs.

//ammo_placement.sqf
_pos_ammo = _this select 0;//position
_radarray = _this select 1;//radius array around position and Direction [300,300,53]
_veh = createVehicle ["Box_IND_Ammo_F", _pos_ammo, [], 0, "NONE"];

show errors returns:

_position_mark = |#|_this select 0;
undefined variable in _this
undefined variable in _current_cache_pos

I realized after posting that this is not actually trying to add element to another array , but instead taking last element and assigning a variable to it in order to pass it to another script. Still the problem exists. Does anyone have a solution?

Edited by Jigsor

Share this post


Link to post
Share on other sites

carn't speak for the rest of the script but this

null0 = [[jigxcoor,jigycoor,jigzcoor],[300,300,0]]; execvm "scripts\ammo_placement.sqf";

should be this

null0 = [[jigxcoor,jigycoor,jigzcoor],[300,300,0]] execvm "scripts\ammo_placement.sqf";

other wise it's two separate commands

Share this post


Link to post
Share on other sites

Thanks F2k Sel, That is just a typo from copy/pasting the relevant bits of these scripts. The original doesn't have that mistake. Anyone have a clue if I am doing this wrong or maybe something else is wrong? Is jig_ammo_cache select _arrayLength; not a feasable solution?

Share this post


Link to post
Share on other sites

_position_mark isn't in ammo_placement.sqf snippet you have posted so maybe you're looking at the wrong file.

To answer your heading question...

_array1 = _array1 + [(_array2 select (count _array2))]

Not tested, I'm on my phone

Share this post


Link to post
Share on other sites

It's kind of hard to read as we don't know what some of the variables are.

in //server_fncs.sqf

There are incorrect brackets around the while should be {}

but more importantly _city_pos is not defined so adding nothing to the array

also what format is it in [x,y,z] or is it an object

Even it it was defined it seems to be making an endless array adding itself every 20 seconds as long as (_city getVariable "BTC_city_act" == 1)

you will need to remove 1 from arrayLength.

array [1,2,3] would return 3 when counted

to select the last element you use select 2 as select starts from zero not one.

// init_ammocache.sqf

_current_cache_pos = jig_ammo_cache select (count jig_ammo_cache)-1;

the next bit depends on the format mentioned above

if they are coords array [x,y,z] then your right it should be

jigxcoor = _current_cache_pos select 0;

jigycoor = _current_cache_pos select 1;

jigzcoor = _current_cache_pos select 2;

But if it's an object then it needs to be

jigxcoor = getpos _current_cache_pos select 0;

jigycoor = getpos _current_cache_pos select 1;

jigzcoor = getpos _current_cache_pos select 2;

Are you trying to remove the last element from the array?

Also don't see the point of extracting the coords, either use them directly or if it's an object put getpos in front of it.

The only reason to break it down would be to add some other value to it.

If you are trying to remove the last element then it would be

//if (!(count _current_cache_pos == 0)) then {current_cache_pos [set []};

_current_cache_pos = jig_ammo_cache select (count jig_ammo_cache)-1;

jig_ammo_cache = jig_ammo_cache - [_current_cache_pos];

Edited by F2k Sel

Share this post


Link to post
Share on other sites

Sorry, I left out some variable values. I've got it work out now. I've rewriten and combined init_ammocache.sqf and ammo_placement.sqf instead of passing the Vars. Thanks for the insight F2k Sel. Squeeze, that bit of code could be handy for me later . I will test and try that. Thanks again guys.

Share this post


Link to post
Share on other sites

only works this way if the array has elements in it, so not to error you'll have to check ... if(count _array2 > 0)then

_array1 = _array1 + [(_array2 select (count _array2 - 1))];

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  

×