Editing Foreach
Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.
The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.
Latest revision | Your text | ||
Line 1: | Line 1: | ||
Added by NVSE | {{Function | ||
|origin = NVSE | |||
|summary = Added by NVSE V4. Used to repeat a script block for each entry in an array. Each entry is itself a [[Array Variables|stringmap array]] with two fields, "key" and "value". | |||
Within foreach loops, the command | Within foreach loops, the command 'continue' may be used to skip any remaining loop code for that entry, and move on to the next. In the same context, the command 'break' may be used to end the loop immediately, ignoring any remaining code and entries. | ||
== | |name = Foreach | ||
|returnType = void | |||
|arguments = | |||
{{FunctionArgument | |||
|Name = entry | |||
|Type = array{{FunctionArgument | |||
|Name = <- | |||
|Type = | |||
}} | |||
}}{{FunctionArgument | |||
|Name = set | |||
|Type = array | |||
}} | |||
}} | |||
<pre> | <pre> | ||
array_var aSomeArray | |||
array_var aEntry | |||
array_var | |||
array_var | |||
foreach | foreach aEntry <- aSomeArray | ||
; | ; aEntry["key"] is the key of each entry in the array (0, 1, 2... for lists) | ||
; | ; aEntry["value"] is value of each entry in the array | ||
loop | loop | ||
</pre> | </pre> | ||
Line 36: | Line 31: | ||
==Example== | ==Example== | ||
<pre> | <pre> | ||
array_var aBeatles | |||
array_var aEntry | |||
array_var | |||
array_var | |||
ref rMusician | ref rMusician | ||
int iPosition | int iPosition | ||
let | let aBeatles := ar_List JohnREF, PaulREF, GeorgeREF, RingoREF | ||
foreach | foreach aEntry <- aBeatles | ||
let iPosition := | let iPosition := aEntry["key"] | ||
let rMusician := | let rMusician := aEntry["value"] | ||
PrintC "Entry %g is %n", iPosition, rMusician | |||
loop | loop | ||
; Will print in game: | ; Will print in game: | ||
; Entry | ; Entry 0 is John Lennon | ||
; Entry | ; Entry 1 is Paul McCartney | ||
; Entry | ; Entry 2 is George Harrison | ||
; Entry | ; Entry 3 is Ringo Starr | ||
</pre> | </pre> | ||
<pre> | |||
; Using Continue | |||
foreach aEntry <- aBeatles | |||
let rMusician := aEntry["value"] | |||
foreach | |||
let rMusician := | |||
if rMusician.GetDead | if rMusician.GetDead | ||
continue ; * Go direct to next entry: we ignore dead members | continue ; * Go direct to next entry: we ignore dead members | ||
endif | endif | ||
rMusician.AddItem Beer | rMusician.AddItem Beer 1 | ||
loop | loop | ||
; Every living member of the Beatles is given a beer | ; Every living member of the Beatles is given a beer | ||
</pre> | </pre> | ||
<pre> | |||
; Using Break | |||
foreach aEntry <- aBeatles | |||
let rMusician := aEntry["value"] | |||
foreach | if rMusician.GetInWorldSpace Liverpool == 0 | ||
let rMusician := | |||
if rMusician.GetInWorldSpace Liverpool | |||
break ; * End Loop immediately if we find a member is not in Liverpool | break ; * End Loop immediately if we find a member is not in Liverpool | ||
endif | endif | ||
rMusician.AddItem Beer 1 | |||
loop | loop | ||
; Give a beer to each member of the Beatles until one is found not to be in Liverpool- assume that all Beatles except Paul are in Liverpool; since Paul is the second entry of the array, only John, the first, gets a beer. | ; Give a beer to each member of the Beatles until one is found not to be in Liverpool- assume that all Beatles except Paul are in Liverpool; since Paul is the second entry of the array, only John, the first, gets a beer. | ||
</pre> | </pre> | ||
==Notes== | |||
*This page may not be comprehensive, refer to external documentation for further information. | |||
==See Also== | ==See Also== | ||
*[[Array Variables]] | *[[Array Variables]] | ||
*[[Let]] | *[[Let]] | ||
*[[While]] | |||
*[[Eval]] | *[[Eval]] | ||
*[[Label | *[[Label]] | ||
*[[ | *[[Goto]] | ||
[[Category:Functions_(NVSE)]] | [[Category:Functions_(NVSE)]] | ||
[[Category:Commands]] | [[Category:Commands]] | ||
[[Category:Scripting]] | [[Category:Scripting]] |