While

Revision as of 04:54, 11 April 2014 by imported>Odessa (Started page)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
< [[::Category:Functions|Category:Functions]]

A function added by the New Vegas Script Extender.

Description

Added by NVSE V4. Whilst a specified condition is met, the code section below, until the 'loop' command, will repeat. Within while loops, the command 'continue' may be used to skip remaining loop code and return to the while line. In the same context, the command 'break' may be used to end the loop immediately, regardless of the while condition.

The same effect could be achieved using label, goto, if and endif statements, but this method allows for fewer lines and improved readability. Care must be taken to avoid the possibility of infinite loops- a while condition that never becomes false will immediately crash the game.

Syntax

While Condition:boolean
while ( SomeCondition ) ; this is similar to an 'if' statement
    ; do something
loop ; this is similar to an 'endif' statement

int iCount
set iCount to 0
while ( iCount < 5 )
    let iCount += 1 ; Note: in this example, the increment is before the print
    MessageEx "%g" iCount
loop
; The above code will print the numbers 1, 2, 3, 4 and 5 as messages in game

set iCount to 0
while ( iCount < 5 )
    let iCount += 1
    if iCount == 2
        continue ; go back to the loop start
    elseif iCount == 4
        break ; end the loop immediately
    endif
    MessageEx "%g" iCount
loop
; The above code will print the numbers 1 and 3 as messages in game. The number 2 is skipped due to the continue, whilst numbers 4 and 5 are skipped because of the break.


Notes

  • The above examples demonstrate simple usage of the expressions, see external documentation for more complex techniques.

See Also