This wiki has been moved to https://github.com/SuperTux/wiki into the mediawiki branch.

Scripting reference

From SuperTux
Revision as of 05:18, 25 June 2007 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 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:

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

    ;; (Tilemaps, objects, whatever.)

    (init-script "
DisplayEffect.fade_out(2.5);
")
  )
)

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.

Classes

Classes
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 Control over the camera (what the player sees)
Level Control over the status the game keeps about the current level.
ScriptedObject Object in a level than can be manipulated from scripts
Text Display text in the top-center of the screen
FloatingImage Display an image floating in front of anything on-screen
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.

display

Usage: display(*** object)

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

print_stacktrace

Usage: print_stacktrace()

Displays contents of the current stack.

get_current_thread

Usage: get_current_thread()

Returns the currently running thread.

display_text_file

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

load_worldmap

Usage: load_worldmap(string filename)

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

load_level

Usage: load_level(string filename)

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

wait

Usage: wait(float time)

Pauses execution of the Squirrel code for time seconds.

wait_for_screenswitch

Usage: wait_for_screenswitch()

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

exit_screen

Usage: exit_screen()

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

fadeout_screen

Usage: fadeout_screen(float seconds)

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

shrink_screen

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

translate

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.

import

Usage: import(string filename)

Imports and runs the Squirrel script filename. (The path is relative to the data root.)

save_status

Usage: save_status()

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

play_music

Usage: play_music(string musicfile)

Changes music to musicfile

play_sound

Usage: play_sound(string soundfile)

Plays a soundfile

FloatingImage

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

Constructor

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

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

set_layer

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

get_layer

Usage: <floatimage>.get_layer()

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

set_pos

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

Move the image in relation to the current anchor point.

get_pos_x

Usage: <floatimage>.get_pos_x()

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

get_pos_y

Usage: <floatimage>.get_pos_y()

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

set_anchor_point

Usage: <floatimage>.set_anchor_point(int anchorpt)

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

get_anchor_point

Usage: <floatimage>.get_anchor_point()

Returns: int; the current anchor point

set_visible

Usage: <floatimage>.set_visible(bool visible)

Set the image visibility according to visible.

get_visible

Usage: <floatimage>.get_visible()

Returns: bool; is the image visible?

set_action

Usage: <floatimage>.set_action(string action)

Set current action.

get_action

Usage: <floatimage>.get_action()

Returns: string; current action.