Skip to content
KCKingdoms Connected Scripting APIScripting guides and API
GuideMaintainer authored

Lock doors, open gates

Lock, open and remove the level's own doors, open and close its castle gates, and store them by GUID.

The level places every house door in Bohemia and the castles’ drawbridges, portcullises and gates. The server can lock a door, open it, take it out of the world or drive a gate, and the state holds for every player. To add a door where the level has none, see Hang new doors.

// Lock whatever door the player is standing at.
async function lockDoorAt(player: Player): Promise<boolean> {
const answer = await Door.queryNearest(player);
if (!answer.found || !answer.door) return false;
answer.door.locked = true;
return true;
}

Door.queryNearest(player) always settles and never rejects. It resolves with found: false and a reason when no door is in sight, the client is in another level, it did not answer within five seconds, or the player left.

async function describeDoor(player: Player): Promise<void> {
const answer = await Door.queryNearest(player);
if (!answer.found || !answer.door) {
Chat.sendToPlayer(player, `No door: ${answer.reason}`);
return;
}
const label = answer.name || "unnamed";
Chat.sendToPlayer(player, `${label} (${answer.guid}), ${answer.distance?.toFixed(1)} m, locked: ${answer.locked}`);
}

guid, name, distance, open and locked are only present when found is true. open and locked are what that client sees now, which can be fresher than the server’s copy. There is no Door.nearest: only the client standing there can say which of thousands of doors it is.

A Door handle has:

Property What it is
guid The EntityGuid, sixteen lowercase hex digits, the same on every machine. A level door’s never changes; a hung door’s lasts until the server stops.
locked Writable. Reaches every client that has the door, including one standing at it.
open Read-only. door.setOpen(open, fromFront?) opens or closes it on every client, playing the game’s own swing, and returns false when it is already that way. Opening a locked door unlocks it.
stateToken Bumped on every durable change, to tell one change from the next.
spawned true for a door a script or a World Builder export hung, false for a level door.
model The leaf mesh, objects/characters/assets/doors/...cdf.

Store guid, not id: the id is a replication handle, the GUID is the level’s identity. Door.find(guid, world?) takes one back, with or without an 0x prefix.

A level door’s GUID never changes, and every level door exists from the moment the server starts, so saved locks can be put back as soon as your resource starts:

const saved = ["00a1b2c3d4e5f607"]; // loaded from your own storage
for (const guid of saved) {
const door = Door.find(guid);
if (door) door.locked = true;
}

Door.find returns null for a GUID the level does not place, or for a door taken out of that world. Door.find and Gate.find search the global virtual world unless you pass another. Each virtual world has its own copy of a level door’s state, built the first time anything there reaches it. A hung door’s GUID changes every run; save its toJSON() instead (see Hang new doors).

Door.remove(guid, virtualWorld?) takes one of the level’s doors out of a virtual world, collision included. Without a world it is taken out of the global one, and so out of every world. door.destroy() on a level door does the same in that handle’s world; on a hung door it takes the door down for good.

// Take the door a player stands at out of every world.
async function removeDoorAt(player: Player): Promise<void> {
const answer = await Door.queryNearest(player);
if (answer.found && answer.door && !answer.door.spawned) Door.remove(answer.door.guid);
}

While a door is out of a world, players there and players in the global world stop seeing it, Door.all() and Door.find leave it out, the server refuses anyone working it, Door.queryNearest does not find it, and server-spawned NPCs and navigation queries in that world treat the doorway as open.

A removal is a level edit: it shows in LevelEdit.all(), LevelEdit.restoreAll() puts the door back, and a level edit that swaps the door for another mesh counts as removing it. Removing a door already out does nothing; removing one that has a move or a swap in that world replaces that edit. Like every level edit it is kept in memory only, and undone when the resource that made it stops.

Door.removed(virtualWorld?) lists the level doors that are out, whether a script or a World Builder export took them out: every world when omitted, or only removals made in that world. Each row has guid, name, position and virtualWorld. Door.restore(guid, virtualWorld?) puts one back, closed and locked as the level builds it, and returns its handle, or null when that world has no such removal. A door taken out of the global world is put back only from there.

for (const row of Door.removed()) Door.restore(row.guid, row.virtualWorld);

A gate’s kind is drawbridge or portcullis, which animate, or gate, a pair of leaves that opens and closes at once (the Ratborsch and Nebakov fortress gates and the Ruthard palace gate). Every gate exists from the moment the server starts, and its position comes from the level’s data, so Gate.nearest answers on the server with no round trip. Use a radius of tens of metres.

const gate = Gate.nearest(player.position, 60, player.virtualWorld);
if (gate) {
const result = gate.toggle();
Chat.sendToPlayer(player, result.accepted
? `The ${gate.kind} is ${result.opening ? "opening" : "closing"} (${result.seconds.toFixed(1)} s).`
: `The ${gate.kind} ${result.reason}.`);
}

toggle(true) opens, toggle(false) closes, and no argument heads away from the current end, reversing a moving gate. Every client plays the motion from the server clock, so late arrivals see the right point. “Open” always means passable: portcullis raised, drawbridge lowered. Gate.find(guid, world?) works like Door.find.

Property What it is
cycle closed, opening, open or closing. (Not state, which is the entity’s state bag.)
openness 0 shut to 1 fully open, moving during a cycle.
moving Whether a cycle is running.
openDuration, closeDuration Clip lengths in seconds. 0 means no clip that way, and always for a gate.
Call Fails when Result
Door.queryNearest No door in sight, other level, 5 s timeout, player left. Resolves found: false with a reason. Never rejects.
Door.find, Gate.find, Gate.nearest, getById No such door or gate there, or the door is taken out of that world. null.
door.setOpen Already that way. Returns false.
Door.remove The GUID is not one of the level’s doors, or names a hung door. Throws. Take a hung door down with destroy().
Door.restore That world has no such removal. null.
gate.toggle() Already at that end, or no clip that way. A GateToggle with accepted: false, a reason that reads after the gate’s name (is already open) and seconds: 0. Never throws.

Locking and opening level doors spawns nothing, so nothing needs cleanup. A lock stays until you change it or the server restarts.