Jump to content
Sign in to follow this  
[frl]myke

[Beginners guide]: Arrays

Recommended Posts

Another source of information and examples is to do this:

De-pbo Arma 2\AddOns\modules.pbo, then go to:

Arma 2\AddOns\modules\Functions\arrays\ and check out the scripts there.

Ehm, my example is really not that advanced when you examine it :) It looks a bit messy due to all the comments, but comments are insanely valuable to those trying to learn this stuff. I tried nice indenting on the comments, but the forum messed it up making it look ugly. And it shows several things you often would use arrays for and do with and to them, with an actual script that does something rather than arbitrary examples which might be hard to put into context.

Remember sometimes the new scripters to arrive the scene has no coding background whatsoever. For me (and probably most new scripters), that was was made the Wiki so damned hard to use. Every example I tried never worked.

Share this post


Link to post
Share on other sites

What is the:

for "_i" from 0 to (count _arrayAdd) do

especially the

"_i" from 0

bit about?:confused:

Share this post


Link to post
Share on other sites
What is the:

for "_i" from 0 to (count _arrayAdd) do

especially the

"_i" from 0

bit about?:confused:

That is for another topic and not related to arrays. Maybe this weekend i can make a "How-to" about such loops.

Please don't be offended, i just try to keep this topic clear and focussed. You'll get your answer soon, please be patient.

Share this post


Link to post
Share on other sites

Works sorta like a forEach.

What it does is declare a varaible "_i". This variable starts at value 0, and then the code inside of the do statement is executed (with the variable _i still using the value 0). Then it checks if _i is equal to (count _arrayAdd). If not, it increments the value of _i (so now it's 1) and executes the code again with that value for _i. It keeps doing this until _i is equal to (count _arrayAdd).

And that's a breif description of a for loop. For loops are extremely common in most programming languages, so there is a lot of documentation out there and still don't understand it, just do a google search and you'll probably find a nice explanation of it.

Share this post


Link to post
Share on other sites

it's wrong anyway, since it really should be

for "_i" from 0 to ((count _arrayAdd) - 1) do
{
  ...
};

... just in case you happen to not have a fetish for index-out-of-bounds error messages. :)

Btw. I feel this is partially ontopic nevertheless, since it's about accessing/working with arrays. To do this reliably, one needs to understand that arrays are indexed, starting from zero and that the last index is always going to be the number of items in the array minus one.

But sure, I woudln't mind either explaining loops in here, or anywhere else, since there is already a pretty good article about control structures available.

Share this post


Link to post
Share on other sites

does anyone know why this code does not remove the entry?

serverminearray = serverminearray + [_playerpos];

serverminearray = serverminearray - [_themine];

both variables equal the same position array, confirmed by formatted text messages, the + line works correctly but the - one does not remove the position array? whats the correct way? I also tried

serverminearray = serverminearray - _themine;

still did not work.

Share this post


Link to post
Share on other sites

read http://community.bistudio.com/wiki/Array,Subtraction:

Nested arrays cannot be substracted

You can't remove arrays directly from arrays - and a position is exactly that. An array. So you either delete by index or replace the position with something that can be removed from an array with this operator. Or you keep an array of your positions (say _positionPool) and an array that only saves indices to these positions. These indices, beeing integer, can be easily removed by the array substraction operation.

Edited by ruebe

Share this post


Link to post
Share on other sites
read http://community.bistudio.com/wiki/Array,Subtraction:

You can't remove arrays directly from arrays - and a position is exactly that. An array. So you either delete by index or replace the position with something that can be removed from an array with this operator. Or you keep an array of your positions (say _positionPool) and an array that only saves indices to these positions. These indices, beeing integer, can be easily removed by the array substraction operation.

can anyone give an example on how to delete these nested arrays properly? I tried

serverminearray = serverminearray - serverminearray select _index;

_index = the location of the nested array

and even

serverminearray = serverminearray - (serverminearray select _index)select 0;

trying to delete just one entry of nested array

both made my array return any so it broke the array entirely :(

Share this post


Link to post
Share on other sites

I think my question belongs in this thread:

If i have two scripts, running in parallel, and working with same global array (adding, and removing elements),

is it safe to use array = array - [_olditem] and array Set [count array, _newitem] directly in each of those scripts?

Is it "thread safe"?

Or should these operations be somehow queued and performed in one place (in a function perhaps?) to ensure consistency?

I mean... isn't there a theoretical danger that array Set [count array, newitem] in 1st script will be negated by 2nd script doing array = array - [olditem]?

...is it possible that array - [olditem] can be executed before array Set [...], and after that the part array = array ... will be executed,

resulting in array not containing newitem ?

Share this post


Link to post
Share on other sites

I wold be very careful here. Why two scripts working the same array at the same time? If absolutely necessary, I'd use variables to keep track of progress, and wait as needed, to avoid the potential problems you (and I) expect.

Share this post


Link to post
Share on other sites

One script is spawning units and adding them into that array, second script is removing them when they die (called from killed eventhandler), and third is deleting (despawning) the units when they are not needed (or if there are no other units in their vicinity to interact with).

The array is used to iterate over all those units in an infinte loop and do something with/to each of those units (you go there, you stay here, you look over there, you kill that guy, etc.).

...it's for the zombies.

Anyway, when thinking about it with a fresh brain after i got some sleep, it seems clear it is safe, otherwise well ...people would know - almost everybody who did something with arrays would most probably experienced some problem.

And it also doesn't make sense.

AND also i was already told by others that it should be ok.

Share this post


Link to post
Share on other sites

As CarlGustaffa said, great care is to be taken when multiple scripts alter the same array at the same time, for what you are doing, there is no problem as one is only adding and the other is removing.

But if one was to do something with one object in array and another script removes the object, it will be conflicts ofc, either getting wrong type(string, group, object etc..) or using wrong unit, or getting out of bounds or simply not do anything as the object is no longer in the array.

Worst case scenario is ctd with out of bounds, special circumstances.

simple "safety" checks work fine in many cases, like:

if (object in arrayname) then {do stuff} else {do nothing or something else};

or simply assign a variable as mentioned above and wait for it to be true

waitUntil {variable};

and switching it on/off in the relevant scripts, then you will never get a conflict unless coding is not taking into acount that items are removed and added by multiple scripts.

But it all comes down to code, you can make it work fine, or really just create a mess.

Edited by Demonized

Share this post


Link to post
Share on other sites

Only 1 statement can be executed at a time, and afaik they are executed in full, so array set [count array, _entry]; and array = array - [_entry]; should be safe.

Share this post


Link to post
Share on other sites

Can you do this to an array?

_myArray = ["inGameObject1,inGameObject2","inGameObject4"];

The inGameObject is not AI but rather an object i.e building etc...

Also would it consider that the "inGameObject1,inGameObject2" as array position 0 and "inGameObject4" as position 1?

Thanks

Share this post


Link to post
Share on other sites

"Arrays of Arrays" or how to access nested arrays

your doubts explained here.

and what types of variables can an array contain.

Share this post


Link to post
Share on other sites
Can you do this to an array?

_myArray = ["inGameObject1,inGameObject2","inGameObject4"];

The inGameObject is not AI but rather an object i.e building etc...

Also would it consider that the "inGameObject1,inGameObject2" as array position 0 and "inGameObject4" as position 1?

Thanks

I just re-looked at this and realized that this became a string with the double quotation marks.

Share this post


Link to post
Share on other sites

How would one go about gathering information from a passed array. Example being a trigger was activated, and the passed array is [by, type, repeating]. So how would one gather the information from a passed array?

Share this post


Link to post
Share on other sites
How would one go about gathering information from a passed array. Example being a trigger was activated, and the passed array is [by, type, repeating]. So how would one gather the information from a passed array?

Select

Ex:

by = array select 0;

Share this post


Link to post
Share on other sites

I don't think triggers actually pass that kind of information when they activate. They pass thislist which is an array of units within the trigger area that satisfy the trigger conditions and thisTrigger which is the object of the trigger itself.

Share this post


Link to post
Share on other sites

Oh ok, well I checked up on my previous post, and according to BIS triggerActivation returns an array [by, type, repeating]. So unless By = side in this case. So according to Dawg, to use the passed array of trigger used to execute an .sqs or .sqf all I need to use is "_by = array select 0;" etc. I honestly thought it would be more complex than that. I figured I would have to do something of the nature like "trgarray = triggerActivaction trg1; by = trgarray select 0;". Thanks for the information, I'll test this and post back results of test.

Share this post


Link to post
Share on other sites

triggerActivation just checks what the by, type and repeating of a trigger is. Not quite sure why you'd need or not know that however.

What exactly are you trying to do or check?

Share this post


Link to post
Share on other sites

I figured with the passed array I could get who activated the trigger, the exact unit specifically. I would like to use the trigger as a way of relocating them. So I figured I could use one of the elements of the passed array, and use the "setpos" command and put them back in the middle of the AO.

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  

×