Scripting Flow Control

From Cube Wiki
Revision as of 16:59, 12 December 2014 by Admin (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

CubeScript control flow

Any more complex scripting than simply binding static commands to keys or defining the most simple of menus will require your script to change it's runtime behaviour in response to the state of the game. This is called control flow, the classic possibilities available in most languages also exist in CubeScript.

mathematical operations

+ A B
- A B
* A B
div A B
mod A B

(add, substract, multiply, divide, modulo): these all work like the integer operators from other languages. Example:

echo x squared is (* $x $x)

value comparison

= A B
< A B
> A B
strcmp A B

(equals, lessthan, greaterthan, stringcompare): comparison operators that return 1 for true and 0 for false.

value passing

result V

Normally the result of a [] block is the result of the last command in the block. If you want the result to be a particular variable or value, you can use e.g. "result $i" etc.

forks in execution

if cond true false

executes the true or false part depending on wether cond is "0" or something else. Example:

if (< $x 10) [ echo "x is" $x ] [ echo "x is too big" ]

repetition

loop I N body

evaluates body N times, and sets the alias I from 0 to N-1 for every iteration. Example:

loop i 10 [ echo $i ]
while cond body

evaluates body while cond evaluates to true. Note that cond here has to have [], otherwise it would only be evaluated once. Example (same result as the "loop" example):

i = 0; while [ (< $i 10) ] [ echo $i; i = (+ $i 1) ]

string manipulation

concat S...

concatenates all the arguments and returns the result

concatword S...

same as concat but without spaces between the elements.

format F V1..Vn

substitutes the V values into the format string F and returns the result. The format strings %1 through %9 are substituted with V1 through V9, respectively, and may be used multiple times. %% will generate a single % character. Example:

format "%1 bottles of %2 on the %3, %1 bottles of %2!" 99 beer wall
at S N

grabs the Nth word out of string S and returns the result

listlen L

returns the number of items in the list L

game state specifics

onrelease A

only executes A if the command is executed on the release of a key/button (must be in an action in a bind or an alias in a bind).

paused B

wether the game is paused or not (default 0, default key F1 toggles).

$editing

true when in edit mode