http://cubeengine.com/w/index.php?title=Scripting_Flow_Control&feed=atom&action=historyScripting Flow Control - Revision history2024-03-29T15:00:03ZRevision history for this page on the wikiMediaWiki 1.31.7http://cubeengine.com/w/index.php?title=Scripting_Flow_Control&diff=171&oldid=prevAdmin at 15:59, 12 December 20142014-12-12T15:59:31Z<p></p>
<p><b>New page</b></p><div>=CubeScript control flow=<br />
<br />
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.<br />
<br />
==mathematical operations==<br />
<pre><br />
+ A B<br />
- A B<br />
* A B<br />
div A B<br />
mod A B<br />
</pre><br />
(add, substract, multiply, divide, modulo): these all work like the integer operators from other languages. Example:<br />
<pre><br />
echo x squared is (* $x $x)<br />
</pre><br />
<br />
==value comparison==<br />
<pre><br />
= A B<br />
< A B<br />
> A B<br />
strcmp A B<br />
</pre><br />
(equals, lessthan, greaterthan, stringcompare): comparison operators that return 1 for true and 0 for false.<br />
<br />
==value passing==<br />
<pre><br />
result V<br />
</pre><br />
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.<br />
<br />
==forks in execution==<br />
<pre><br />
if cond true false<br />
</pre><br />
executes the true or false part depending on wether cond is "0" or something else. Example:<br />
<pre><br />
if (< $x 10) [ echo "x is" $x ] [ echo "x is too big" ]<br />
</pre><br />
==repetition==<br />
<pre><br />
loop I N body<br />
</pre><br />
evaluates body N times, and sets the alias I from 0 to N-1 for every iteration. Example:<br />
<pre><br />
loop i 10 [ echo $i ]<br />
</pre><br />
<br />
<pre><br />
while cond body<br />
</pre><br />
evaluates body while cond evaluates to true. Note that cond here has to have [], otherwise it would only be evaluated once.<br />
Example (same result as the "loop" example):<br />
<pre><br />
i = 0; while [ (< $i 10) ] [ echo $i; i = (+ $i 1) ]<br />
</pre><br />
==string manipulation==<br />
<pre><br />
concat S...<br />
</pre><br />
concatenates all the arguments and returns the result<br />
<br />
<pre><br />
concatword S...<br />
</pre><br />
same as concat but without spaces between the elements.<br />
<br />
<pre><br />
format F V1..Vn<br />
</pre><br />
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:<br />
<pre><br />
format "%1 bottles of %2 on the %3, %1 bottles of %2!" 99 beer wall<br />
</pre><br />
<br />
<pre><br />
at S N<br />
</pre><br />
grabs the Nth word out of string S and returns the result<br />
<br />
<pre><br />
listlen L<br />
</pre><br />
returns the number of items in the list L<br />
<br />
==game state specifics==<br />
<pre><br />
onrelease A<br />
</pre><br />
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).<br />
<br />
<pre><br />
paused B<br />
</pre><br />
wether the game is paused or not (default 0, default key F1 toggles).<br />
<br />
<pre><br />
$editing<br />
</pre><br />
true when in edit mode</div>Admin