Difference between revisions of "Foreach"

From the Fallout3 GECK Wiki
Jump to navigation Jump to search
imported>Odessa
(note on direct array access)
imported>Odessa
(exapnded, it works for containers + strings too)
Line 1: Line 1:
{{Function
Added by NVSE 4. Used to repeat a script block for each entry in a collection. The collection may be either a container, [[Array Variable|array]] or [[String Variable|string]]. In the case of a container, an entry is a [[Ref Variable|ref]] to an item in its inventory. For an array, each entry is a stringmap with two fields, <b>"key"</b> and <b>"value"</b>. For a string, each entry is a string containing a single character.
|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, <b>"key"</b> and <b>"value"</b>.


Within foreach loops, the command <b>continue</b> may be used to skip any remaining loop code for that entry, and move on to the next. In the same context, the command <b>break</b> may be used to end the loop immediately, ignoring any remaining code and entries.
Within foreach loops, the command <b>continue</b> may be used to skip any remaining loop code for that entry, and move on to the next. In the same context, the command <b>break</b> may be used to end the loop immediately, ignoring any remaining code and entries.
 
==Syntax==
|name = Foreach
<pre>
|returnType = void
foreach Item:ref <- Container:ref
|arguments =  
    ; Item is an item in container
  {{FunctionArgument
loop
  |Name = entry
</pre>
  |Type = array{{FunctionArgument
  |Name = <-
  |Type =
  }}
  }}{{FunctionArgument
  |Name = set
  |Type = array
  }}
}}
<pre>
<pre>
array_var SomeArray
foreach Entry:array <- Collection:array
array_var Entry
 
foreach Entry <- SomeArray
     ; Entry["key"] is the key (index) of each entry (0, 1, 2... for lists)
     ; Entry["key"] is the key (index) of each entry (0, 1, 2... for lists)
     ; Entry["value"] is the value of each entry in the array
     ; Entry["value"] is the value of each entry in the array
loop
</pre>
<pre>
foreach Char:string <- String:string
    ; Char is a single character in String
loop
loop
</pre>
</pre>

Revision as of 12:41, 14 September 2014

Added by NVSE 4. Used to repeat a script block for each entry in a collection. The collection may be either a container, array or string. In the case of a container, an entry is a ref to an item in its inventory. For an array, each entry is a stringmap with two fields, "key" and "value". For a string, each entry is a string containing a single character.

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 Item:ref <- Container:ref
    ; Item is an item in container
loop
foreach Entry:array <- Collection:array
    ; Entry["key"] is the key (index) of each entry (0, 1, 2... for lists)
    ; Entry["value"] is the value of each entry in the array
loop
foreach Char:string <- String:string
    ; Char is a single character in String
loop

Example

array_var Beatles
array_var Entry
ref rMusician
int iPosition

let Beatles := ar_List JohnREF, PaulREF, GeorgeREF, RingoREF

foreach Entry <- Beatles
    let iPosition := Entry["key"]
    let rMusician := Entry["value"]
    Print "Entry #" + $iPosition + " is " + $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 Entry <- Beatles
    let rMusician := Entry["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 Entry <- Beatles
    let rMusician := Entry["value"]
    if rMusician.GetInWorldSpace Liverpool
        rMusician.AddItem Beer, 1
    else
        break ; * End Loop immediately if we find a member is not in Liverpool
    endif
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.

You can also use the Ar_Range command to approximate the traditional 'C' style for loop. The code below prints the numbers 0-10:

foreach Entry <- (Ar_Range 0, 10)
    Print $Entry["value"]
loop

Note that above we reference Entry["value"] in the Print function directly, rather than use an intermediary variable (let SomeVar := Entry["value"]..). This is only possible when using NVSE aware functions or the Script Compiler Override.

See Also

External Links