OFPEC Forum

Editors Depot - Mission Editing and Scripting => OFP - Editing/Scripting General => Topic started by: Kundich on 27 Apr 2005, 00:06:50

Title: Removing Element From Array...
Post by: Kundich on 27 Apr 2005, 00:06:50
This may have a relatively simple solution:

I have an array containing group names with noone assigned to the group (utilized the deleteVehicle command on original unit for each group).  I call a random group name(s) during one iteration of the script, and want to remove that groupname from the array (although I want to reinsert it at a later point in the script).  The most important lines of code are as follows:

_group = [fia_group1, fia_group2, fia_group3, fia_group4, fia_group5, fia_group6, fia_group7]

_groupLD = random 6
_groupD = _groupLD - (_groupLD mod 1)
_groupL = (_group select _groupD)


Basically, I want to remove _groupL from _group.  Later, I will add it back.  As I said, I'm sure it has a simple solution, but it's really starting to hack me off.

Any ideas?
Title: Re:Removing Element From Array...
Post by: Chris Death on 27 Apr 2005, 00:11:05
If you head over to the ed-depot, you should find a function,
which will do this for you  ;)

If you want to know how to do it manually -> here ya go:

You will have to create a new array by copying the first one,
just that you don't add the element, which you want to remove.
Finally you replace the first array with the new one.

Or: you keep the original one for later use (like you said, you
may want to add the element later again).

~S~ CD
Title: Re:Removing Element From Array...
Post by: Kundich on 27 Apr 2005, 00:13:14
Thanks, but my problem is the element to be removed is a random element of the array during each iteration.  That's what's got me confused.  I do want to know how to do it manually, but the element could be any group.  Does that make sense?

I must be stupid, eh?  Thanks for your help, though.


Thanks,
ADM
Title: Re:Removing Element From Array...
Post by: General Barron on 27 Apr 2005, 00:15:27
...OR you can just use OFP's - command (http://www.ofpec.com/editors/comref.php?letter=num#-). From the com ref:

arrayA - arrayB

Operand types:
arrayA: Array
arrayB: Array
Type of returned value:
Array
Description:
all elements in arrayB removed from arrayA

So in your case it would be:

_group = _group -  [_groupL]

Title: Re:Removing Element From Array...
Post by: Kundich on 27 Apr 2005, 00:18:12
Excellent.  I was trying it:

_group = _group - _groupL

NO BRACKETS!!!  I will check it out tonight.  Thanks for your help.


Thanks,
ADM
Title: Re:Removing Element From Array...
Post by: General Barron on 27 Apr 2005, 00:31:30
Yeah, you have to read the com ref really carefully sometimes. It asks for two ARRAYS as input. You were trying to pass it an array and a GROUP. I've messed this one up many times before. Same goes for the array + array command.
Title: Re:Removing Element From Array...
Post by: Chris Death on 27 Apr 2005, 00:32:25
@GB - you're right, array = array - [element] should be ok too,
maybe it could lead into a prob when trying to recreate the
original array with same order of elements but it should work.

@Kundich - it doesn't matter, wether you know what to remove
or not.

When building the new array, you would just have to do it
that way:

if (element to be added into new_array) not = (element to be
removed from old_array), then new_array = new_array + [element]

:note - this is not syntax version (just the theory, how to do it)

~S~ CD
Title: Re:Removing Element From Array...
Post by: THobson on 27 Apr 2005, 07:28:36
Just to add further the the discussion.  If the elements of the array are arrays themselves (for example if you have an array of positions) then you cannot remove any of the elements by using the - command
Title: Re:Removing Element From Array...
Post by: Chris Death on 27 Apr 2005, 21:39:57
Just to add further the the discussion.  If the elements of the array are arrays themselves (for example if you have an array of positions) then you cannot remove any of the elements by using the - command

For exactly that reason i tend to using the version i described above,
as when i've been working on my multi group control engine i've been
working with arrays consisting of other arrays. I prefer to use methods, which work for every case, but then again off course can
happen that easy solutions like General Barrons subtract element
from array become forgotten  :D

~S~ CD
Title: Re:Removing Element From Array...
Post by: Kundich on 28 Apr 2005, 02:24:05
Thanks for the input, guys.  It worked like a charm.  Amazing how two little characters can make such a world of difference.

My next problem is somewhat related: under normal circumstances, a group is limited to twelve units, array elements 0 to 11.  If the units in a group are killed, they automatically should be removed from an array, yes?  

If so, is there some limit to the number of AI units that may be spawned to a single group over time?


Thanks,
ADM
Title: Re:Removing Element From Array...
Post by: THobson on 28 Apr 2005, 07:46:51
As long as the group does not exceed 12 members at anyone time you can just keep going.  Note however that it sometimes takes a little while for dead units to be removed from the group.  Try doing a count groupname and see.  Think about when you are leading a group and members get killed, if they are out of sight of other group memebers it can take a while before you are told they are dead.  Untill the leader knows they are dead they are still in the group
Title: Re:Removing Element From Array...
Post by: macguba on 28 Apr 2005, 10:12:48
IIRC count groupname returns the number of loons the group leader thinks are in the group.     Or something.    Use "alive _x " count units grpName to get the atual number.
Title: Re:Removing Element From Array...
Post by: THobson on 28 Apr 2005, 11:04:44
True, but if you are planning on adding new members to a group which is the number that counts against the limit of 12, the number in the group or the number of living units in the group?   I have not done any experimenting with this and I have always assumed the first of these is the impotant one, but I could be wrong.
Title: Re:Removing Element From Array...
Post by: macguba on 28 Apr 2005, 12:32:59
I think - although I'm not sure - that the number of units in the group is the number the group leader thinks there are.     Using an alive command crystallises the losses.    

I'm not sure if count actually returns a wrong answer, but certainly a trigger based on count==0 should always be done with an alive command as well, otherwise there can be a delay of up to several minutes before the trigger fires.
Title: Re:Removing Element From Array...
Post by: THobson on 28 Apr 2005, 12:45:46
Quote
Using an alive command crystallises the losses.
I didn't know that.  Thanks.
Title: Re:Removing Element From Array...
Post by: Kundich on 30 Apr 2005, 18:40:58
Thanks so much.  That's perfect.


Thanks,
ADM
Title: Re:Removing Element From Array...
Post by: Kundich on 02 May 2005, 02:46:05
I appreciate all the help.  I think I've got it now.  Thanks for all the help.


Thanks,
ADM