Editor

Namespace: editor Language: Lua Type: Defold Lua File: editor.apidoc Source: engine/docs/editor.apidoc

Editor scripting documentation

API

editor.bob

Type: FUNCTION Run bob the builder program For the full documentation of the available commands and options, see the bob manual.

Parameters

Examples

Print help in the console:

editor.bob({help = true})

Bundle the game for the host platform:

local opts = {
    archive = true,
    platform = editor.platform
}
editor.bob(opts, "distclean", "resolve", "build", "bundle")

Using snake_cased and repeated options:

local opts = {
    archive = true,
    platform = editor.platform,
    build_server = "https://build.my-company.com",
    settings = {"test.ini", "headless.ini"}
}
editor.bob(opts, "distclean", "resolve", "build")

editor.browse

Type: FUNCTION Open a URL in the default browser or a registered application

Parameters

editor.can_add

Type: FUNCTION Check if editor.tx.add() (as well as editor.tx.clear() and editor.tx.remove()) transaction with this property won’t throw an error

Parameters

Returns

editor.can_get

Type: FUNCTION Check if you can get this property so editor.get() won’t throw an error

Parameters

Returns

editor.can_reorder

Type: FUNCTION Check if editor.tx.reorder() transaction with this property won’t throw an error

Parameters

Returns

editor.can_reset

Type: FUNCTION Check if editor.tx.reset() transaction with this property won’t throw an error

Parameters

Returns

editor.can_set

Type: FUNCTION Check if editor.tx.set() transaction with this property won’t throw an error

Parameters

Returns

editor.command

Type: FUNCTION Create an editor command

Parameters

Returns

Examples

Print Git history for a file:

editor.command({
  label = "Git History",
  query = {
    selection = {
      type = "resource",
      cardinality = "one"
    }
  },
  run = function(opts)
    editor.execute(
      "git",
      "log",
      "--follow",
      "." .. editor.get(opts.selection, "path"),
      {reload_resources=false})
  end
})

editor.create_directory

Type: FUNCTION Create a directory if it does not exist, and all non-existent parent directories. Throws an error if the directory can’t be created.

Parameters

Examples

editor.create_directory("/assets/gen")

editor.create_resources

Type: FUNCTION Create resources (including non-existent parent directories). Throws an error if any of the provided resource paths already exist

Parameters

Examples

Create a single resource from template:

editor.create_resources({
  "/npc.go"
})

Create multiple resources:

editor.create_resources({
  "/npc.go",
  "/levels/1.collection",
  "/levels/2.collection",
})

Create a resource with custom content:

editor.create_resources({
  {"/npc.script", "go.property('hp', 100)"}
})

editor.delete_directory

Type: FUNCTION Delete a directory if it exists, and all existent child directories and files. Throws an error if the directory can’t be deleted.

Parameters

Examples

editor.delete_directory("/assets/gen")

editor.editor_sha1

Type: VARIABLE A string, SHA1 of Defold editor

editor.engine_sha1

Type: VARIABLE A string, SHA1 of Defold engine

editor.execute

Type: FUNCTION Execute a shell command. Any shell command arguments should be provided as separate argument strings to this function. If the exit code of the process is not zero, this function throws error. By default, the function returns nil, but it can be configured to capture the output of the shell command as string and return it — set out option to “capture” to do it.By default, after this shell command is executed, the editor will reload resources from disk.

Parameters

Returns

Examples

Make a directory with spaces in it:

editor.execute("mkdir", "new dir")

Read the git status:

local status = editor.execute("git", "status", "--porcelain", {
  reload_resources = false,
  out = "capture"
})

editor.external_file_attributes

Type: FUNCTION Query information about file system path

Parameters

Returns

editor.get

Type: FUNCTION Get a value of a node property inside the editor. Some properties might be read-only, and some might be unavailable in different contexts, so you should use editor.can_get() before reading them and editor.can_set() before making the editor set them.

Parameters

Returns

editor.open_external_file

Type: FUNCTION Open a file in a registered application

Parameters

editor.platform

Type: VARIABLE Editor platform id. A string, either:

editor.prefs.get

Type: FUNCTION Get preference value The schema for the preference value should be defined beforehand.

Parameters

Returns

editor.prefs.is_set

Type: FUNCTION Check if preference value is explicitly set The schema for the preference value should be defined beforehand.

Parameters

Returns

editor.prefs.schema.array

Type: FUNCTION array schema

Parameters

Returns

editor.prefs.schema.boolean

Type: FUNCTION boolean schema

Parameters

Returns

editor.prefs.schema.enum

Type: FUNCTION enum value schema

Parameters

Returns

editor.prefs.schema.integer

Type: FUNCTION integer schema

Parameters

Returns

editor.prefs.schema.keyword

Type: FUNCTION keyword schema A keyword is a short string that is interned within the editor runtime, useful e.g. for identifiers

Parameters

Returns

editor.prefs.schema.number

Type: FUNCTION floating-point number schema

Parameters

Returns

editor.prefs.schema.object

Type: FUNCTION heterogeneous object schema

Parameters

Returns

editor.prefs.schema.object_of

Type: FUNCTION homogeneous object schema

Parameters

Returns

editor.prefs.schema.one_of

Type: FUNCTION one of schema

Parameters

Returns

editor.prefs.schema.password

Type: FUNCTION password schema A password is a string that is encrypted when stored in a preference file

Parameters

Returns

editor.prefs.schema.set

Type: FUNCTION set schema Set is represented as a lua table with true values

Parameters

Returns

editor.prefs.schema.string

Type: FUNCTION string schema

Parameters

Returns

editor.prefs.schema.tuple

Type: FUNCTION tuple schema A tuple is a fixed-length array where each item has its own defined type

Parameters

Returns

editor.prefs.SCOPE.GLOBAL

Type: VARIABLE “global”

editor.prefs.SCOPE.PROJECT

Type: VARIABLE “project”

editor.prefs.set

Type: FUNCTION Set preference value The schema for the preference value should be defined beforehand.

Parameters

editor.properties

Type: FUNCTION List property names for a node. The result is context-sensitive and can vary by node/resource type and editor state. Returned names are readable with editor.get(node, property). Mutating capabilities are per-property; use editor.can_set(), editor.can_reset(), editor.can_add(), and editor.can_reorder() to check which operations are supported.

Parameters

Returns

editor.resource_attributes

Type: FUNCTION Query information about a project resource

Parameters

Returns

editor.save

Type: FUNCTION Persist any unsaved changes to disk

editor.transact

Type: FUNCTION Change the editor state in a single, undoable transaction

Parameters

editor.tx.add

Type: FUNCTION Create a transaction step that will add a child item to a node’s list property when transacted with editor.transact().

Parameters

editor.tx.clear

Type: FUNCTION Create a transaction step that will remove all items from node’s list property when transacted with editor.transact().

Parameters

Returns

editor.tx.remove

Type: FUNCTION Create a transaction step that will remove a child node from the node’s list property when transacted with editor.transact().

Parameters

Returns

editor.tx.reorder

Type: FUNCTION Create a transaction step that reorders child nodes in a node list defined by the property if supported (see editor.can_reorder())

Parameters

Returns

editor.tx.reset

Type: FUNCTION Create a transaction step that will reset an overridden property to its default value when transacted with editor.transact().

Parameters

Returns

editor.tx.set

Type: FUNCTION Create transaction step that will set the node’s property to a supplied value when transacted with editor.transact().

Parameters

Returns

editor.ui.ALIGNMENT.BOTTOM

Type: VARIABLE “bottom”

editor.ui.ALIGNMENT.BOTTOM_LEFT

Type: VARIABLE “bottom-left”

editor.ui.ALIGNMENT.BOTTOM_RIGHT

Type: VARIABLE “bottom-right”

editor.ui.ALIGNMENT.CENTER

Type: VARIABLE “center”

editor.ui.ALIGNMENT.LEFT

Type: VARIABLE “left”

editor.ui.ALIGNMENT.RIGHT

Type: VARIABLE “right”

editor.ui.ALIGNMENT.TOP

Type: VARIABLE “top”

editor.ui.ALIGNMENT.TOP_LEFT

Type: VARIABLE “top-left”

editor.ui.ALIGNMENT.TOP_RIGHT

Type: VARIABLE “top-right”

editor.ui.button

Type: FUNCTION Button with a label and/or an icon

Parameters

Returns

editor.ui.check_box

Type: FUNCTION Check box with a label

Parameters

Returns

editor.ui.COLOR.ERROR

Type: VARIABLE “error”

editor.ui.COLOR.HINT

Type: VARIABLE “hint”

editor.ui.COLOR.OVERRIDE

Type: VARIABLE “override”

editor.ui.COLOR.TEXT

Type: VARIABLE “text”

editor.ui.COLOR.WARNING

Type: VARIABLE “warning”

editor.ui.component

Type: FUNCTION Convert a function to a UI component. The wrapped function may call any hooks functions (editor.ui.use_*), but on any function invocation, the hooks calls must be the same, and in the same order. This means that hooks should not be used inside loops and conditions or after a conditional return statement. The following props are supported automatically:grow booleandetermines if the component should grow to fill available space in a horizontal or vertical layout containerrow_span integerhow many rows the component spans inside a grid container, must be positive. This prop is only useful for components inside a grid container.column_span integerhow many columns the component spans inside a grid container, must be positive. This prop is only useful for components inside a grid container.

Parameters

Returns

editor.ui.dialog

Type: FUNCTION Dialog component, a top-level window component that can’t be used as a child of other components

Parameters

Returns

editor.ui.dialog_button

Type: FUNCTION Dialog button shown in the footer of a dialog

Parameters

Returns

editor.ui.external_file_field

Type: FUNCTION Input component for selecting files from the file system

Parameters

Returns

editor.ui.grid

Type: FUNCTION Layout container that places its children in a 2D grid

Parameters

Returns

editor.ui.heading

Type: FUNCTION A text heading

Parameters

Returns

editor.ui.HEADING_STYLE.DIALOG

Type: VARIABLE “dialog”

editor.ui.HEADING_STYLE.FORM

Type: VARIABLE “form”

editor.ui.HEADING_STYLE.H1

Type: VARIABLE “h1”

editor.ui.HEADING_STYLE.H2

Type: VARIABLE “h2”

editor.ui.HEADING_STYLE.H3

Type: VARIABLE “h3”

editor.ui.HEADING_STYLE.H4

Type: VARIABLE “h4”

editor.ui.HEADING_STYLE.H5

Type: VARIABLE “h5”

editor.ui.HEADING_STYLE.H6

Type: VARIABLE “h6”

editor.ui.horizontal

Type: FUNCTION Layout container that places its children in a horizontal row one after another

Parameters

Returns

editor.ui.icon

Type: FUNCTION An icon from a predefined set

Parameters

Returns

editor.ui.ICON.CLEAR

Type: VARIABLE “clear”

editor.ui.ICON.MINUS

Type: VARIABLE “minus”

editor.ui.ICON.OPEN_RESOURCE

Type: VARIABLE “open-resource”

editor.ui.ICON.PLUS

Type: VARIABLE “plus”

editor.ui.image

Type: FUNCTION An image

Parameters

Returns

editor.ui.integer_field

Type: FUNCTION Integer input component based on a text field, reports changes on commit (Enter or focus loss)

Parameters

Returns

editor.ui.ISSUE_SEVERITY.ERROR

Type: VARIABLE “error”

editor.ui.ISSUE_SEVERITY.WARNING

Type: VARIABLE “warning”

editor.ui.label

Type: FUNCTION Label intended for use with input components

Parameters

Returns

editor.ui.number_field

Type: FUNCTION Number input component based on a text field, reports changes on commit (Enter or focus loss)

Parameters

Returns

editor.ui.open_resource

Type: FUNCTION Open a resource, either in the editor or in a third-party app

Parameters

editor.ui.ORIENTATION.HORIZONTAL

Type: VARIABLE “horizontal”

editor.ui.ORIENTATION.VERTICAL

Type: VARIABLE “vertical”

editor.ui.PADDING.LARGE

Type: VARIABLE “large”

editor.ui.PADDING.MEDIUM

Type: VARIABLE “medium”

editor.ui.PADDING.NONE

Type: VARIABLE “none”

editor.ui.PADDING.SMALL

Type: VARIABLE “small”

editor.ui.paragraph

Type: FUNCTION A paragraph of text

Parameters

Returns

editor.ui.resource_field

Type: FUNCTION Input component for selecting project resources

Parameters

Returns

editor.ui.scroll

Type: FUNCTION Layout container that optionally shows scroll bars if child contents overflow the assigned bounds

Parameters

Returns

editor.ui.select_box

Type: FUNCTION Dropdown select box with an array of options

Parameters

Returns

editor.ui.separator

Type: FUNCTION Thin line for visual content separation, by default horizontal and aligned to center

Parameters

Returns

editor.ui.show_dialog

Type: FUNCTION Show a modal dialog and await a result

Parameters

Returns

editor.ui.show_external_directory_dialog

Type: FUNCTION Show a modal OS directory selection dialog and await a result

Parameters

Returns

editor.ui.show_external_file_dialog

Type: FUNCTION Show a modal OS file selection dialog and await a result

Parameters

Returns

editor.ui.show_resource_dialog

Type: FUNCTION Show a modal resource selection dialog and await a result

Parameters

Returns

editor.ui.SPACING.LARGE

Type: VARIABLE “large”

editor.ui.SPACING.MEDIUM

Type: VARIABLE “medium”

editor.ui.SPACING.NONE

Type: VARIABLE “none”

editor.ui.SPACING.SMALL

Type: VARIABLE “small”

editor.ui.string_field

Type: FUNCTION String input component based on a text field, reports changes on commit (Enter or focus loss)

Parameters

Returns

editor.ui.TEXT_ALIGNMENT.CENTER

Type: VARIABLE “center”

editor.ui.TEXT_ALIGNMENT.JUSTIFY

Type: VARIABLE “justify”

editor.ui.TEXT_ALIGNMENT.LEFT

Type: VARIABLE “left”

editor.ui.TEXT_ALIGNMENT.RIGHT

Type: VARIABLE “right”

editor.ui.use_memo

Type: FUNCTION A hook that caches the result of a computation between re-renders. See editor.ui.component for hooks caveats and rules. If any of the arguments to use_memo change during a component refresh (checked with ==), the value will be recomputed.

Parameters

Returns

Examples

local function increment(n)
    return n + 1
end

local function make_listener(set_count)
    return function()
        set_count(increment)
    end
end

local counter_button = editor.ui.component(function(props)
    local count, set_count = editor.ui.use_state(props.count)
    local on_pressed = editor.ui.use_memo(make_listener, set_count)
    return editor.ui.text_button {
        text = tostring(count),
        on_pressed = on_pressed
    }
end)

editor.ui.use_state

Type: FUNCTION A hook that adds local state to the component. See editor.ui.component for hooks caveats and rules. If any of the arguments to use_state change during a component refresh (checked with ==), the current state will be reset to the initial one.

Parameters

Returns

Examples

local function increment(n)
  return n + 1
end

local counter_button = editor.ui.component(function(props)
  local count, set_count = editor.ui.use_state(props.count)
  return editor.ui.text_button {
    text = tostring(count),
    on_pressed = function()
      set_count(increment)
    end
  }
end)

editor.ui.vertical

Type: FUNCTION Layout container that places its children in a vertical column one after another

Parameters

Returns

editor.version

Type: VARIABLE A string, version name of Defold

http.request

Type: FUNCTION Perform an HTTP request

Parameters

Returns

http.server.external_file_response

Type: FUNCTION Create HTTP response that will stream the content of a file defined by the path

Parameters

Returns

http.server.json_response

Type: FUNCTION Create HTTP response with a JSON value

Parameters

Returns

http.server.local_url

Type: VARIABLE Editor’s HTTP server local url

http.server.port

Type: VARIABLE Editor’s HTTP server port

http.server.resource_response

Type: FUNCTION Create HTTP response that will stream the content of a resource defined by the resource path

Parameters

Returns

http.server.response

Type: FUNCTION Create HTTP response

Parameters

Returns

http.server.route

Type: FUNCTION Create route definition for the editor’s HTTP server

Parameters

Returns

Examples

Receive JSON and respond with JSON:

http.server.route(
  "/json", "POST", "json",
  function(request)
    pprint(request.body)
    return 200
  end
)

Extract parts of the path:

http.server.route(
  "/users/{user}/orders",
  function(request)
    print(request.user)
  end
)

Simple file server:

http.server.route(
  "/files/{*file}",
  function(request)
    local attrs = editor.external_file_attributes(request.file)
    if attrs.is_file then
      return http.server.external_file_response(request.file)
    elseif attrs.is_directory then
      return 400
    else
      return 404
    end
  end
)

http.server.url

Type: VARIABLE Editor’s HTTP server url

json.decode

Type: FUNCTION Decode JSON string to Lua value

Parameters

json.encode

Type: FUNCTION Encode Lua value to JSON string

Parameters

localization.and_list

Type: FUNCTION Create a message pattern that renders a list with the “and” conjunction (for example: a, b, and c) once it is stringified

Parameters

Returns

localization.concat

Type: FUNCTION Create a message pattern that concatenates values (similar to table.concat) and performs the actual concatenation when stringified

Parameters

Returns

localization.message

Type: FUNCTION Create a message pattern for a localization key defined in an .editor_localization file; the actual localization happens when the returned value is stringified

Parameters

Returns

localization.or_list

Type: FUNCTION Create a message pattern that renders a list with the “or” conjunction (for example: a, b, or c) once it is stringified

Parameters

Returns

pprint

Type: FUNCTION Pretty-print a Lua value

Parameters

tilemap.tiles.clear

Type: FUNCTION Remove all tiles

Parameters

Returns

tilemap.tiles.get_info

Type: FUNCTION Get full information from a tile at a particular coordinate

Parameters

Returns

tilemap.tiles.get_tile

Type: FUNCTION Get a tile index at a particular coordinate

Parameters

Returns

tilemap.tiles.iterator

Type: FUNCTION Create an iterator over all tiles in a tiles data structure When iterating using for loop, each iteration returns x, y and tile index of a tile in a tile map

Parameters

Returns

Examples

Iterate over all tiles in a tile map:

local layers = editor.get("/level.tilemap", "layers")
for i = 1, #layers do
  local tiles = editor.get(layers[i], "tiles")
  for x, y, i in tilemap.tiles.iterator(tiles) do
    print(x, y, i)
  end
end

tilemap.tiles.new

Type: FUNCTION Create a new unbounded 2d grid data structure for storing tilemap layer tiles

Returns

tilemap.tiles.remove

Type: FUNCTION Remove a tile at a particular coordinate

Parameters

Returns

tilemap.tiles.set

Type: FUNCTION Set a tile at a particular coordinate

Parameters

Returns

zip.METHOD.DEFLATED

Type: VARIABLE “deflated” compression method

zip.METHOD.STORED

Type: VARIABLE “stored” compression method, i.e. no compression

zip.ON_CONFLICT.ERROR

Type: VARIABLE “error”, any conflict aborts extraction

zip.ON_CONFLICT.OVERWRITE

Type: VARIABLE “skip”, existing file is overwritten

zip.ON_CONFLICT.SKIP

Type: VARIABLE “skip”, existing file is preserved

zip.pack

Type: FUNCTION Create a ZIP archive

Parameters

Examples

Archive a file and a folder:

zip.pack("build.zip", {"build", "game.project"})

Change the location of the files within the archive:

zip.pack("build.zip", {
  {"build/wasm-web", "."},
  {"configs/prod.json", "config.json"}
})

Create archive without compression (much faster to create the archive, bigger archive file size, allows mmap access):

zip.pack("build.zip", {method = zip.METHOD.STORED}, {
  "build",
  "resources"
})

Don’t compress one of the folders:

zip.pack("build.zip", {
  {"assets", method = zip.METHOD.STORED},
  "build/wasm-web"
})

Include files from outside the project:

zip.pack("build.zip", {
  "build",
  {"../secrets/auth-key.txt", "auth-key.txt"}
})

zip.unpack

Type: FUNCTION Extract a ZIP archive

Parameters

Examples

Extract everything to a build dir:

zip.unpack("build/dev/resources.zip")

Extract to a different directory:

zip.unpack(
  "build/dev/resources.zip",
  "build/dev/tmp",
)

Extract while overwriting existing files on conflict:

zip.unpack(
  "build/dev/resources.zip",
  {on_conflict = zip.ON_CONFLICT.OVERWRITE}
)

Extract a single file:

zip.unpack(
  "build/dev/resources.zip",
  {"config.json"}
)

zlib.deflate

Type: FUNCTION Deflate (compress) a buffer

Parameters

Returns

zlib.inflate

Type: FUNCTION Inflate (decompress) a buffer

Parameters

Returns