Home   Help Search Login Register  

Author Topic: Foreach returns??  (Read 1988 times)

0 Members and 1 Guest are viewing this topic.

Offline paddy

  • Contributing Member
  • **
  • Llama whipping good
Foreach returns??
« 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.

Offline nominesine

  • Former Staff
  • ****
  • I'm NOT back!
    • The IKB Forum
Re:Foreach returns??
« Reply #1 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.

OFPEC | Intel Depot
RETARDED Ooops... Retired!

Offline paddy

  • Contributing Member
  • **
  • Llama whipping good
Re:Foreach returns??
« Reply #2 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.

Offline nominesine

  • Former Staff
  • ****
  • I'm NOT back!
    • The IKB Forum
Re:Foreach returns??
« Reply #3 on: 04 Sep 2005, 17:46:38 »
Yes, you saved me som time by testing it for me  ;D
OFPEC | Intel Depot
RETARDED Ooops... Retired!

Offline paddy

  • Contributing Member
  • **
  • Llama whipping good
Re:Foreach returns??
« Reply #4 on: 04 Sep 2005, 17:47:32 »
All of 1 minute, you waster lol

Glad we got that cleared up neway

Offline nominesine

  • Former Staff
  • ****
  • I'm NOT back!
    • The IKB Forum
Re:Foreach returns??
« Reply #5 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 :-)
OFPEC | Intel Depot
RETARDED Ooops... Retired!

Offline macguba

  • Former Staff
  • ****
    • macguba's operation flashpoint page
Re:Foreach returns??
« Reply #6 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.
« Last Edit: 04 Sep 2005, 20:34:39 by macguba »
Plenty of reviewed ArmA missions for you to play

bored_onion

  • Guest
Re:Foreach returns??
« Reply #7 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.

Offline nominesine

  • Former Staff
  • ****
  • I'm NOT back!
    • The IKB Forum
Re:Foreach returns??
« Reply #8 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  :)
OFPEC | Intel Depot
RETARDED Ooops... Retired!

Offline h-

  • OFPEC Site
  • Administrator
  • *****
  • Formerly HateR_Kint
    • OFPEC
Re:Foreach returns??
« Reply #9 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...
« Last Edit: 04 Sep 2005, 20:55:34 by HateR_Kint »
Project MCAR   ---   Northern Fronts   ---   Emitter 3Ditor
INFORMATIVE THREAD TITLES PLEASE. "PLEASE HELP" IS NOT ONE..
Chuck Norris can divide by zero.

Offline Triggerhappy

  • Contributing Member
  • **
  • Llama, it's what's for dinner.
Re:Foreach returns??
« Reply #10 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
« Last Edit: 04 Sep 2005, 21:03:16 by Triggerhappy »

Offline h-

  • OFPEC Site
  • Administrator
  • *****
  • Formerly HateR_Kint
    • OFPEC
Re:Foreach returns??
« Reply #11 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
« Last Edit: 04 Sep 2005, 21:07:16 by HateR_Kint »
Project MCAR   ---   Northern Fronts   ---   Emitter 3Ditor
INFORMATIVE THREAD TITLES PLEASE. "PLEASE HELP" IS NOT ONE..
Chuck Norris can divide by zero.

Offline Triggerhappy

  • Contributing Member
  • **
  • Llama, it's what's for dinner.
Re:Foreach returns??
« Reply #12 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
« Last Edit: 04 Sep 2005, 21:06:49 by Triggerhappy »

Offline Triggerhappy

  • Contributing Member
  • **
  • Llama, it's what's for dinner.
Re:Foreach returns??
« Reply #13 on: 04 Sep 2005, 21:07:26 »
lol, it keeps happening! ;D ;D ;D ;D ;D ;D

Offline h-

  • OFPEC Site
  • Administrator
  • *****
  • Formerly HateR_Kint
    • OFPEC
Re:Foreach returns??
« Reply #14 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
Project MCAR   ---   Northern Fronts   ---   Emitter 3Ditor
INFORMATIVE THREAD TITLES PLEASE. "PLEASE HELP" IS NOT ONE..
Chuck Norris can divide by zero.

Offline Planck

  • Honoured
  • Former Staff
  • ****
  • I'm never wrong ....I'm just not always right !
Re:Foreach returns??
« Reply #15 on: 04 Sep 2005, 21:10:14 »
*Ahem*


Planck
I know a little about a lot, and a lot about a little.

Offline Triggerhappy

  • Contributing Member
  • **
  • Llama, it's what's for dinner.
Re:Foreach returns??
« Reply #16 on: 04 Sep 2005, 21:12:38 »
lol ;D ;D ;D ;D

 :-X

Offline THobson

  • OFPEC Patron
  • Former Staff
  • ****
Re:Foreach returns??
« Reply #17 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])


« Last Edit: 05 Sep 2005, 00:28:51 by THobson »

Offline paddy

  • Contributing Member
  • **
  • Llama whipping good
Re:Foreach returns??
« Reply #18 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?

Offline THobson

  • OFPEC Patron
  • Former Staff
  • ****
Re:Foreach returns??
« Reply #19 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 {}

Offline paddy

  • Contributing Member
  • **
  • Llama whipping good
Re:Foreach returns??
« Reply #20 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.

Offline h-

  • OFPEC Site
  • Administrator
  • *****
  • Formerly HateR_Kint
    • OFPEC
Re:Foreach returns??
« Reply #21 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... ::)
« Last Edit: 05 Sep 2005, 16:50:43 by HateR_Kint »
Project MCAR   ---   Northern Fronts   ---   Emitter 3Ditor
INFORMATIVE THREAD TITLES PLEASE. "PLEASE HELP" IS NOT ONE..
Chuck Norris can divide by zero.

Offline THobson

  • OFPEC Patron
  • Former Staff
  • ****
Re:Foreach returns??
« Reply #22 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
 :)

Offline h-

  • OFPEC Site
  • Administrator
  • *****
  • Formerly HateR_Kint
    • OFPEC
Re:Foreach returns??
« Reply #23 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
Project MCAR   ---   Northern Fronts   ---   Emitter 3Ditor
INFORMATIVE THREAD TITLES PLEASE. "PLEASE HELP" IS NOT ONE..
Chuck Norris can divide by zero.

Offline Triggerhappy

  • Contributing Member
  • **
  • Llama, it's what's for dinner.
Re:Foreach returns??
« Reply #24 on: 06 Sep 2005, 00:53:02 »
lol ;D :D :P

SMILIES

Offline paddy

  • Contributing Member
  • **
  • Llama whipping good
Re:Foreach returns??
« Reply #25 on: 07 Sep 2005, 12:47:30 »
Problem solved - Topic closed