Since Tiled 1.3

Scripting

Introduction

Tiled can be extended with the use of JavaScript. See the Tiled Scripting API for a reference of all available functionality.

TypeScript definitions of the API are available as the @mapeditor/tiled-api NPM package, which can provide auto-completion in your editor. The API reference is generated based on these definitions.

On startup, Tiled will execute any script files present in extension folders. In addition it is possible to run scripts directly from the console. All scripts share a single JavaScript context.

Note

A few example scripts and links to existing Tiled extensions are provided at the Tiled Extensions repository: https://github.com/mapeditor/tiled-extensions

Warning

Most builds of Tiled use Qt 5.12 or later, which support ECMAScript 7. However, the builds for Windows XP and the snap release of Tiled currently rely on Qt 5.6 and Qt 5.9 respectively. ECMAScript 7 features will not work there and some other functionality is missing as well, like interrupting scripts on API usage errors and the creation of new Tiled-specific objects.

Scripted Extensions

Extensions can be placed in a system-specific or project-specific location.

The system-specific folder can be opened from the Plugins tab in the Preferences dialog. The usual location on each supported platform is as follows:

Windows
C:/Users/<USER>/AppData/Local/Tiled/extensions/
macOS
~/Library/Preferences/Tiled/extensions/
Linux
~/.config/tiled/extensions/

The project-specific folder defaults to “extensions”, relative to the directory of the .tiled-project file, but this can be changed in the Project Properties.

An extension can be placed either directly in an extensions directory, or in a sub-directory. All scripts files found in these directories are executed on startup.

When any loaded script is changed or when any files are added/removed from the extensions directory, the script engine is automatically reinstantiated and the scripts are reloaded. This way there is no need to restart Tiled when installing extensions. It also makes it quick to iterate on a script until it works as intended.

Apart from scripts, extensions can include images that can be used as the icon for scripted actions or tools.

Console View

In the Console view (View > Views and Toolbars > Console) you will find a text entry where you can write or paste scripts to evaluate them.

You can use the Up/Down keys to navigate through previously entered script expressions.

API Reference

See the Tiled Scripting API.

The following global variable is currently not documented in the generated documentation, since it conflicts with nodejs types:

__filename
The file path of the current file being evaluated. Only available during initial evaluation of the file and not when later functions in that file get called. If you need it there, copy the value to local scope.