Appearance
Instances
An instance is a private copy of one or more levels (or gmaps) for one player or a group. Each copy has its own NPCs, its own shared level state and its own player roster, while still reading the same level files. Use one whenever content must be replayable or must not be shared with the rest of the server: a quest dungeon full of monsters, a boss arena, a tutorial area, a puzzle that every player should solve from the start.
Without instances, every player shares one copy of cave.glvl. If one player kills the goblins, they're gone for everyone until the level is reloaded. With an instance, a party gets its own cave.glvl with fresh goblins, and the shared copy (and every other instance) is untouched.
Everything here is serverside. Clients only ever see the plain level names; a clientside script can check player.instanced to tell whether it's in one.
Creating an instance and warping in
instances.create makes an instance, and player.warpto with the instance option sends players into it:
ts
// Serverside (an NPC, weapon server half, or server script)
const inst = instances.create({
name: 'goblin-cave', // type label, shown by /instances
levels: ['goblincave*.glvl'], // the member levels (wildcards allowed)
exit: { level: 'town.gmap', x: 120, y: 64 },
destroyWhenEmpty: 60, // seconds; omit to keep it until destroy()
})
inst.goal = 5 // shared state (see below)
player.warpto('goblincave1.glvl', 30, 60, { instance: inst })The InstanceOptions are:
| Option | Meaning |
|---|---|
name | A type label (1–64 chars). It doesn't need to be unique: every goblin-cave run can be called 'goblin-cave'. Staff see it in /instances, and instances.list(name) filters by it. |
levels | The member levels and gmaps: file names with */? wildcards, case-insensitive. Listing a gmap includes all of its member levels. |
exit | Where players go when the instance is destroyed (and where they log back in after it's gone). Without it, each player returns to the spot they entered the instance from. |
destroyWhenEmpty | Auto-destroy after the instance has been empty for this many seconds. Without it, the instance persists until a script calls destroy(). |
The countdown for destroyWhenEmpty starts as soon as the instance is created, so leave enough time to warp players in. A player who is mid-warp into the instance already counts as inside it.
The same level can be used in the shared world and in any number of instances at once. Instances add copies; the original is never touched.
Moving in, around and out
warpto takes an optional fourth argument (WarpOptions):
| Call | Lands in |
|---|---|
player.warpto(level, x, y) | Inherits: stays in the player's current instance when level is one of its members, otherwise the shared world |
player.warpto(level, x, y, { instance: inst }) | inst. Returns false if level isn't a member or the instance was destroyed |
player.warpto(level, x, y, { instance: null }) | The shared world, always |
Links behave like the inheriting form. Inside an instance, a link to a member level keeps the player in the instance, so a dungeon's floors can link to each other normally. A link to a level that isn't a member exits to the shared world, so the dungeon's exit door is an ordinary link back to town and needs no scripting.
To bring a party, warp each player into the same instance:
ts
const party = this.level.players.filter((p: Player) => Math.hypot(p.x - this.x, p.y - this.y) < 6)
for (const p of party)
p.warpto('goblincave1.glvl', 30, 60, { instance: inst })Shared instance state: this.instance
An NPC inside an instance has this.instance (it's null in the shared world). It works like this.level's shared state, but there is one bag for the whole instance. A value set by an NPC in goblincave1.glvl can be read by an NPC in goblincave2.glvl, or by one on a member of an instanced gmap:
ts
// goblin (serverside): count kills for the whole instance
export function onShot() {
if (!this.instance || this.dead) return
this.dead = true
this.instance.kills = (this.instance.kills ?? 0) + 1
}
// gate (serverside, another level of the same instance)
export function onUpdate() {
if ((this.instance?.kills ?? 0) >= this.instance?.goal) this.dontblock()
}The handle returned by instances.create, and a player's player.instance, are the same object. Weapons and server scripts can therefore read and write the same state (for example inst.goal = 5 above).
The Instance built-ins are id, name, levels, players, alive and destroy(). They're read-only, and assigning one throws. Any other key is shared state: live JavaScript values that are never saved, and cleared when the instance is destroyed.
this.level still works inside an instance. Each instanced copy of a level has its own level state, separate from the shared world's copy and from other instances, and this.level.name is still the plain file name.
Lifetime and destroying
An instance lives until one of these happens:
- a script calls
inst.destroy()(orthis.instance.destroy()from one of its NPCs), - it has been empty for
destroyWhenEmptyseconds, - staff run
/instance <id> destroyin RC, - the server restarts (instances are memory-only).
Destroying runs in this order:
onInstanceDestroyedfires: the NPC event on the instance's NPCs, andonInstanceDestroyed(instance)on server scripts and weapon server halves. The shared state is still readable here, so save results (for example intoplayer.clientror a database) now.- Every player still inside is warped to their exit.
- The instance's NPCs unload for good (their timers stop), its projectiles vanish, and its state is cleared. Old handles report
alive === false.
Server scripts can also react to players coming and going with onPlayerEntersInstance(player, instance) and onPlayerLeavesInstance(player, instance). A disconnect also counts as leaving. Use these hooks for lifetime rules beyond destroyWhenEmpty, such as "destroy as soon as the last party member leaves":
ts
// Server script
export function onPlayerLeavesInstance(player: Player, inst: Instance) {
if (inst.name === 'boss-arena' && inst.players.length === 0) inst.destroy()
}Logging out inside an instance
A player who disconnects inside an instance and comes back while it still exists logs back in to the same place in it. If it's gone (destroyed, or the server restarted), they log in at its exit.
Clientside: player.instanced
Clients never learn instance ids or keys. The local player.instanced flag is true while the player is inside any instance, and player.level stays the plain level name. Use the flag for presentation, like a "Leave dungeon" button or a different music track. Anything that matters for gameplay should be decided on the server.
Limits and gotchas
What isn't instanced
- Level edits (
setleveltileand the other level editing methods) fail on an instance's level object — edit the shared copy viafindlevel(name)instead. Instances read the shared copy's tiles, so don't edit tiles in a level that you also use as an instance template. - Level-scoped collections use the plain level name, so an instanced copy shares collection data with the shared world's copy. Use shared instance state for per-run data.
- Staff warps from RC (
/warp, the Players editor) always land in the shared world.
- Each instance runs its own copy of its levels' NPC scripts, with fresh top-level code and state. Destroying the instance frees that copy, so short-lived instances (one per dungeon run, say) are fine.
- NPCs in an instanced level activate the first time a player enters it, just like in the shared world. A level nobody has reached yet has no running NPCs, so nothing in it has added to
this.instanceyet. instances.get(id)returnsnullfor destroyed instances. Holding on to a handle is safe: once the instance is destroyed its state reads come back empty anddestroy()returnsfalse.
Staff commands
| RC command | What it does |
|---|---|
/instances [name] | Lists live instances: id, name, the players inside, age, and the auto-destroy countdown |
/instance <id> | Details: member patterns, exit, players, which levels are active, and the keys of the shared state |
/instance <id> destroy | Destroys it, the same as inst.destroy() |
These require the instances staff right.
See the instanced dungeon tutorial for a complete example.