Foreach

From the Fallout3 GECK Wiki
Revision as of 14:48, 25 May 2014 by imported>Odessa (Created page)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
< [[::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 map 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.

Syntax

[help]
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 ; * 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 ; * If we find a member is not in Liverpool, we give up
    endif
    rMusician.AddItem Beer 1
loop

; Give a beer to each member of the Beatles until one is found not to be in Liverpool. Hence, assume all Beatles except Paul are in Liverpool; since Paul is the second entry of the array, only John gets a beer.

Notes

  • This page may not be comprehensive, refer to external documentation for further information.

See Also