Home   Help Search Login Register  

Author Topic: Deleting array entry  (Read 1299 times)

0 Members and 1 Guest are viewing this topic.

Offline myke13021

  • Contributing Member
  • **
  • Myke
Deleting array entry
« on: 02 Apr 2008, 01:33:06 »
Ok, this one is bugging me. First, i'll post the respective code:
Code: [Select]
while {(! _return) and (! _squad_dest)} do
{
sleep 0.5;
{
_ammocount = 0;
_dummy_veh = _x select 0;
_dummy_mag = _x select 1;
{
_ammocount = _ammocount + (_dummy_veh ammo _x);
}
foreach _dummy_mag;
if (_reference == 0) then
{
_reference = _ammocount;
};
if ((fuel _dummy_veh) <= 0.4) exitwith {_return = true};
if (_ammocount <= (_reference / 5)) exitwith {_return = true};
if (((_dummy_veh emptypositions "driver") == 1) or (! (alive driver _dummy_veh))) then
{
hint "booh";
_squad_list = _squad_list - [_x];
};
//hint format ["%1", _squad_list];
if ((count _squad_list) == 0) exitwith {_squad_dest = true};
if ((damage _dummy_veh) >= 0.6) exitwith {_return = true};
}
foreach _squad_list;
};

The code is meant to track the status of one or more planes to let them (later in script) return to base. The part thats bugging me is, that if all planes are downed, the script should stop (also done a few lines later).
For completeness: the _squad_list array looks like this (while testing):
Code: [Select]
[[WEST 1-1-B, ["BombLauncher]]]
For each plane an additional subarray is added so i can track all planes individually (primarly the weapons).

So far everything works fine except this line:
Code: [Select]
_squad_list = _squad_list - [_x];
I want to remove the entire array from a downed plane to track the remaining planes and abort if all are down. So the following count of the array would let me abort if it reaches 0.

I know that the line
Code: [Select]
if (((_dummy_veh emptypositions "driver") == 1) or (! (alive driver _dummy_veh))) then
does work as expected since the hint "booh" is shown. But a hint format ["%1", count _squad_list] still shows 1.

Also tried with
Code: [Select]
_squad_list = _squad_list - [[_x]];
but still without success.


If anyone may lighten me up how i can get rid of this array out of the other array....well, would help me a lot.

Myke out

Offline Mandoble

  • Former Staff
  • ****
    • Grunt ONE and MandoMissile suite
Re: Deleting array entry
« Reply #1 on: 02 Apr 2008, 01:49:14 »
From an array you may "substract" individual (or discrete) items, not individual items that are arrays. And it seems that each _x is in fact an array of two or more elements.

You may use an index to keep track of each item, for example, _i = 0 before the forEach block, and _i = _i + 1 at the last line of the forEach block. Then, to remove an item you set it to objNull, for example.

Code: [Select]
_squad_list set [_i, objNull]; instead of
Code: [Select]
_squad_list = _squad_list - [_x];
And after the forEach:
Code: [Select]
_squad_list = _squad_list - [objNull]This will remove all the elements that have been set to objNull inside the forEach block.

Offline Wolfrug

  • Addons Depot
  • Former Staff
  • ****
  • Official OFPEC Old Timer
Re: Deleting array entry
« Reply #2 on: 02 Apr 2008, 10:35:15 »
That might work, I've had some problems with removing integrated arrays like this in that manner however. You might have to rebuild the whole array upon deletion as well. Something like:

Code: [Select]
//This finds the index number of the squad to be removed
_removeSquad = _squad_list find _x;
_new_squad_list = [];
//This rebuilds the array sans the offending squad
for "_i" from 0 to ((count _squad_list) - 1) do
{
if (_i !=_x) then {_new_squad_list = _new_squad_list + [_squad_list select _i];
};
// This makes the new squad list the current squad list.
_squad_list = _new_squad_list;

I might have some errors in there since I'm not quite as proficient at this as Mandoble & co., but that's the general idea anyway! If you get the ObjNull thing to work, my solution is superfluos anyway!  :good:

Wolfrug out
"When 900 years YOU reach, look as good you will not!"

Offline myke13021

  • Contributing Member
  • **
  • Myke
Re: Deleting array entry
« Reply #3 on: 02 Apr 2008, 15:40:07 »
Indeed, i thought subarrays would be treaten like variables (at least when it comes to add or remove) so i guessed i could eliminate an subarray the same way as i would do with every other variable.

I will try these things out.

Thanks Mandoble and Wolfrug to put some light in my darkened mind  :D


Myke out

:EDIT:
Just to make sure i get it right: the above code including Mandobles solution would look like this:
Code: [Select]
while {(! _return) and (! _squad_dest)} do
{
sleep 0.5;
_i = 0;
{
_ammocount = 0;
_dummy_veh = _x select 0;
_dummy_mag = _x select 1;
{
_ammocount = _ammocount + (_dummy_veh ammo _x);
}
foreach _dummy_mag;
if (_reference == 0) then
{
_reference = _ammocount;
};
if ((fuel _dummy_veh) <= 0.4) exitwith {_return = true};
if (_ammocount <= (_reference / 5)) exitwith {_return = true};
if (((_dummy_veh emptypositions "driver") == 1) or (! (alive driver _dummy_veh))) then
{
hint "booh";
_squad_list set [_i, objNull];
};
//hint format ["%1", _squad_list];
if ((count _squad_list) == 0) exitwith {_squad_dest = true};
if ((damage _dummy_veh) >= 0.6) exitwith {_return = true};
_i = _i + 1;
}
foreach _squad_list;
_squad_list = _squad_list - [objNull];
};
Did i included that part correctly?
Tried it out, didn't worked....the entry wasn't deleted.

Also tried Wolfrugs way. This is how i implemented his snippet:
Code: [Select]
while {(! _return) and (! _squad_dest)} do
{
sleep 0.5;
{
_ammocount = 0;
_dummy_veh = _x select 0;
_dummy_mag = _x select 1;
{
_ammocount = _ammocount + (_dummy_veh ammo _x);
}
foreach _dummy_mag;
if (_reference == 0) then
{
_reference = _ammocount;
};
if ((fuel _dummy_veh) <= 0.4) exitwith {_return = true};
if (_ammocount <= (_reference / 5)) exitwith {_return = true};
if (((_dummy_veh emptypositions "driver") == 1) or (! (alive driver _dummy_veh))) then
{
_removeSquad = _squad_list find _x;
_new_squad_list = [];
for "_i" from 0 to ((count _squad_list) - 1) do
{
if (_i !=_x) then {_new_squad_list = _new_squad_list + [_squad_list select _i]};
};
_squad_list = _new_squad_list;
};
hint format ["%1", _squad_list];
if ((count _squad_list) == 0) exitwith {_squad_dest = true};
//if ((damage _dummy_veh) >= 0.6) exitwith {_return = true};
}
foreach _squad_list;
};
Same result, the entry wont be deleted.

Probably i'm doing something wrong but i can't see where.
« Last Edit: 02 Apr 2008, 17:03:22 by myke13021 »

Offline Mandoble

  • Former Staff
  • ****
    • Grunt ONE and MandoMissile suite
Re: Deleting array entry
« Reply #4 on: 02 Apr 2008, 20:49:10 »
Check your implementation because the described method works perfectly:

Code: [Select]
_array = [["A", "B"], ["C", "D"], ["E", "F"], ["G", "D"]];

_msg = format["init:%1\n\n", _array];

// Removing any element with a "D" inside
_i = 0;
{
   if ("D" in _x) then
   {
      _array set [_i, objNull];
   };
   _i = _i + 1;
} forEach _array;

_array = _array - [objNull];
_msg = _msg + format["end:%1\n\n", _array];

hint _msg;

Offline myke13021

  • Contributing Member
  • **
  • Myke
Re: Deleting array entry
« Reply #5 on: 02 Apr 2008, 23:05:49 »
I guess i know where i made the mistake...the subarray i try to delete also contains an array.
Probably first i have to set objnull this array before i can delete the desired subarray.

To let you see, the _squad_list array may look like this (assuming there's 2 vehicles stored in it):
Code: [Select]
[[group, ["weapon_classname", "weapon_classname"]], [group, ["weapon_classname", "weapon_classname"]]]
So i think i have to use your method on the weapon_classname array first and then delete the vehicle array completely.

subarrays can turn me mad.


I will try this and report back if this will not work.

Offline Mandoble

  • Former Staff
  • ****
    • Grunt ONE and MandoMissile suite
Re: Deleting array entry
« Reply #6 on: 02 Apr 2008, 23:08:13 »
If set that member of the upper array to objNull and then you remove [objNull], the member will be removed, doest matter how many subarrays doest it have inside.

Offline myke13021

  • Contributing Member
  • **
  • Myke
Re: Deleting array entry
« Reply #7 on: 03 Apr 2008, 02:48:34 »
Problem solved with the help of Mandoble. I can't see whats different but it works. Thx again Mandoble


*SOLVED*