Whilevarn: Difference between revisions

From EDukeWiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
'''whilevarn''' <[[gamevar]]> <value> { code to execute }<br>
<span {{code}}>'''whilen''' <gamevar> <constant|gamevar> { code to execute }</span>
'''whilevarvarn''' <[[gamevar]]> <[[gamevar]]> { code to execute }


Executes the code in the curly braces as long as <gamevar> does not equal <value>. This is a simple while loop.
<span {{code}}>'''whilevarn''' <gamevar> <constant> { code to execute }</span> ''(deprecated)''
 
<span {{code}}>'''whilevarvarn''' <gamevar> <gamevar> { code to execute }</span> ''(deprecated)''
 
Executes the code in the curly braces as long as the values of the two parameters are not equal. This is a simple while loop.
 
In general, it is preferable to use [[for]] loops if possible, for reasons of performance and robustness.


Caution: Poor execution or endless loops will cause your game to close without error or simply stall and crash!
Caution: Poor execution or endless loops will cause your game to close without error or simply stall and crash!

Revision as of 11:05, 15 December 2024

whilen <gamevar> <constant|gamevar> { code to execute }

whilevarn <gamevar> <constant> { code to execute } (deprecated)

whilevarvarn <gamevar> <gamevar> { code to execute } (deprecated)

Executes the code in the curly braces as long as the values of the two parameters are not equal. This is a simple while loop.

In general, it is preferable to use for loops if possible, for reasons of performance and robustness.

Caution: Poor execution or endless loops will cause your game to close without error or simply stall and crash!

This example will set up the palette of the floor from all sectors on the map:

gamevar TEMP 0 0
onevent EVENT_LOADACTOR
  setvar TEMP 0
  whilevarvarn TEMP NUMSECTORS
  {
    setsector[TEMP].floorpal 1
    addvar TEMP 1
  }
endevent

There's no variable keeping the number of sprites in the map. The sprites may be randomly arranged throughout the array with a maximum of 16384 entries. An example of a correct per-sprite loop:

setvar sectnum 0
whilevarvarn sectnum NUMSECTORS
{
  headspritesect I sectnum
  setvarvar HEAD I
  whilevarn I -1 {
    // LOOP CODE
    nextspritesect I I
    ifvarvare I HEAD setvar I -1 // Break the loop
  }
  addvar sectnum 1
}

Commands with an additional "var" suffix take gamevars rather than constants or defined labels for their inputs. As an alternate short form, "varvar" can be dropped from these commands; for example ife serves an an alias for ifvarvare.