Appearance
Pressure plate & door
Build a door that opens while a player stands on a pressure plate and closes again shortly after they step off. The two NPCs never search for each other by position: the door registers itself in the level's shared state, and the plate calls it by name.
By the end you will have:
- a door that blocks the way until something opens it,
- a pressure plate that holds the door open while anyone stands on it, and closes it 1.5 seconds after the last player steps off.
What you'll learn
- Sharing state between NPCs with
this.level:this.level.door = this - Calling another NPC's exported functions:
this.level.door.open() - Finding the players in a level with
this.level.players, including on gmaps - Toggling collision with
dontblock()/blockagain()
The finished files are in docs/examples/pressure-plate-door/:
| File | Runs | Goes to |
|---|---|---|
npcs/door.npc.ts | server | the serverside script of the door NPC |
npcs/plate.npc.ts | server | the serverside script of the plate NPC |
You also need three 32×32 images in assets/images/: door.png (closed), door-open.png and plate.png. Any images work. Upload them in GRC's file browser.
How it fits together
door NPC plate NPC
──────── ─────────
onCreated: onUpdate:
this.level.door = this ──────────► this.level.players.some(standingOn)
? this.level.door?.open()
export open() ◄──────────────────────── : this.level.door?.close() (after 1.5 s)
export close() ◄────────────────────────Any property set on this.level, other than its builtins (name, players, triggerAction, …), lands in a bag that every serverside NPC in the level shares. Values are live, so the plate gets the door's actual this and can call its exported functions directly.
Step 1: The door registers itself
ts
export function onCreated() {
this.image = 'images/door.png'
this.setShape(0, 0, 32, 32) // 2×2 tiles: the area it blocks while closed
this.opened = false
// Shared level state: every serverside NPC in this level now sees
// this.level.door — a live handle to this NPC.
this.level.door = this
}setShape gives the door a 2×2 tile hitbox. While the door is blocking, players can't walk through that box. The last line publishes the door to the level.
Register in onCreated
Shared state is cleared whenever the level is hot-reloaded (saved in GRC), and the re-created NPCs run onCreated again. Registering there means the door is always findable after a reload.
Step 2: Open and close
ts
// Called by the pressure plate as this.level.door.open(). It runs as the
// door: `this` is the door and any timers it starts belong to the door.
export function open() {
if (this.opened) return
this.opened = true
this.image = 'images/door-open.png'
this.chat = ''
this.dontblock()
}
export function close() {
if (!this.opened) return
this.opened = false
this.image = 'images/door.png'
this.blockagain()
}When the plate calls this.level.door.open(), the function runs as the door. this is the door, so this.opened is the door's own per-NPC state. Any setTimeout it started would belong to the door too. dontblock() lets players walk through, and blockagain() restores collision.
Step 3: The plate
ts
const CLOSE_DELAY_MS = 1500
export function onCreated() {
this.image = 'images/plate.png'
this.dontblock() // players must be able to walk onto it
this.pressed = false
}The plate must not block, or nobody could stand on it.
ts
export function onUpdate() {
const pressed = this.level.players.some((p: Player) => standingOn(this, p))
if (pressed === this.pressed) return
this.pressed = pressed
// The door registers itself in its onCreated; NPC creation order isn't
// guaranteed, so it may not be there yet — hence the ?.
if (pressed) {
clearTimeout(this.closeTimer)
this.level.door?.open()
} else {
this.closeTimer = setTimeout(() => this.level.door?.close(), CLOSE_DELAY_MS)
}
}Every tick (20 per second), the plate checks whether any player is on it. It only does something when that answer changes. Pressing opens the door right away. Releasing schedules a close, which a new press cancels.
this.level.door?.open() uses optional chaining because the order NPCs are created in isn't guaranteed. If the plate updates before the door has registered, nothing happens yet.
Step 4: Who is standing on it?
ts
// Is the player's walking footprint (the lower half of their 2×2 body) on
// this 2×2 plate? On a gmap, players' x/y are gmap-global tiles while the
// NPC's are member-local, so convert the plate's position first —
// leveltogmap returns null on a plain level, where both are level-local.
function standingOn(plate: NpcThis, p: Player): boolean {
const g = leveltogmap(plate.level.name, plate.x, plate.y)
const x = g ? g.x : plate.x
const y = g ? g.y : plate.y
return p.x < x + 2 && p.x + 2 > x && p.y + 1 < y + 2 && p.y + 2 > y
}this.level.players lists everyone who has the level loaded. On a gmap, that includes players in neighbouring member levels, and their x/y are gmap-global. The plate's own x/y are member-local, so leveltogmap converts them first. On a plain level it returns null and the coordinates are compared as they are.
A player's position is the top-left of their 2×2 body. They "stand" on what their lower half (y + 1) covers, which is also the footprint used for walking collision.
Try it
- Open a level in GRC's Level Editor. Add an NPC across a corridor, and paste
door.npc.tsinto its serverside script. - Add a second NPC a few tiles away, and paste
plate.npc.ts. - Save the level. Walk onto the plate: the door opens. Step off: it closes 1.5 seconds later.
Complete files
npcs/door.npc.ts
ts
// Serverside script of the door NPC (paste into the NPC's serverside script
// in GRC's level editor). It registers itself in the level's shared state so
// other NPCs can find it, and exports open()/close() for them to call.
export function onCreated() {
this.image = 'images/door.png'
this.setShape(0, 0, 32, 32) // 2×2 tiles: the area it blocks while closed
this.opened = false
// Shared level state: every serverside NPC in this level now sees
// this.level.door — a live handle to this NPC.
this.level.door = this
}
// Called by the pressure plate as this.level.door.open(). It runs as the
// door: `this` is the door and any timers it starts belong to the door.
export function open() {
if (this.opened) return
this.opened = true
this.image = 'images/door-open.png'
this.chat = ''
this.dontblock()
}
export function close() {
if (!this.opened) return
this.opened = false
this.image = 'images/door.png'
this.blockagain()
}npcs/plate.npc.ts
ts
// Serverside script of the pressure-plate NPC (paste into the NPC's
// serverside script in GRC's level editor). While any player stands on it,
// the door registered in the level's shared state stays open.
const CLOSE_DELAY_MS = 1500
export function onCreated() {
this.image = 'images/plate.png'
this.dontblock() // players must be able to walk onto it
this.pressed = false
}
export function onUpdate() {
const pressed = this.level.players.some((p: Player) => standingOn(this, p))
if (pressed === this.pressed) return
this.pressed = pressed
// The door registers itself in its onCreated; NPC creation order isn't
// guaranteed, so it may not be there yet — hence the ?.
if (pressed) {
clearTimeout(this.closeTimer)
this.level.door?.open()
} else {
this.closeTimer = setTimeout(() => this.level.door?.close(), CLOSE_DELAY_MS)
}
}
// Is the player's walking footprint (the lower half of their 2×2 body) on
// this 2×2 plate? On a gmap, players' x/y are gmap-global tiles while the
// NPC's are member-local, so convert the plate's position first —
// leveltogmap returns null on a plain level, where both are level-local.
function standingOn(plate: NpcThis, p: Player): boolean {
const g = leveltogmap(plate.level.name, plate.x, plate.y)
const x = g ? g.x : plate.x
const y = g ? g.y : plate.y
return p.x < x + 2 && p.x + 2 > x && p.y + 1 < y + 2 && p.y + 2 > y
}Going further
- More doors. Store a collection instead of one handle:
(this.level.doors ??= []).push(this), thenthis.level.doors.forEach(d => d.open()). - Named links. Give each plate a target name in its
onCreated(this.target = 'vault'), have doors register asthis.level['door:vault'] = this, and callthis.level['door:' + this.target]?.open(). - Local NPCs. A door spawned with
putnpcsurvives a level reload, but its registration doesn't. Have it register again, for example inonPlayerEnters.