Difference between revisions of "Foreach"
Jump to navigation
Jump to search
imported>Odessa (Created page) |
imported>Odessa (entries are 'stringmap' not 'map' artays) |
||
Line 1: | Line 1: | ||
{{Function | {{Function | ||
|origin = NVSE | |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| | |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 '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 entries. | 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 | |name = Foreach | ||
Line 58: | Line 58: | ||
let rMusician := aEntry["value"] | let rMusician := aEntry["value"] | ||
if rMusician.GetDead | if rMusician.GetDead | ||
continue ; * we ignore dead members | continue ; * Go direct to next entry: we ignore dead members | ||
endif | endif | ||
rMusician.AddItem Beer 1 | rMusician.AddItem Beer 1 | ||
Line 71: | Line 71: | ||
let rMusician := aEntry["value"] | let rMusician := aEntry["value"] | ||
if rMusician.GetInWorldSpace Liverpool == 0 | if rMusician.GetInWorldSpace Liverpool == 0 | ||
break ; * | break ; * End Loop immediately if we find a member is not in Liverpool | ||
endif | endif | ||
rMusician.AddItem Beer 1 | rMusician.AddItem Beer 1 | ||
loop | loop | ||
; Give a beer to each member of the Beatles until one is found not to be in Liverpool | ; 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> |
Revision as of 17:13, 30 May 2014
< [[::Category:Functions|Category:Functions]]
A function added by the New Vegas Script Extender.
Description
Added by NVSE V4. Used to repeat a script block for each entry in an array. Each entry is itself a stringmap array with two fields, "key" and "value".
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.
Syntax
Foreach entry:array <- set:array
array_var aSomeArray array_var aEntry 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
Example
array_var aBeatles array_var aEntry ref rMusician int iPosition let aBeatles := ar_List JohnREF, PaulREF, GeorgeREF, RingoREF foreach aEntry <- aBeatles let iPosition := aEntry["key"] let rMusician := aEntry["value"] PrintC "Entry %g is %n", iPosition, rMusician loop ; Will print in game: ; Entry 0 is John Lennon ; Entry 1 is Paul McCartney ; Entry 2 is George Harrison ; Entry 3 is Ringo Starr
; Using Continue foreach aEntry <- aBeatles let rMusician := aEntry["value"] if rMusician.GetDead continue ; * Go direct to next entry: we ignore dead members endif rMusician.AddItem Beer 1 loop ; Every living member of the Beatles is given a beer
; Using Break foreach aEntry <- aBeatles let rMusician := aEntry["value"] if rMusician.GetInWorldSpace Liverpool == 0 break ; * End Loop immediately if we find a member is not in Liverpool endif rMusician.AddItem Beer 1 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.
Notes
- This page may not be comprehensive, refer to external documentation for further information.