Skip to content

Loader

The Loader module loads project items by path. Paths are project-relative strings such as /resources/textures/wood (no file extension). See Asset Loading for a full scripting guide.

Usage

local Loader = require 'engine/loader'

Functions

All load functions return a Promise resolving to an item handle.

Function Description
loadImage(path, options?) Image / texture item. Options: flipY, waitUntilFinished.
loadMaterial(path, options?) Material item.
loadMesh(path, options?) Mesh item.
loadPrefab(path, options?) Prefab item.
loadFont(path, options?) Font item (for Canvas).
loadGridMap(path, options?) Grid map item.
loadStructuredData(path, options?) Structured data item.
getItemCountToLoad() Returns how many items are still loading (synchronous).

Examples

Non-blocking load

Loader.loadImage("/resources/textures/mytexture"):next(function(texture)
    print("Loaded:", texture:getName())
end)

Setup with await

function MyBehaviour:init()
    self.texture = await(Loader.loadImage("/resources/textures/wood", {
        waitUntilFinished = true,
    }))
end

Create a mesh scene object from a loaded item

local SgMesh = require 'engine/sceneobjects/sgmesh'

Loader.loadMesh("/resources/meshes/crate"):next(function(meshItem)
    SgMesh.create({ mesh = meshItem })
end)

Reference

getItemCountToLoad

static getItemCountToLoad()

Return how many items are currently beeing loaded

Returns

  • number: count

loadImage

static loadImage(path, options)

Loads an image from an item by its relative path

Parameters

  • path (string): relative path to the image file inside the project i.e. ("/resources/textures/tex1")
  • options: object with the following keys:
    • flipY (boolean): if set to true, image will be flipped. for canvas rendering this should be set to false. default = true
    • waitUntilFinished (boolean): if set to true, the promise will only fulfil as soon as the texture is fully loaded. default = false

Returns

  • Promise: a promise that will deliver a Texture object

loadMaterial

static loadMaterial(path, options)

Loads a material by path and will emit an loadMaterialResult event when the material is ready or loadMaterialError event on error

Parameters

  • path (string): relative path to the material item inside the project i.e. ("/resources/materials/mat1")
  • options: object with the following keys:
    • waitUntilFinished (boolean): if set to true, the promise will only fulfil as soon as the material is fully loaded. default = false

Returns

  • Promise: a promise that will deliver a Material object

loadMesh

static loadMesh(path, options)

Loads a mesh from an item by its relative path.

Parameters

  • path (string): relative path to the mesh item inside the project i.e. ("/resources/meshes/mesh1")
  • options: object with the following keys:
    • waitUntilFinished (boolean): if set to true, the promise will only fulfil as soon as the mesh is fully loaded. default = false

Returns

  • Promise: a promise that will deliver a Mesh object

loadPrefab

static loadPrefab(path, options)

Loads a prefab from an item by its relative path.

Parameters

  • path (string): relative path to the prefab item inside the project i.e. ("/resources/meshes/prefab1")
  • options: object with the following keys:
    • waitUntilFinished (boolean): if set to true, the promise will only fulfil as soon as the prefab is fully loaded. default = false

Returns

  • Promise: a promise that will deliver a Prefab object

loadGridMap

static loadGridMap(path, options)

Loads a gridmap from an item by its relative path.

Parameters

  • path (string): relative path to the gridmap item inside the project i.e. ("/resources/gridmaps/gridmap1")
  • options: object with the following keys:
    • waitUntilFinished (boolean): if set to true, the promise will only fulfil as soon as the gridmap is fully loaded. default = false

Returns

  • Promise: a promise that will deliver a GridMap object

loadFont

static loadFont(path, options)

Loads a font from an item by its relative path. Once this is loaded, it can be instantly used in canvas by its name for example Canvas.setFont("20px font1")

Parameters

  • path (string): relative path to the font item inside the project i.e. ("/resources/fonts/font1")
  • options: object with the following keys:
    • waitUntilFinished (boolean): if set to true, the promise will only fulfil as soon as the font is fully loaded. default = false

Returns

  • Promise: a promise that will deliver a Font object

loadStructuredData

static loadStructuredData(path, options)

Loads a structured data item by its relative path.

Parameters

  • path (string): relative path to the structured data item inside the project i.e. ("/resources/data/level1")
  • options: object with the following keys:
    • waitUntilFinished (boolean): if set to true, the promise will only fulfil as soon as the data is fully loaded. default = false

Returns

  • Promise: a promise that will deliver a Structured Data object