Skip to content

Instanced dungeon ​

Build a goblin cave that every party plays from the start. A cave keeper in town opens a private copy of the cave for the players around them. The goblins inside belong to that copy alone, a kill counter is shared across every floor of it, and a treasure gate opens once the party has defeated enough goblins. When the run is over, the copy cleans itself up.

By the end you will have:

  • a cave keeper who creates an instance and warps a whole party into it,
  • goblins that count kills for their instance,
  • a gate on the last floor that opens at the goal and rewards everyone, and
  • a cave that is destroyed a minute after the last player leaves.

What you'll learn

  • Creating instances with instances.create
  • Warping a party in with player.warpto(level, x, y, { instance })
  • Sharing state across all of an instance's levels with this.instance
  • How links behave inside an instance, and how destroyWhenEmpty and exit end a run

The finished files are in docs/examples/instanced-dungeon/:

FileRunsGoes to
npcs/cavekeeper.npc.tsserverthe serverside script of an NPC in town
npcs/goblin.npc.tsserverthe serverside script of each goblin in the cave
npcs/gate.npc.tsserverthe serverside script of the gate on the last floor

You need a way to shoot goblins. The gun from the projectile gun tutorial works, since goblins react to onShot. You also need a few 32×32 images in assets/images/: cavekeeper.png, goblin.png, gate.png and gate-open.png. Any images work.

How it fits together ​

 town.gmap (shared)                 goblin cave — one private copy per party
 ──────────────────                 ────────────────────────────────────────
 cave keeper                        goblincave1.glvl ──link──► goblincave2.glvl
   "enter cave"                       goblins: kills++           goblins: kills++
   instances.create(...) ───────►                                gate: kills >= goal?
   p.warpto(..., { instance })      this.instance = { goal: 5, kills: n }  (shared)
                                    exit link ──► town.gmap  (leaves the instance)

Step 1: Build the cave ​

In GRC's Level Editor, create two levels named goblincave1.glvl and goblincave2.glvl:

  1. In goblincave1.glvl, add a few goblin NPCs, a link to goblincave2.glvl, and an exit link back to your town level.
  2. In goblincave2.glvl, add a few more goblins and the gate NPC.

Both floors match the pattern goblincave*.glvl, so they are members of the instance. A link between them keeps the party inside their copy. The exit link points at the town, which is not a member, so walking through it leaves the instance. You don't need any scripting for either link.

Step 2: The cave keeper ​

ts
const CAVE_LEVELS = ['goblincave*.glvl']   // every floor of the cave
const ENTRANCE = { level: 'goblincave1.glvl', x: 30, y: 58 }
const PARTY_RADIUS = 6                     // tiles around the keeper

export function onCreated() {
    this.image = 'images/cavekeeper.png'
    this.chat = 'Say "enter cave" to start the hunt!'
}
ts
export function onPlayerChats(player: Player, text: string) {
    if (text.trim().toLowerCase() !== 'enter cave') return

    // Already inside a run? Nothing to do.
    if (player.instance) return

    // The party: everyone near the keeper who isn't in another instance.
    const party = this.level.players.filter((p: Player) =>
        !p.instance && Math.hypot(p.x - this.x, p.y - this.y) <= PARTY_RADIUS)

    const cave = instances.create({
        name: 'goblin-cave',
        levels: CAVE_LEVELS,
        // Back in front of the keeper when the run ends.
        exit: { level: this.level.name, x: this.x, y: this.y + 3 },
        // Clean up a minute after the last player leaves.
        destroyWhenEmpty: 60,
    })
    cave.goal = 5      // shared instance state: every cave NPC can read it
    cave.kills = 0

    for (const p of party)
        p.warpto(ENTRANCE.level, ENTRANCE.x, ENTRANCE.y, { instance: cave })
    this.chat = `Good luck, party of ${party.length}!`
}

Each instances.create call makes a brand-new copy of the cave, so every "enter cave" starts a fresh run. A few details:

  • name: 'goblin-cave' is a label for staff and for instances.list. Every run shares it.
  • exit brings players back to the keeper when the instance is destroyed.
  • destroyWhenEmpty: 60 removes the copy a minute after the last player leaves. Without it, the instance would stay until a script called destroy().
  • cave.goal and cave.kills are shared instance state: any property that isn't a built-in (id, name, levels, players, alive, destroy). Every NPC in the cave sees them as this.instance.goal and this.instance.kills.

p.instance is null for players in the shared world, so the party filter skips anyone who is already in another run.

Step 3: Goblins count kills ​

ts
export function onCreated() {
    this.image = 'images/goblin.png'
    this.dead = false
}

export function onShot() {
    // Outside an instance (someone placed a goblin in a shared level) the
    // kill counter has nowhere to go.
    if (this.dead || !this.instance) return
    this.dead = true
    this.image = ''        // no image = no shape: it can't be hit again
    this.dontblock()

    // One counter for the whole instance, whichever floor this goblin is on.
    this.instance.kills = (this.instance.kills ?? 0) + 1
}

A goblin's this.instance is its instance's handle. Goblins on both floors write to the same kills, while a goblin in another party's copy writes to that party's counter. The !this.instance check keeps the script harmless if someone places a goblin in a shared level.

Step 4: The gate ​

ts
export function onCreated() {
    this.image = 'images/gate.png'
    this.setShape(0, 0, 32, 32)
    this.opened = false
}

export function onUpdate() {
    const cave = this.instance
    if (!cave || this.opened) return

    const kills = cave.kills ?? 0
    this.chat = `${kills}/${cave.goal} goblins`
    if (kills < cave.goal) return

    this.opened = true
    this.chat = 'The gate opens!'
    this.image = 'images/gate-open.png'
    this.dontblock()

    // Reward everyone in the run — wherever in the cave they are.
    for (const p of cave.players)
        p.clientr.goblinCaveClears = (p.clientr.goblinCaveClears ?? 0) + 1
}

The gate reads the counter every tick and opens at the goal. cave.players lists everyone in the instance on any floor, so the reward reaches the whole party.

Try it ​

  1. Place the cave keeper NPC in your town level, paste cavekeeper.npc.ts into its serverside script, and save.
  2. Stand next to it with a friend and say enter cave. You both warp into a new copy of goblincave1.glvl.
  3. Shoot the goblins, take the link to the second floor, and watch the gate count up. At 5 it opens.
  4. Walk out through the exit link: you're back in the shared world. Ask the keeper again and you get a fresh cave with every goblin back.

While testing, run /instances in RC to see the live caves, who is in them, and the auto-destroy countdown. /instance <id> shows the shared state keys (goal, kills).

Clientside: "you're in a dungeon"

A weapon can show a HUD hint while player.instanced is true. Clients never see instance ids, only whether they're in one.

Complete files

npcs/cavekeeper.npc.ts

ts
// Serverside script of the cave keeper NPC, in town (paste into the NPC's
// serverside script in GRC's level editor). Say "enter cave" next to it and
// you — plus everyone standing nearby — get a fresh, private goblin cave.

const CAVE_LEVELS = ['goblincave*.glvl']   // every floor of the cave
const ENTRANCE = { level: 'goblincave1.glvl', x: 30, y: 58 }
const PARTY_RADIUS = 6                     // tiles around the keeper

export function onCreated() {
    this.image = 'images/cavekeeper.png'
    this.chat = 'Say "enter cave" to start the hunt!'
}

export function onPlayerChats(player: Player, text: string) {
    if (text.trim().toLowerCase() !== 'enter cave') return

    // Already inside a run? Nothing to do.
    if (player.instance) return

    // The party: everyone near the keeper who isn't in another instance.
    const party = this.level.players.filter((p: Player) =>
        !p.instance && Math.hypot(p.x - this.x, p.y - this.y) <= PARTY_RADIUS)

    const cave = instances.create({
        name: 'goblin-cave',
        levels: CAVE_LEVELS,
        // Back in front of the keeper when the run ends.
        exit: { level: this.level.name, x: this.x, y: this.y + 3 },
        // Clean up a minute after the last player leaves.
        destroyWhenEmpty: 60,
    })
    cave.goal = 5      // shared instance state: every cave NPC can read it
    cave.kills = 0

    for (const p of party)
        p.warpto(ENTRANCE.level, ENTRANCE.x, ENTRANCE.y, { instance: cave })
    this.chat = `Good luck, party of ${party.length}!`
}

npcs/goblin.npc.ts

ts
// Serverside script of a goblin NPC inside the cave (paste into each goblin's
// serverside script). Shoot it once to defeat it. Every instance has its own
// goblins, so each party fights all of them.

export function onCreated() {
    this.image = 'images/goblin.png'
    this.dead = false
}

export function onShot() {
    // Outside an instance (someone placed a goblin in a shared level) the
    // kill counter has nowhere to go.
    if (this.dead || !this.instance) return
    this.dead = true
    this.image = ''        // no image = no shape: it can't be hit again
    this.dontblock()

    // One counter for the whole instance, whichever floor this goblin is on.
    this.instance.kills = (this.instance.kills ?? 0) + 1
}

npcs/gate.npc.ts

ts
// Serverside script of the treasure gate on the cave's last floor. It opens
// once the party has defeated enough goblins — counted across every floor of
// their instance — and rewards everyone inside.

export function onCreated() {
    this.image = 'images/gate.png'
    this.setShape(0, 0, 32, 32)
    this.opened = false
}

export function onUpdate() {
    const cave = this.instance
    if (!cave || this.opened) return

    const kills = cave.kills ?? 0
    this.chat = `${kills}/${cave.goal} goblins`
    if (kills < cave.goal) return

    this.opened = true
    this.chat = 'The gate opens!'
    this.image = 'images/gate-open.png'
    this.dontblock()

    // Reward everyone in the run — wherever in the cave they are.
    for (const p of cave.players)
        p.clientr.goblinCaveClears = (p.clientr.goblinCaveClears ?? 0) + 1
}