Cubescript Tutorial Chapter 4

From Cube Wiki
Revision as of 17:59, 12 December 2014 by Admin (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

CubeScript Tutorial

Chapter 4: More Valuable Information

4.1 arg1!

Each value after a command is called an argument, “arg” for short. Many commands in Sauerbraten have only one argument; but some have several. How can you give this power to your own commands (i.e. zoom)? Try this out for size:

emergency = [
    say We have $arg1 enemy attacking $arg2.
    say Send $arg3 men immediately!
]

arg1”, “arg2”, and “arg3” are three (that’s more than two but less than four) arguments following the new command “emergency”. If you type

/emergency [ many ] [ base 1 ] [ 5 ]

The following message will be sent out to all players:

We have many enemy attacking base 1. Send 5 men immediately!

Cool, huh? (Yes, there is a script you can write to check and correct your grammar/spelling. No, it will never be written.)

A note about “arg” before we move on. “$arg” can only be used directly inside its parent script, as show. If you place it within [ ] inside of the script, use “@arg” instead. If you need a further [ ] inside the [ ], use “@@arg”, etc..:

example = [ $arg1 [ @arg2 [ @@arg3 [ @@@arg4 ] ] ] ]

4.2 if ( = $continuing 1 ) [ proceed ] [ return_to 2.1 ]

For the past few sections, you’ve been using “if” statements that only do something if a value equals X. “if” statements are a lot more flexible, though. What if you wanted to do D if “zoomvar” equals one, but N if it doesn’t? Simply place the second part after the first:

if (= $zoomvar 1) [echo zoomvar is 1] [echo zoomvar is not 1]

This will echo

zoomvar is one

if "zoomvar" is one, and

zoomvar is not 1

otherwise. Another neat thing to do with “if” statements is non-equals comparisions (ie greater or less than):

if (> $zoomvar 1) [echo zoomvar is too big!]
if (< $zoomvar 1) [echo zoomvar is too small!]
if (= $zoomvar 1) [echo zoomvar is just right!] //jff...

To figure out which direction to place the compare symbols, just think (X > Y), then just move the symbol to the front. Or just use

($zoomvar > 1)

Wait, there’s more! The two math equations 3-1 and 1+1 both equal 2, but they’re not the same. Cube can tell the difference between the contents of two scripts with “strcmp” (string compare):

if (strcmp $zoom $rootzoom) [echo you copied rootnis’ zoom] [echo you did not copy rootnis’ zoom]

Then there are the “existence” comparisons.

if ($zoom) [echo the script zoom exists]

if (! $zoom) [echo zoom does not exist]

if (&& $zoom $rootzoom) [echo both zoom and rootzoom exist]

if (|| $zoom $rootzoom) [echo either zoom or rootzoom exists]

if (^ $zoom $rootzoom) [echo either zoom or rootzoom does not exist]

Those should be pretty self-explanitory.

Now, this has only a little to do with “if” statements, but see the part in parenthese ()? Those are all the equations you can perform with “if”, but you can also add, subtract, etc.:

(+ 2 2) //add

(- 2 1) //subtract

(* 2 2) //multiply

(div 4 2) //divide

(mod 17 5) //finds the remainder after the two numbers are divided

Very similar to “if” is “while”, which is very useful. We’ll get to that later.

4.3 Your fourth zoom script

(still debugging the example script)