Cubescript Tutorial Chapter 2
Chapter 2: Simple Scripts
2.1 Your first and second zoom scripts (and "if", too!)
Almost everyone's first script is a zoom script. Sauerbraten already comes with one, but it's kinda hard to use (hold "G" while scrolling with the mouse, then scroll out to zoom out). Here it is:
defaultmodifier = 0 modifier = $defaultmodifier domodifier = [ modifier = $arg1; onrelease [ modifier = $defaultmodifier ] zfov = 120 delta_game_1 = [ zfov = ( + $zfov ( * $arg1 ( div $zfov -5 ) ) ) if ( < $zfov 10 ) [ zfov = 10 ] if ( > $zfov 120 ) [ zfov = 120 ] fov $zfov ] bind G [ domodifier 1 ]
Technically, in Cube 1, you can’t zoom. You only decrease the field of vision (fov), which give the effect of zooming in, and allows you to see much finer details. The script I commonly use was written by rootnis, and it allows you to zoom in while you hold down MOUSE2, but stop any time; the next click, you return to normal zoom. Your first will be much simpler, of course.
Open myfirstscript.cfg. Now you need a name for your script. How about “zoom”? To name an alias (a name that allows easier script execution), you can use two methods:
alias zoom [ ] zoom = [ ]
The first one is the original way, so you’ll see it used a lot. The second is newer, but a lot easier to use: we'll use this. (Make sure to use spaces between the name, the equals sign, and the brackets that contain the script.) Now that you’ve got a name, let’s find a way to decrease your fov. The simplest way is to zoom all the way in when you click the button:
zoom = [ fov 50 ]
Okay, there’s a zoom script. Problem is, you can't zoom out. Try the command "onrelease", which will execute whatever you tell it to when you release the button.
zoom = [ fov 50 onrelease [ fov 100 ] ]
And here’s that as a one-liner:
zoom = [ fov 50; onrelease [ fov 100 ] ]
Now let's try one that’s a little more complex. Let's say you want to click to zoom in, and then click to zoom out. A touchy way to do this is (if you want the zoom bound to MOUSE2):
zoom1 = [ fov 50 bind "MOUSE2" [ zoom2 ] ] zoom2 = [ fov 100 bind "MOUSE2" [ zoom1 ] ] bind "MOUSE2" [ zoom1 ]
So many problems arise with this one that it’s impossible to use. Plus, you never (except in rare cases) want to bind anything inside a script. So it must be time to introduce “if”.
"if" is a neccessary part of many, many scripts. The sooner you learn to use it, the better. Here's how if's used, with an explanation:
if ( = $zoomvar 1 ) [ ]
First comes "if" itself, declaring this to be an if-then statement. Then we have parentheses enclosing some wierd math equation. First comes the equals? CubeScript has some odd syntax. These statements are comparing the value of a script/variable (these are the same thing; script names are just variables with scripts as their values) called "zoomvar" and 1, to see if they equal each other. (Note: remember to put $ before any variables in the statement.) If they do, then the following [script] will execute; if not, the script will pass on to the next part (if there is any).
Now to add this to your zoom script. (As your scripts get more complex, you’ll want to move away from using single-liners, and start formatting them like the following. If you use this style, you don’t need “;” between individual commands, only start them on a new line):
zoom = [ if ( = $zoomvar 1 ) [ fov 50 zoomvar = 0 //sets zoomvar to 0 ] if ( = $zoomvar 0 ) [ fov 100 zoomvar = 1 // Resets zoomvar to 1 ] ]
Looks complicated, huh? Look closer, and see if you can understand it all. It's all simple stuff you’ve already learned, combined. Now all you have to do is find a way to execute it.
2.2 Executing scripts
To execute your script, Sauerbraten needs a way to find it. So far, you’ve saved your zoom script to myfirstscript.cfg, but the game doesn’t know this. Here’s where autoexec.cfg comes into play again. Place the line
into your autoexec. Now add your bind (somewhere after the exec line: Sauerbraten always has to know a script’s name before it can use it in another script or bind):
bind "MOUSE2" [zoom]
So fire up Sauerbraten, and try it out. If it doesn’t work, make sure you’ve typed it all correctly (no missing “]”s, etc.). If it still doesn't work, remember the common solution to most problems: delete config.cfg, and open Sauerbraten again.
Now that you’ve executed it with a bind, let’s try the command line. Some commands don’t necessarily need to be bound, such as loading a map. Try out your zoom script; open sauerbraten and press /. This make a little arrow (>) near the bottom of your screen, with a blinking "_" after it. The "_" is your cursor; just type
so it looks like
and press Enter. Your script executes. Now type it again, and hit Enter. You zoom out. Simple. (Remember to type "/" in front of all commands you enter in the console, otherwise they just appear as a chat message.) It's just as easy to load a map:
will load the map of your choice. Try "/map corruption", which loads "Corruption" by Kaiser.