Options
All
  • Public
  • Public/Protected
  • All
Menu

Namespace tiled

The tiled module is the main entry point and provides properties, functions and signals which are documented below.

Index

Variables

Const actions

actions: string[]

Available actions for tiled.trigger().

Let activeAsset

activeAsset: Asset | null

Currently selected asset, or null if no file is open. Can be assigned any open asset in order to change the active asset, or a newly created asset to open it in the editor.

Const activeAssetChanged

activeAssetChanged: Signal<Asset>

The activeAsset has changed.

Const applicationDirPath

applicationDirPath: string

The directory containing the Tiled executable.

since

1.8

Const arch

arch: string

Processor architecture. One of x64, x86 or unknown.

Const assetAboutToBeClosed

assetAboutToBeClosed: Signal<Asset>

An asset is about to be closed.

Const assetAboutToBeSaved

assetAboutToBeSaved: Signal<Asset>

An asset is about to be saved. Can be used to make last-minute changes.

Const assetCreated

assetCreated: Signal<Asset>

A new asset has been created.

Const assetOpened

assetOpened: Signal<Asset>

An asset has been opened.

Const assetSaved

assetSaved: Signal<Asset>

An asset has been saved.

Const extensionsPath

extensionsPath: string

The system- and user-specific path where global extensions are installed.

Note that normally it is not necessary to use this path, since the "ext:" prefix can be used to refer to files shipping with extensions. Also, there is a __filename property containing the full file path of the currently evaluated file.

Also note that a Tiled project can have its own additional extensions directory, to make it easier to share extensions with a team or keep them under version control.

since

1.8

Const mapEditor

mapEditor: MapEditor

Access the editor used when editing maps.

Const mapFormats

mapFormats: string[]

List of supported map format names. Use mapFormat to get the corresponding format object to read and write files.

since

1.4

Const menus

menus: string[]

Available menus for tiled.extendMenu().

Const openAssets

openAssets: Asset[]

List of currently opened assets.

Const platform

platform: string

Operating system. One of windows, macos, linux or unix (for any other UNIX-like system).

Const project

project: Project

Currently opened project. If no project is open, the properties of the project will be blank.

since

1.10.1

Const projectFilePath

projectFilePath: string

The file path of the currently loaded project, or empty if no project is currently loaded.

since

1.10

Const qtVersion

qtVersion: string

The version of Qt which Tiled is running against.

since

1.8.5

Const scriptArguments

scriptArguments: string[]

A list of arguments passed to a script that is evaluated from the command-line.

Scripts can be executed on the command-line using tiled --evaluate <script-file> [args].

since

1.9

Const tilesetEditor

tilesetEditor: TilesetEditor

Access the editor used when editing tilesets.

Const tilesetFormats

tilesetFormats: string[]

List of supported tileset format names. Use tilesetFormat to get the corresponding format object to read and write files.

since

1.4

Const version

version: string

Currently used version of Tiled.

Functions

alert

  • alert(text: string, title?: string): void
  • Shows a modal warning dialog to the user with the given text and optional title.

close

  • close(asset: Asset): boolean
  • Closes the given asset without checking for unsaved changes (to confirm the loss of any unsaved changes, set activeAsset and trigger the "Close" action instead).

compress

  • compress(data: ArrayBuffer | string, method?: CompressionMethod, compressionLevel?: number): ArrayBuffer
  • Compresses the given data using the given compression method and compression level.

    When no compression method is given, defaults to Zlib compression. The compression level defaults to the default for the respective method.

    example

    Example that compresses data using Gzip compression:

    buffer = tiled.compress(buffer, Tiled.Gzip);
    
    since

    1.10

confirm

  • confirm(text: string, title?: string): boolean
  • Shows a yes/no dialog to the user with the given text and optional title. Returns true or false.

decompress

  • Decompresses the given data using the given compression method.

    When no compression method is given, defaults to Zlib compression.

    since

    1.10

error

  • error(text: string, activated: () => void): void
  • Outputs the given text in the Console window as error message and creates an issue in the Issues window.

    When the issue is activated (with double-click or Enter key) the given callback function is invoked.

executeCommand

  • executeCommand(name: string, inTerminal: boolean): void
  • Executes the first custom command with the given name, as if it was triggered manually. Works also with commands that are not currently enabled.

    Raises a script error if the command is not found.

    For more control over the executed binary, use Process instead.

extendMenu

  • extendMenu(shortName: string, menu: MenuItem[]): void
  • Extends the menu with the given ID. Supports both a list of items or a single item. Available menu IDs can be obtained using the tiled.menus property.

    If a menu item does not include a before property, the value is inherited from the previous item. When this property is not set at all, the items are appended to the end of the menu.

    Example that adds a custom action to the "Edit" menu, before the "Select All" action and separated by a separator:

    tiled.extendMenu("Edit", [
        { action: "CustomAction", before: "SelectAll" },
        { separator: true }
    ]);
    

    The "CustomAction" will need to have been registered before using tiled.registerAction().

filePath

  • Creates a FilePath object with the given URL.

log

  • log(text: string): void
  • Outputs the given text in the Console window as regular text.

mapFormat

  • mapFormat(shortName: string): MapFormat | undefined
  • Returns the map format object with the given name, or undefined if no object was found. See the mapFormats property for more info.

mapFormatForFile

  • mapFormatForFile(fileName: string): MapFormat | undefined
  • Returns the map format object that can read the given file, or undefined if no object was found.

objectRef

  • Creates an ObjectRef object with the given ID.

open

  • open(fileName: string): Asset | null
  • Requests to open the asset with the given file name. Returns a reference to the opened asset, or null in case there was a problem.

prompt

  • prompt(label: string, text?: string, title?: string): string
  • Shows a dialog that asks the user to enter some text, along with the given label and optional title. The optional text parameter provides the initial value of the text. Returns the entered text.

promptDirectory

  • promptDirectory(defaultDir?: string, title?: string): string
  • Shows a dialog which asks the user to choose an existing directory. Optionally override the starting directory of the dialog or its title.

    Returns the absolute path of the chosen directory, or an empty string if the user cancels the dialog.

    since

    1.10.2

promptOpenFile

  • promptOpenFile(defaultDir?: string, filters?: FileFilter, title?: string): string
  • Shows a dialog which asks the user to choose an existing file. Optionally override the starting directory of the dialog or its title. You can also restrict to only certain file types by specifying filters.

    Returns the absolute path of the chosen file, or an empty string if the user cancels the dialog.

    since

    1.10.2

promptOpenFiles

  • promptOpenFiles(defaultDir?: string, filters?: FileFilter, title?: string): string[]
  • Shows a dialog which asks the user to choose one or more existing files. Optionally override the starting directory of the dialog or its title. You can also restrict to only certain file types by specifying filters.

    Returns an array of the absolute paths of the chosen files, or an empty array if the user cancels the dialog.

    since

    1.10.2

promptSaveFile

  • promptSaveFile(defaultDir?: string, filters?: string, title?: string): string
  • Shows a dialog which asks the user to choose a destination for saving a file. If the user chooses a file path which already exists, they will be asked to confirm that they want to overwrite the file. Optionally override the starting directory of the dialog or its title. You can also restrict to only certain file types by specifying filters.

    Returns the absolute path of the chosen file, or an empty string if the user cancels the dialog.

    since

    1.10.2

propertyValue

  • propertyValue(type: string, value: object | number | string): PropertyValue
  • Creates a PropertyValue object with the given type and value.

    When creating an enum value, the name of the value can be used (or string with comma-separated names, in case of using flags). However, the created PropertyValue object will always use numbers for enum values.

    example

    Creating a value of a custom enum:

    var bodyType = tiled.propertyValue("BodyType", "Dynamic")
    
    example

    Creating a value of a custom class:

    var body = tiled.propertyValue("Body", { type: bodyType, bullet: true })
    
    since

    1.8

registerAction

  • registerAction(id: string, callback: (action: Action) => void): Action
  • Registers a new action with the given id and callback (which is called when the action is triggered). The returned action object can be used to set (and update) various properties of the action.

    The shortcut will currently only work when the action is added to a menu using tiled.extendMenu().

    example
    var action = tiled.registerAction("CustomAction", function(action) {
        tiled.log(action.text + " was " + (action.checked ? "checked" : "unchecked"))
    })
    
    action.text = "My Custom Action"
    action.checkable = true
    action.shortcut = "Ctrl+K"
    

registerMapFormat

  • Registers a new map format that can then be used to open and/or save maps in that format.

    If a map format is already registered with the same shortName, the existing format is replaced. The short name can also be used to specify the format when using --export-map on the command-line, in case the file extension is ambiguous or a different one should be used.

    example

    Example that produces a simple JSON representation of a map:

    var customMapFormat = {
        name: "Custom map format",
        extension: "custom",
    
        write: function(map, fileName) {
            var m = {
                width: map.width,
                height: map.height,
                layers: []
            };
    
            for (var i = 0; i < map.layerCount; ++i) {
                var layer = map.layerAt(i);
                if (layer.isTileLayer) {
                    var rows = [];
                    for (y = 0; y < layer.height; ++y) {
                        var row = [];
                        for (x = 0; x < layer.width; ++x)
                            row.push(layer.cellAt(x, y).tileId);
                        rows.push(row);
                    }
                    m.layers.push(rows);
                }
            }
    
            var file = new TextFile(fileName, TextFile.WriteOnly);
            file.write(JSON.stringify(m));
            file.commit();
        },
    }
    
    tiled.registerMapFormat("custom", customMapFormat)
    

registerTilesetFormat

registerTool

  • Registers a custom tool that will become available on the Tools tool bar of the Map Editor.

    If a tool is already registered with the same shortName the existing tool is replaced.

    example

    Here is an example tool that places a rectangle each time the mouse has moved by 32 pixels:

    var tool = tiled.registerTool("PlaceRectangles", {
        name: "Place Rectangles",
    
        mouseMoved: function(x, y, modifiers) {
            if (!this.pressed)
                return
    
            var dx = Math.abs(this.x - x)
            var dy = Math.abs(this.y - y)
    
            this.distance += Math.sqrt(dx*dx + dy*dy)
            this.x = x
            this.y = y
    
            if (this.distance > 32) {
                var objectLayer = this.map.currentLayer
    
                if (objectLayer && objectLayer.isObjectLayer) {
                    var object = new MapObject(MapObject.Rectangle, ++this.counter)
                    object.x = Math.min(this.lastX, x)
                    object.y = Math.min(this.lastY, y)
                    object.width = Math.abs(this.lastX - x)
                    object.height = Math.abs(this.lastY - y)
                    objectLayer.addObject(object)
                }
    
                this.distance = 0
                this.lastX = x
                this.lastY = y
            }
        },
    
        mousePressed: function(button, x, y, modifiers) {
            this.pressed = true
            this.x = x
            this.y = y
            this.distance = 0
            this.counter = 0
            this.lastX = x
            this.lastY = y
        },
    
        mouseReleased: function(button, x, y, modifiers) {
            this.pressed = false
        },
    })
    

reload

  • Reloads the given asset from disk, without checking for unsaved changes. This invalidates the previous script reference to the asset, hence the new reference is returned for convenience. Returns null if reloading failed.

tilesetFormat

  • Returns the tileset format object with the given name, or undefined if no object was found. See the tilesetFormats property for more info.

tilesetFormatForFile

  • tilesetFormatForFile(fileName: string): TilesetFormat | undefined
  • Returns the tileset format object that can read the given file, or undefined if no object was found.

trigger

  • trigger(action: string): void
  • This function can be used to trigger any registered action. This includes most actions you would normally trigger through the menu or by using their shortcut.

    Use the tiled.actions property to get a list of all available actions.

    Actions that are checkable will toggle when triggered.

versionLessThan

  • versionLessThan(a: string, b?: string): boolean
  • When given two versions, returns whether the first version comes before the second version. When given one version, it returns whether Tiled's current version comes before the given version.

    since

    1.10

warn

  • warn(text: string, activated: () => void): void
  • Outputs the given text in the Console window as warning message and creates an issue in the Issues window.

    When the issue is activated (with double-click or Enter key) the given callback function is invoked.

Generated using TypeDoc