Skip to content

# Lua API Reference

## Overview

This is the API specification of Lua functions, methods, callbacks and types defined by Solarus. This documentation page is intented to quest makers who want to write scripts for their maps, items, enemies and menus.

For the point of view of the C++ code of the engine, see the documentation of class `LuaContext`.

Most of the data types defined in the C++ engine (like sprites, map entities, movements, savegames, etc.) are exported as Lua types in the scripting API of Solarus. We give here the full reference of these types and the functions available for each type.

The API exports C++ functions and C++ datatypes that may be used by your Lua scripts. Examples of such features are creating a sprite, drawing an image or moving an enemy.

In the opposite way, Solarus will also call your own Lua functions (if you define them), for example to notify your script that an enemy has reached an obstacle, that a pressure plate has just been activated or that the hero is talking to a particular non-playing character.

The following script files are loaded by the engine when they exist:

Script | Role
--- | ---
`main.lua` | Global script that controls the menus (if any) before starting a game, and then decides to start a game.
`maps/XXXX.lua` | The script of a map controls the map `XXXX`.Called when the player enters the map.
`enemies/XXXX.lua` | The script of an enemy controls an enemy whose breed is `XXXX`.Called when an enemy of this breed is created on the map.
`items/XXXX.lua` | The script of an item : controls the equipment item named `XXXX`.Called when a savegame is loaded or created.

All these various scripts run in the same Lua state. In other words, they share global values.

Interactions between your Lua world and the engine are managed through a predefined global table called `sol`. The whole Solarus Lua API is available in the `sol` table. It contains functions, types and values that allow you to interact with the C++ engine.

Most types of the Lua API (like game, item, map, entity, movement and sprite) are Lua userdata that have something special: they can also be indexed like tables. This mechanism is used by the engine when it needs to invoke callback methods that you defined on your objects.

But you can also extend these objects with your own functions and data. This is very useful in the game and map objects to implement and store everything that is not built-in in the Solarus API: your pause menu, your HUD, a puzzle, or some properties and utility functions specific to your quest.

## Features

The following features are defined in the global `sol` table. See the specification page of each feature for more details.

API | Role
--- | ---
`sol.main` | General-purpose features.
`sol.audio` | Playing musics and managing sounds.
`sol.music` | Playing musics.
`sol.sound` | Playing sound effects.
`sol.video` | Changing video settings.
`sol.shader` | Modifying the rendering.
`sol.controls` | Binding low-level input to game commands.
`sol.input` | Checking keyboard and joypad state.
`sol.joypad` | Handling joypads.
`sol.file` | Directly accessing data files.
`sol.menu` | Showing various information on the screen.
`sol.language` | Handling translations.
`sol.timer` | Making an action later with a delay.
`sol.sprite` | Displaying animated images.
`sol.surface` | Displaying fixed images.
`sol.text_surface` | Displaying text.
`sol.movement` | Moving objects.
`sol.game` | Handling data saved (life, equipment, etc.) and running a game.
`sol.item` | Controlling a specific type of equipment item and its behavior.
`sol.map` | Handling the current map and its properties (only during a game).
`sol.entity` | Managing entities placed on the map (only during a game), like the hero, enemies, chests, non-playing characters, etc.
`sol.state` | Customizing the behavior of the hero entity.

April 1, 2025