Skip to content
Download

Instances

Functions over the instances of the current room. The object is given by name (Lua) or by class, <T> (C++).

Function What it does
spawn(object, x, y, layer) Creates an instance on the layer and returns it. Its onSpawn runs immediately.
spawnAtDepth(object, x, y, depth) Creates an instance outside the layers, at a depth.
count(object) How many live instances exist.
any(object) true if at least one exists.
instance(object) The first live instance, or nothing. For single-instance objects, such as a controller.
closest(object) The instance closest to this one, or nothing.
each(object, fn) Calls fn for each live instance.
all(object) Lua: the list of live instances.
destroyAll(object) Destroys all instances of the object.
local bala = spawn("Bullet", self.x, self.y - 8, "Instancias")
bala.vspeed = -5
if count("Enemy") == 0 then goTo("Vitoria") end
local alvo = self:closest("Enemy")
if alvo then self:moveToward(alvo.x, alvo.y, 1.5) end
each("Enemy", function(e) e:destroy() end)
  • “Live” = not destroyed yet. Instances created during an each are not visited by it.
  • C++: count, any, each and destroyAll also accept the object name in quotes, for objects without code.
  • C++: functions that return a pointer return nullptr when there is no instance. To keep a reference for more than one step, use a Handle.
  • Lua: a stored instance answers other:exists() after it is removed.