# Loops

Labels and Routers can be used to create common code loops like a while, for and for each loop.

Below we list a few examples to base your own script code and story loops.


# For

The following loop runs 10 times before continuing the story flow.

$ :i = 0
@ MyForLoop | ? {:i} < 10

  - .
    Index: {:i}

$ :i += 1
-> MyForLoop

# While

The following loop runs until the custom function $ MyEscapeFunction changes the value of the temporary variable :flag to on.

$ :flag = off
@ MyWhileLoop | ? {:flag} == off

  // This function changes the value of the 'flag' quillscript variable.
  $ MyEscapeFunction

$ Wait 0.1    // Prevent an infinity loop from locking the frame
-> MyWhileLoop

# For Each

The following loop shows the value of all elements in an array.

# Array or Set

$ &Level.ArrayVar		             // Custom function: Store the Array in a variable called '$Array'
$ ^Quillscript.Tools.Length {$Array} // Custom function: Store the length in a variable called '$ReturnValue'

$ :i = 0
@ MyArrayForEachLoop | ? {:i} < {$ReturnValue}

  - .
    Array Value: <b>{$Array::{:i}}</>

$ :i += 1
-> MyArrayForEachLoop

# Map

The following loop shows the key-value pair of all elements in a map.

$ MyMapVar			               // Custom function: Store the Map in a variable called '$Map'
$ MyMapKeys		                   // Custom function: Store the Map keys in a variable called '$Keys'
$ ^Quillscript.Tools.Length {$Map} // Store the length in a variable called '$ReturnValue'

$ :i = 0
@ MyMapForEachLoop | ? {:i} < {$ReturnValue}

  - .
    Map Key-Value Pair: <b>{$Keys::{:i}} = {$Map::{$Keys::{:i}}}</>

$ :i += 1
-> MyMapForEachLoop