Difference between revisions of "Scripting Syntax"

From Cube Wiki
Jump to: navigation, search
 
(No difference)

Latest revision as of 16:59, 12 December 2014

The basic command structure

Commands consist of the command itself, followed by any number of arguments separated by white space. You can use "" to quote strings with white space in them (such as the actions in bind/alias), and wherever a command is required you can also use ; to sequence multiple commands in one.

You can evaluate aliases and expressions. You can substitute the value of an alias as an argument by prefixing it with a "$" sign, i.e.:

echo "The current value of x is $x"

You can even substitute the values of console variables this way, i.e $fov gives the current FOV value. Some values though will not be returned in this way, noteably name and team. A solution for this is to work with buffer aliases to later access the value when needed. Some aliases are set automatically, for example $arg1 to $argN are set if you supply arguments when you execute an alias. But beware, these should not be tested for emptiness since they may contain values of preceding calls of other aliases!

There are two alternatives to "" for quoting a string: () and []. They work in the same way as "", with the difference that they can be nested infinitely, and that they may contain line feeds (useful for larger scripts). () is different from [] in that it evaluates the commands contained in it before it evaluates the surrounding command, and substitutes the results. () bracketed strings are called expressions, and [] bracketed strings may be thought of as blocks.

An alternative to $x is @x, which uses an alias as a macro. The difference is that @x can be subtituted inside [] or () forms before they have ever been evaluated (at parse time), which makes them useful for composing strings or creating code on the fly. The @x form will be substituted using the value of x at the time the enclosing [] is evaluated. You can add more @ prefixes to move up more levels of []s, so @@x will move up two levels of []s and so on. Example:

x = 0; if $cond [x = 1; [x = 2; echo @@x]]

will echo 0, since it uses the value of x at two levels up.

The form @(body) is similar to @x, except that body contains commands executed at parse time. The result value after body executes is substituted in for @(body). You may use multiple @ prefixes as with the @x form. Example:

@(result "Hello, World!")

will substitute itself with Hello, World!

An overview of especially useful commands can be found on the Scripting Commands page. The possibilities of flow control for CubeScript are listed on the Scripting Flow Control page.