This wiki has been moved to into the mediawiki branch.

Scripting reference

From SuperTux
Revision as of 07:06, 15 February 2007 by AnMasterBot (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 and worldmap files. The Console is a full-fledged Squirrel interpreter as well.

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.


Globals This section contains all globally defined constants and functions
Player An object that is controlled by a (human) player
DisplayEffect An object that does graphical effects on the whole screen
Camera ...
Level ...
ScriptedObject Object in a level than can be manipulated from scripts
Sound ...
Text ...
FloatingImage ...
Platform Scripting interface to Platform object
Wind Scripting interface to Wind object
Candle Scripting interface to Candle object
Thunderstorm Scripting interface to Thunderstorm object
AmbientSound Scripting interface to AmbientSound object

TODO: Convert the rest of the scripting docu to the new format (and improve format maybe?)

Global Functions

These global functions access basic or generic methods of SuperTux. They are called without an object name. TODO: Move these into the globals section above.


Usage: display(*** object)

Displays the string value of object in the Console. Object can be of any data type.


Usage: print_stacktrace()

Displays contents of the current stack.


Usage: get_current_thread()

Returns the currently running thread.


Usage: display_text_file(string filename)

Displays the SuperTux text file named filename. (The path is relative to the data root, e.g. "/home/joe/src/supertux-trunk/data")

See also: SuperTux file format reference, SuperTux texts


Usage: load_worldmap(string filename)

Loads and runs the worldmap specified in filename. (The path is relative to the data root.)


Usage: load_level(string filename)

Loads and runs the level specified in filename. (The path is relative to the data root.)


Usage: wait(float time)

Pauses execution of the Squirrel code for time seconds.


Usage: wait_for_screenswitch()

Pauses execution of the Squirrel code until a new screen is displayed (e.g. menu → worldmap or worldmap → level).


Usage: exit_screen()

Exits the current screen, returning to the previous one or, if the active screen is the last one, exiting SuperTux.


Usage: fadeout_screen(float seconds)

Does a fadeout for the specified number of seconds before next screenchange.


Usage: shrink_screen(float dest_x, float dest_y, float seconds)

Does a shrinking fade towards the destposition for the specified number of seconds before next screenchange


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 data root.)


Usage: save_status()

Dumps the current state into the user's save game file.


Usage: play_music(string musicfile)

Changes music to musicfile


Usage: play_sound(string soundfile)

Plays a soundfile


The FloatingImage class contains methods for handling an image floating in mid-air, such as the SuperTux logo.


Usage: <floatimage> <- FloatingImage(string filename)

Creates a FloatingImage from filename (which is relative to the data root).


Usage: <floatimage>.set_layer(int layer)

Puts the image on layer.

See also: src/video/drawing_context.hpp for the predefined layers and their values (note that you can't use these constants in your Squirrel code).


Usage: <floatimage>.get_layer()

Returns: int; the layer the floating image is on.


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

Move the image in relation to the current anchor point.


Usage: <floatimage>.get_pos_x()

Get the image's X coordinate relative to the current anchor point.


Usage: <floatimage>.get_pos_y()

Get the image's Y coordinate relative to the current anchor point.


Usage: <floatimage>.set_anchor_point(int anchorpt)

Set the image's anchor point. Possible values are represented by the ANCHOR_* constants.


Usage: <floatimage>.get_anchor_point()

Returns: int; the current anchor point


Usage: <floatimage>.set_visible(bool visible)

Set the image visibility according to visible.


Usage: <floatimage>.get_visible()

Returns: bool; is the image visible?


Usage: <floatimage>.set_action(string action)

Set current action.


Usage: <floatimage>.get_action()

Returns: string; current action.