Skip to content

Loader

const loader = require("@vectarine/loader")

Loader

Functions to load external resources like images, scripts, fonts, shaders, audio, etc.

loader.loadScript<T>(path: string, type_hint: T?): (Res.ScriptResource, T)

Similar to require(), but it is synchronous and will return an empty table if the script has not been loaded yet. Usually, you should use require() unless you want to load your scripts asynchronously, if you have a lot of scripts.

For typing purposes, you can pass a table as the second argument, but this has no runtime effect. The return value is a ScriptResource and the module table returned by the script.

Example:

local loader = require("@vectarine/loader")
-- We pass require("other.luau") as the second argument to get proper type inference for the returned module "other".
local res, other = loader.loadScript("scripts/other.luau", require("other.luau"))

Parameters

  • path (string): For example scripts/monster.lua
  • type_hint (T?): A table to hint the type of the module returned by the script. This has no runtime effect.
loader.loadImage(path: string, antialiasing: boolean?): Image.ImageResource

Load an image from a path If antialiasing is true, the image will be drawn with linear filtering, otherwise with nearest filtering. If you are using a pixel art style, you probably want antialiasing to be false.

Parameters

  • path (string): string

Returns ImageResource

loader.loadFont(path: string): Text.FontResource

Load a font from a path

Parameters

  • path (string): string

Returns FontResource

loader.loadText(path: string): Res.TextResource

Load text from a path. The path should be relative to the game.vecta file, like “./texts/mytext.txt”

Parameters

  • path (string): string

Returns TextResource

loader.loadShader(path: string): Canvas.ShaderResource

Load a shader from a path

Parameters

  • path (string): string

Returns ShaderResource

loader.loadAudio(path: string): Audio.AudioResource

Load audio from a path

Parameters

  • path (string): string

Returns AudioResource

loader.loadTileset(path: string): Tile.TilesetResource

Load a tileset from a path

Parameters

  • path (string): string

Returns TilesetResource

loader.loadTilemap(path: string): Tile.TilemapResource

Load a tilemap from a path

Parameters

  • path (string): string

Returns TilemapResource

loader.init(): nil

Return a table which is referentially stable across script reloads for a given script, or nil if the script has never been loaded before. You should use this table to store the functions and properties declared in your script.

local loader = require("@vectarine/loader")
local module = loader.init() or {}
if not module.hasBeenLoaded then
--- This module has been loaded for the first time!
end
--- Define all the properties of your module here
module.hasBeenLoaded = true
return module