OFPEC Forum

Editors Depot - Mission Editing and Scripting => OFP - Editing/Scripting General => Topic started by: paddy on 04 Sep 2005, 17:29:14

Title: Foreach returns??
Post by: paddy on 04 Sep 2005, 17:29:14
Anyone who has studies the 'foreach' command knows that it allows you to easily issue commands for a variety of units/objects without writing out the command for every object.

However, there is no documentation that I can find that tells me if foreach is able to return values, for instance ....

Code: [Select]
"_x setdammage 1" forEach units group player - The line given in the COMREF, gives an order to units in players group.


Code: [Select]
"getpos _x" forEach units group player - A command that returns a value, possible? ???
I would think if it was it would return an array of some sort

I have no idea how to test this and would be quite interested to find this out for my own scripting purposes.
Title: Re:Foreach returns??
Post by: nominesine on 04 Sep 2005, 17:38:41
The format command is useful for testing. If the foreach command returns any information:

hint format ["%1","getpos _x" forEach units group player]

should give you the coordinates of every member in players group, displayed as a hint on the screen. Write in the activation line of a radio alpha trigger, press 0-0-1 and see what happens.

Title: Re:Foreach returns??
Post by: paddy on 04 Sep 2005, 17:44:31
Tried it and the hint said "<null>"

I guess this proves that the foreach command can ONLY issue orders and will not return values ...

I hope this helps others as much as myself.
Title: Re:Foreach returns??
Post by: nominesine on 04 Sep 2005, 17:46:38
Yes, you saved me som time by testing it for me  ;D
Title: Re:Foreach returns??
Post by: paddy on 04 Sep 2005, 17:47:32
All of 1 minute, you waster lol

Glad we got that cleared up neway
Title: Re:Foreach returns??
Post by: nominesine on 04 Sep 2005, 17:50:20
Me too. Leave this thread open a couple of days though. My experience tells me that the scripting gurus (where are you General B???) can have a different results when they do their testing. And they are usually correct :-)
Title: Re:Foreach returns??
Post by: macguba on 04 Sep 2005, 20:31:52
Nah, returning values is no problem.   I would think.   Try something like:

{hint format ["%1", getDammage _x]} forEach units group player

The hints will fly by instantly of course, and I'm not sure how you would get a delay in it, and I'm pretty confident that the syntax is wrong, but give it a try.

I never know the exact rules for curly brackets {} and quotes "".

My point is that forEach is not a sophisticated command: it just does what it is told.   For example you can nest them.   I'm not a scripting guru though.

But you're right, it doesn't return an array or indeed anything else.  It just carries out the code.    I suppose you could add the terms to an array one by one within the code.
Title: Re:Foreach returns??
Post by: bored_onion on 04 Sep 2005, 20:38:00
perhaps the count command could be used to similar effect in order to separate out the elements. so the script would count the number of units it would be getting information for and would then display the information one at a time in a delay. then you could make the script take all these variables and stick it in an array.
Title: Re:Foreach returns??
Post by: nominesine on 04 Sep 2005, 20:50:15
Quote
My experience tells me that the scripting gurus can have a different results when they do their testing. And they are usually correct :-)

Watson, release the hounds. The game is afoot  :)
Title: Re:Foreach returns??
Post by: h- on 04 Sep 2005, 20:53:46
:P

Code: [Select]
wupii=[]; "wupii=wupii+getpos _x" foreach units group player; hint format ["%1",wupii]Put that in your radio trigger and fire away...

There's bound to be a more 'intelligent' way, but at least that works ;)

EDIT:
It's not the forEach that returns anything, it's a function that goes through an array of element issuing what ever is in the "" quotes to each of those elements..
So in this case it's the getPos that returns the values, not the forEach...
Title: Re:Foreach returns??
Post by: Triggerhappy on 04 Sep 2005, 20:59:40
Code: [Select]
_pos_array = []
{_pos_array = _pos_array + [(getpos _x)]} foreach units group player
hint format ["%1",_pos_array]

i believe thats what you wanted  ;D

remember hater, that adding arrays together puts it all into 1 array, so you need to add brackets around the getpos to keep the position in an array by itself among the others
Title: Re:Foreach returns??
Post by: h- on 04 Sep 2005, 21:01:14
Well, that's exactly the same as mine but I made it work in a trigger for consistency since it was the method used by the 'discussors' earlier ;)

EDIT:
By the way, your method makes it return something like
... ;)


EDIT:
;D Damn!
Forgot that totally...

I really should do some mission/etc based scripting more often ;D
Title: Re:Foreach returns??
Post by: Triggerhappy on 04 Sep 2005, 21:04:29
i posted during your post and didn't see it, although there is a difference, other than the radio trigger thing...can you find it? :P

EDIT:stop changing stuff while im posting!!!
i edited that as well, i had one extra [] but now its right

yours on the other hand would give this:

[312.32,343.33,567.87,695,785,200]
not keeping them seperated
Title: Re:Foreach returns??
Post by: Triggerhappy on 04 Sep 2005, 21:07:26
lol, it keeps happening! ;D ;D ;D ;D ;D ;D
Title: Re:Foreach returns??
Post by: h- on 04 Sep 2005, 21:08:00
;D ;D ;D ;D ;D

OMG this is getting out of hand ;D ;D

But yes, you are absolutely right, me bad :P ;D
Title: Re:Foreach returns??
Post by: Planck on 04 Sep 2005, 21:10:14
*Ahem*


Planck
Title: Re:Foreach returns??
Post by: Triggerhappy on 04 Sep 2005, 21:12:38
lol ;D ;D ;D ;D

 :-X
Title: Re:Foreach returns??
Post by: THobson on 05 Sep 2005, 00:23:57
Wow this wasn't here this morning!

If you want the forEach command to create a list of unit positions try:

Code: [Select]
_positions = []
{_positions = _positions + [getPos _x]} forEach units group player
I haven't tested it but it looks like it should work

EDIT:
If you want an array of positions of all the unit in the player's group - but not the player (and I think you do want that if I remember your script correctly) then try:

Code: [Select]
_positions = []
{_positions = _positions + [getPos _x]} forEach ((units group player) - [player])


Title: Re:Foreach returns??
Post by: paddy on 05 Sep 2005, 10:16:01
Code: [Select]
_pos_array = []
{_pos_array = _pos_array + [(getpos _x)]} foreach _eastunits
hint format ["%1",_pos_array]


WAHH!!

I thought 'foreach' only worked with a string like in the comref, can this be true, can BIS comref be inaccurate again?
Title: Re:Foreach returns??
Post by: THobson on 05 Sep 2005, 10:29:54
ForEach works with any set of instructions.  These instructions are defined within a string.  Some people use "" to identify the string and some prefer {}
Title: Re:Foreach returns??
Post by: paddy on 05 Sep 2005, 10:34:16
That's news to me, well thanks guys.  This has definitly solved my dilemma.

However i'll leave the post open for another few days just incase someone else has questions or any brainwaves.
Title: Re:Foreach returns??
Post by: h- on 05 Sep 2005, 16:49:59
I just wanted to pop in and say that we did offer the same solution as THobson before all the editing went bananas, so it might have been missed because of that ;D

'My apologies for inconviniencing'... :P

Next time I will think before posting, I swear... ::)
Title: Re:Foreach returns??
Post by: THobson on 05 Sep 2005, 17:12:27
I see it/them now. Replies#9 & 10.  

Last night I was in a hurry, I saw the question, skimmed some of the replies - saw some attempts to use format to get forEach to return a value, followed by lots of smileys so I stuck in my two cents worth.   Indeed you both had it right way back.

Next time I will think read before posting, I swear...

Possibly
 :)
Title: Re:Foreach returns??
Post by: h- on 05 Sep 2005, 18:37:51
I'm not trying to have a pissin' contest here :P

Just saying that there actually was some logic in the smiley/edit chaos back there... somewhere... prolly...

:-X
Title: Re:Foreach returns??
Post by: Triggerhappy on 06 Sep 2005, 00:53:02
lol ;D :D :P

SMILIES
Title: Re:Foreach returns??
Post by: paddy on 07 Sep 2005, 12:47:30
Problem solved - Topic closed