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

urrently selected asset, or null if no file is open. Can be assigned any open asset in order to change the active asset.

Const activeAssetChanged

activeAssetChanged: Signal<Asset>

The currently active asset has changed.

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

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.

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: Menu[]): 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

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

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

  • registerTool(shortName: string, tool: Tool): Tool
  • 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(++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

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

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