This wiki has been moved to into the mediawiki branch.

Scripting reference

From SuperTux
Revision as of 04:22, 29 January 2006 by RavuAlHemio (Talk | contribs)

Jump to: navigation, search

Since May 2005, SuperTux sports a Squirrel scripting interface useful for level designers who want to add some interactive pep to their levels. This document poses as a reference article for those who want to explore the various objects of the SuperTux scripting model.

What is Squirrel?

One of your first questions might be, "What does a rodent have to do with a penguin?" Squirrel is a language with a syntax not much unlike other C-like languages (C, C++, Java, ...). In the current implementation, it is integrated as elements in the SuperTux level files.

Squirrel, S-expr and SuperTux

I have no clue if the developers simply chose Squirrel just because the name so nicely integrates into the series of words "SuperTux" and "S-expr". Currently, the Squirrel code is integrated in string arguments of Scheme elements in SuperTux level files. (Whew.) This is an example code block inside a level:

  (version 2)
  (name (_ "Go Blind"))
  (author "Team")
    (name "main")
    (music "Annoying_penguin_gawking_sounds.ogg")

    ;; (Tilemaps, objects, whatever.)

    (init-script "

When this level loads, the screen fades out completely during two and a half seconds right after the level is loaded. (Mind you, this would be a frustrating experience for the player if you add a horde of badguys near the spawn point.)

Object reference

If you are interested in an object and what cans of worms you can open with it, this section is for you.

"(NYI)" after the function name symbolises functions that haven't been implemented yet. Calling them will result in a line being printed to standard output informing anybody who reads it that the script is using a function that actually doesn't exist. (You can expect Win32 users to happily ignore this, since they tend to simply start the application by opening it with Explorer. Unix users, on the other hand, like starting things from the console, so it's better if you don't use non-existent functions in your scripts.)

Global Constants

These constants can be accessed from anywhere in the game code.


Type: integer

Value: 1

Represents the brass key.

Used in: add_key as argument 1


Type: integer

Value: 2

Represents the iron key.

Used in: add_key as argument 1


Type: integer

Value: 4

Represents the bronze key.

Used in: add_key as argument 1


Type: integer

Value: 8

Represents the silver key.

Used in: add_key as argument 1


Type: integer

Value: 16

Represents the gold key.

Used in: add_key as argument 1

Global Functions

These global functions access basic or generic methods of SuperTux. They are called without an object name.


Usage: display_text_file(string filename)

Displays the SuperTux text file named filename. (The path is relative to the level file.)

See also: SuperTux file format reference, SuperTux texts


Usage: wait(float time)

Pauses execution of the squirrel code for time seconds.


Usage: translate(string text)

Returns: Translated string

Translates text into the user's locale.

Note: This construct is unfortunately not yet recognised by XGetText, so translation files have to be written manually.


Usage: import(string filename)

Imports and runs the Squirrel script filename. (The path is relative to the level file.)


Usage: add_key(int key)

Adds or removes a key from the player's posession. key should be replaced with one of the KEY_ constants.

See also: src/player_status.hpp


DisplayEffect is an interface for toying with the display.


Usage: DisplayEffect.fade_out(float fadetime)

Gradually fades out the screen to black for the next fadetime seconds.


Usage: DisplayEffect.fade_in(float fadetime)

Gradually fades in the screen from black for the next fadetime seconds.


Usage: DisplayEffect.set_black(bool black)

Blackens or un-blackens the screen (depending on the value of black).


Usage: DisplayEffect.is_black()

Returns: bool

Returns true if the screen has been blackened by set_black. Calling fade_in or fade_out resets the return value to false.


Usage: DisplayEffect.sixteen_to_nine()

Sets the display ratio to 16:9, effectively adding black bars at the top and bottom of the screen. Should be used before cutscenes.


Usage: DisplayEffect.four_to_three()

Sets the display ratio to 4:3, removing the black bars added by sixteen_to_nine(). Should be used after cutscenes.


Camera is an interface to manipulate the camera.

shake (NYI)

Usage: Camera.shake(float time, float x, float y)

Warning: This function has not yet been implemented.

set_pos (NYI)

Usage: Camera.set_pos(float x, float y)

Warning: This function has not yet been implemented.

set_mode (NYI)

Usage: Camera.set_mode(string modestring)

Warning: This function has not yet been implemented.


The Level class provides basic controlling functions for the current level.


Usage: Level.finish()

Ends the current level and marks it as completed if launched from a worldmap.

Tip: Very useful if you have created a frustrating level and want to, at some point, save the player from agony.


Usage: Level.spawn(string sectorname, string spawnpointname)

Respawns Tux in the sector sectorname at the spawnpointname spawnpoint.

Exceptions: If sectorname or spawnpointname are empty or the specified sector does not exist, the function will bail out first chance it gets. If the specified spawnpoint doesn't exist, Tux will be spawned at the spawnpoint named main. If this spawnpoint doesn't exist either, Tux will simply end up at the origin (top-left 0, 0).


Usage: Level.flip_vertically()

Flips the level vertically (i.e. top is now bottom and vice versa). Call again to revert the effect.

Tip: Make sure the player can land on something after the level is flipped!


A ScriptedObject is basically a SuperTux object that can be scripted to move around and animate. This object will be used in the SuperTux cutscenes a whole lot.

Usage notes

Since a ScriptedObject is a reference object and not a statically declared object, you will have to build it into your level file, where a scriptedobject is a child of sector. This is an example definition:

  (name "WOOT")
  (x 420)
  (y 94)
  (sprite "snowball")
  (solid #t)
  (physic-enabled #f)
  (visible #t)

Now, the object can be accessed in code using the WOOT identifier like so:



Usage: <scriptedobject>.set_animation(string animation_name)

Activates the sprite's animation specified in animation_name.


Usage: <scriptedobject>.get_animation()

Returns: string

Returns the name of the sprite's current animation.


Usage: <scriptedobject>.move(float x, float y)

Moves the object by x units to the right and y down relative to its current position.


Usage: <scriptedobject>.set_pos(float x, float y)

Basically identical to move, except its relativity to the sector origin.


Usage: <scriptedobject>.get_pos_x()

Returns: float

Returns the X coordinate of the object's position.


Usage: <scriptedobject>.get_pos_y()

Returns: float

Totally identical to get_pos_x except for its obvious choice of the other (Y) axis.


Usage: <scriptedobject>.set_velocity(float x, float y)

Makes the object move in a certain direction (with a certain speed) given by the x and y coordinates.


Usage: <scriptedobject>.get_velocity_x()

Returns: float

Returns the object's velocity in the direction of the X coordinate.


Usage: <scriptedobject>.get_velocity_y()

Returns: float

The difference between this function and get_velocity_x is just the same like between get_pos_y and get_pos_x: same thing, different coordinates.


Usage: <scriptedobject>.set_visible(bool visible)

Shows or hides the object according to the value of visible.


Usage: <scriptedobject>.is_visible()

Returns: bool

Returns true if the object is visible. (You've seen this coming, haven't you?)


Usage: <scriptedobject>.get_name()

Returns: string

Simply gives you the name of the scripted object (as if you didn't have it already...)


This class provides a very simple interface to the audio subsystem.


Usage: Sound.play_music(string track_name)

Plays the selected music track (automatically prepending the path to the music folder and appending the .ogg extension).


Usage: Sound.play_sound(string sound_name)

Plays the sound specified in sound_name (that is identical to the filename of the sound without the .wav extension).


This module provides access to methods reponsible for displaying text on-screen.


Usage: Text.set_text(string text)

Sets the text string to be displayed to text.


Usage: Text.set_font(string font)

Sets the font of the text to be displayed to text. Currently valid values are gold, white, blue, gray, big and small.


Usage: Text.fade_in(float time)

Fades in the specified text for the next time seconds.


Usage: Text.fade_out(float time)

Just the opposite of fade_out.


Usage: Text.set_visible(bool visible)

Shows or hides the text abruptly (drastic counterpart to fade_in and fade_out).


Usage: Text.set_centered(bool centered)

If centered is true, the text will be centered on the screen. Otherwise, it will be left-aligned.


This module contains methods controlling the player. (No, SuperTux doesn't use mind control. Player refers to the type of the player object.)

Once again, just like with ScriptedObject, note that Player is only the object type. Since SuperTux is currently single-player only, use Tux to make changes to the player object, such as Tux.deactivate().


Usage: <player>.make_invincible()

Makes the player invincible for a predefined amount of time.

See also: TUX_INVINCIBLE_TIME in src/object/player.hpp for the amount of seconds that the player becomes invincible.


Usage: <player>.deactivate()

Stops the player and blocks the movement controls.

Tip: Don't call this in front of a horde of badguys.


Usage: <player>.activate()

Reactivates the player's movement controls.


Usage: <player>.walk(float velocity)

Makes the player move in a certain horizontal velocity (specified by velocity). A negative velocity moves the player to the left.