Appearance
Levels & gmaps
A level is one .glvl file: a rectangular grid of tiles with its own collision map, NPCs and links. A gmap is a .gmap file that stitches a grid of same-sized levels together into one seamless world. This page covers how scripts read and change levels, how coordinates work on both, and the traps where the two differ.
Levels are authored in GRC's Level Editor (and gmaps in its gmap editor); scripts mostly read them, but a server-authoritative API can also paint tiles and collision, edit the level's NPCs and links, create blank levels, and save, reload or reset levels at runtime — enough to build an in-game online editor.
What's in a level
| Part | What it is | Editable from scripts? |
|---|---|---|
| Tile layers | One or more grids of tile ids. Layer 0 is the ground; higher layers draw on top. | Yes — level.setleveltile (server) |
| Collision layer | One collision type per tile (1 = walkable, 22 = blocking, …). | Yes — level.setleveltiletype / setleveltiletypes (server) |
| NPCs | Baked NPCs with a server and a client script half. | Yes — level.getlevelnpcs / addlevelnpc / setlevelnpc / removelevelnpc / reloadlevelnpc (server) |
| Links | Rectangles that warp a player who walks into them. | Yes — level.getlevellinks / addlevellink / setlevellink / removelevellink (server) |
A tile id is a row-major index into the layer's tileset image (by default pics1.png, 16×16-pixel tiles): id = ty * tilesetWidthInTiles + tx. For the default 128-tile-wide tileset, the tile at column 3, row 2 is 2 * 128 + 3 = 259. The id 65535 means empty — nothing is drawn on that layer there.
Collision types follow Graal's numbering (11 water, 12 lava, 20 throw-through, 21 jump stone, 22 blocking, …) and are stored as authored, but the engine currently only gives 22 a built-in meaning: it blocks movement. The others are readable with tiletype so your scripts can give them meaning (e.g. damage a player standing on type 12).
Tilesets
Every layer draws from its own tileset, so a level can keep its ground on pics1.png and put trees or buildings from another sheet on a layer above. A layer either names a tileset image (with its size in tiles) or inherits the level's tileset — the default for every layer, and for levels saved before per-layer tilesets existed. On a gmap, inheriting layers use the gmap's tileset.
In GRC's Level Editor, the Layers panel adds (+) and removes (−) layers. Its Level tileset row (Gmap tileset on a gmap, where it changes every member) sets the tileset that inheriting layers draw from, and each layer's own tileset button picks an image for just that layer, or sets it back to inherit. The size in tiles is read from the image. The tileset palette always shows the active layer's tileset. A layer's tile ids index its tileset, which is why gettile and setleveltile ids depend on the layer.
Seasonal tilesets: replaceTiles
replaceTiles(from, to) swaps a tileset for this player only, the same way replaceAni swaps animations. Every layer drawing from from — in the current level, on gmap members, and in every level entered later — draws from to instead:
ts
function onCreated() {
const month = new Date().getMonth()
if (month === 11 || month <= 1)
replaceTiles('pics1.png', 'pics1-winter.png')
}
function onPlayerChats(who: ChatPlayer, chat: string) {
if (chat === '/summer') clearTiles() // or clearTiles('pics1.png')
}Rules worth knowing:
frommatches a layer's tileset name exactly ('images/pics1.png') or by bare filename ('pics1.png'matches it in any folder), ignoring case.- The replacement must be the same pixel size as the original, since tile ids keep their meaning. A missing or differently-sized image is ignored (logged to the client console) and the original keeps drawing.
- The swap is live: the old tileset stays on screen until the new one has downloaded, then switches with no blank frame.
- Replacements are one level deep.
replaceTiles(from, from)removes one, and clearTiles removes the named ones (or all). - They last for the session. Removing the weapon that set them doesn't undo them.
player.replacedTileslists them, e.g.{ 'pics1.png': 'pics1-winter.png' }. - It is purely visual: collision,
gettileand other players are unaffected.
Coordinate units
Everything in the level API is in tiles, and fractions are allowed.
- One tile is 16 pixels. Tile
tstarts at world pixelt * 16. - A player is 2×2 tiles;
player.x/player.yis the top-left corner, so the center is(player.x + 1, player.y + 1). - A player's walking footprint — what collides with walls — is only the lower body: the 2×1 rectangle at
(player.x, player.y + 1). - On a gmap, positions are gmap-global tiles (see Gmaps).
Screen pixels (mouse position, GUI, screen-space images) are a different space; converting between them is covered in Camera, input & movement.
Which level am I in?
| Side | Expression | Gives you |
|---|---|---|
| Server, any player handler | player.level | A ServerLevel object (or null before their first level) |
| Server, inside a player-scoped handler | the level global | The triggering player's level |
| Serverside NPC script | this.level | The NPC's own NpcLevel |
| Client | player.level | The level (or gmap) name, a string |
ServerLevel and the level global expose the level's name plus onwall, tiletype, shoot and putnpc. The difference is binding:
player.levelis bound to that level by name. You can keep it and use it later — in a timer, after anawait— and it still works.levelmeans "the current player's level", and a current player only exists synchronously inside a player-scoped handler (onActionServerSide,onPlayerJoined, …). In a timer callback or after anawait,level.nameis''and its methods returnnull.
ts
// server weapon half
export function onActionServerSide(player: Player, action: string) {
const lvl = player.level // bound by name: safe to keep
if (!lvl) return
setTimeout(() => {
// `level.shoot(...)` would return null here — no current player.
lvl.shoot(player.x + 1, player.y + 1, 'arrow', 0, 10, 3)
}, 1000)
}NPC scripts: always this.level
Inside a serverside NPC script a bare level silently resolves to the player-scoped global above, not to the NPC's level. Use this.level — it's the NPC's own level and accepts member-local coordinates on gmaps.
Reading tiles and collision
Clientside
The client knows the level it is currently in, and reads it directly (these are direct calls — they return live values, safe to use every frame):
- gettile(layer, x, y) — the tile id on a layer,
65535for empty,-1when out of range or no level is loaded. - tiletype(x, y) — the collision type of the tile containing
(x, y), or-1. - tiletyperect(x, y, w, h) — the collision types of a
w×hblock of tiles in one call, as rows:result[row][col]. Cells outside the level are-1. Use it instead of atiletypeloop when scanning an area. - onwall(x, y, w?, h?) —
truewhen a blocking tile intersects the rectangle[x, x+w) × [y, y+h). Omitw/hto test one tile. Tiles outside the level count as blocked, and so do blocking NPCs.
ts
// weapon client half: is the player standing in lava?
export function onUpdate() {
if (tiletype(player.x + 1, player.y + 1.5) === 12)
triggerServer('weapon', this.name, 'burn')
}
// Would the player collide one tile to the right?
const blocked = onwall(player.x + 1, player.y + 1, 2, 1)
// Is there water anywhere in the 9x9 area around the player?
const area = tiletyperect(player.x - 3, player.y - 3, 9, 9)
const nearWater = area.some(row => row.includes(11))The (player.x, player.y + 1, 2, 1) rectangle is the walking footprint; custom movement code should test exactly that (see custom movement).
Serverside
ServerLevel.onwall / tiletype and the NPC equivalents NpcLevel.onwall / tiletype have the same shape. What counts as "blocking" differs slightly:
| Query | Blocking tiles | Blocking NPCs | Players |
|---|---|---|---|
Client onwall | yes | yes | no |
ServerLevel.onwall (player.level) | yes | yes | no |
NpcLevel.onwall (this.level in an NPC) | yes | yes (never the calling NPC itself) | yes — their lower bodies |
The NPC version counts players so a walking NPC doesn't step onto someone. Whether an NPC blocks at all is controlled by dontblock / blockagain.
To read raw tile ids and collision types — the server's current copy, unsaved edits included — every level object has getleveltile(layer, x, y) and getleveltiletype(x, y). They take whole tiles in the object's own coordinates and return -1 when out of range.
TIP
There is no clientside setleveltile — the client only has the level it's standing in, and every edit goes through the server (see below).
Finding players
Every serverside level object — player.level, the player-scoped global level and an NPC's this.level — can list the players in it and search them by area:
| Member | Returns |
|---|---|
players | Everyone in the level (on a gmap: anywhere on the gmap). |
findPlayersCircle(x, y, radius) | Players whose body center (x + 1, y + 1) is within radius tiles, nearest first. |
findPlayersRect(x, y, w, h) | Players whose 2×2 body overlaps the rectangle. |
The results are full Player objects, so client. / clientr. flags are readable (and writable) straight away:
ts
// Serverside: heal everyone on the team standing near the fountain.
for (const p of player.level?.findPlayersCircle(30, 30, 5) ?? []) {
if (p.clientr.team === 'red') p.clientr.hp = 100
}On a gmap the whole gmap is searched, so players across a seam count. Players' x/y are always gmap-global there. player.level and the global level take gmap-global coordinates too. An NPC's this.level takes its member-local coordinates and converts them for you (see NPCs).
this.level.players in an NPC is slightly different: it lists everyone who has the level loaded, which on a gmap includes players in neighbouring members.
Pathfinding
findPath(x1, y1, x2, y2, options?) runs an A* search over whole tiles and returns the route as PathPoints, or null when there is none. The route only has its turning points, from start to goal, so a straight corridor comes back as two points. Start and goal are floored to whole tiles and mark the top-left of the mover's footprint. Each cell on the route is tested like onwall(x, y, width, height):
| FindPathOptions | Default | Effect |
|---|---|---|
width, height | 1, 1 | Footprint size in tiles (max 16). Players and character NPCs walk with a 2×1 lower body: { width: 2, height: 1 }, start/goal at (x, y + 1). |
diagonal | true | Allow diagonal steps. They never squeeze between two wall corners. |
npcs | true | Blocking NPCs are walls. In an NPC script, the NPC never blocks itself. |
players | false | Players' lower bodies are walls. |
maxNodes | 4000 | Search budget (max 20000). Running out returns null. |
ts
// From the player to the shop door, walking like a player does.
const route = player.level?.findPath(player.x, player.y + 1, 41, 12 + 1,
{ width: 2, height: 1 })Searches run on the server tick
A path across an open 64×64 level is cheap, but an unreachable goal makes A* explore everything it can reach before giving up. Keep maxNodes low when you search often, and re-plan every few steps rather than every tick. The route is a snapshot: movers that cross it after the search aren't accounted for, so use move's blockcheck while walking it.
On a gmap, paths cross member seams. player.level and the global level use gmap-global coordinates, while an NPC's this.level uses member-local coordinates in and out, so its waypoints can fall outside its own .glvl. For a complete walking NPC see Chasing a player.
Changing levels at runtime
Levels are edited through level objects. Every serverside level object has the LevelEditor methods covered below:
player.levelis the player's level.- findlevel(name) looks up any level or gmap by file name, whether or not anyone is in it. It returns
nullif there's no such level. - The player-scoped global
leveland an NPC'sthis.levelhave the same methods.
ts
const town = findlevel('town.glvl')
if (town) {
town.setleveltile(0, 10, 4, 259)
town.savelevel()
}Painting tiles
Tile painting is server-authoritative. A client that wants to paint sends the request with triggerServer; the server calls setleveltile, and the change comes back to every player in the level (the painter included) as a tile update:
ts
// weapon server half
export function onActionServerSide(player: Player, action: string, x: number, y: number, tile: number) {
if (action !== 'paint' || !player.level) return
player.level.setleveltile(0, x, y, tile)
}level.setleveltile(layer, x, y, tileId) returns false for out-of-range arguments (including a layer the level doesn't have). Repainting a tile with the id it already has is a no-op and isn't broadcast.
The edit lives only in the server's in-memory copy of the level. It is written to the .glvl file when someone saves the level:
- Serverside, level.savelevel() saves it (see Saving, reloading and resetting).
- Clientside, updatelevel() asks the server to save the level the player is standing in.
updatelevel is tied to the tileeditor weapon
The server only honours the save request from players who have the tileeditor weapon; for anyone else it is silently ignored. It also saves by the player's current level name, so it does nothing while the player is on a gmap (see below). Treat it as the tile editor's save button rather than a general-purpose API.
Unsaved edits are lost on server restart. If someone uploads a new version of the .glvl from GRC in the meantime, the uploaded file wins and the unsaved in-game edits are discarded.
Changing tile types at runtime
The collision layer is separate from the tile graphics — painting a wall tile doesn't make it block. Set collision types with setleveltiletype(x, y, type) for one tile or setleveltiletypes(x, y, w, h, types) for a rectangle, where types is a single type for the whole rect or a row-major array of exactly w * h types:
ts
// weapon server half: paint a brush of collision for the online editor
export function onActionServerSide(player: Player, action: string, x: number, y: number, w: number, h: number, type: number) {
if (action !== 'painttypes' || !player.level || !player.hasstaffright('leveleditor')) return
player.level.setleveltiletypes(x, y, w, h, type) // e.g. 22 = blocking
}Types are 1-255 (see What's in a level). The rect must lie inside the level (on a gmap it may cross member seams, but not holes), and the call is all-or-nothing: one bad value fails it and nothing changes. Like tile paints, changes go into the in-memory copy and reach every player who sees the level at once — as a single update per level touched — so movement, onwall and tiletype (both sides) and light occlusion pick them up immediately. They're persisted by saving the level.
Editing links
Links are edited like NPCs but go live like tiles: getlevellinks() lists them as LevelLinkInfo, and addlevellink(data), setlevellink(index, patch) and removelevellink(index) change them, with fields from a LevelLinkPatch:
| Field | Meaning |
|---|---|
x, y, w, h | The trigger rect in whole tiles. It must be at least 1×1 and inside the level. |
destlevel | Where the link goes: a level or gmap file name. Required. |
destx, desty | Arrival position in the destination, in tiles. |
keepx, keepy | Keep the player's current x / y instead (Graal's playerx / playery), for seamless edge links. |
ts
// A door at (30, 12) into the house; arrive at the house's entrance.
const lvl = findlevel('town.glvl')
const i = lvl?.addlevellink({ x: 30, y: 12, w: 2, h: 1, destlevel: 'house.glvl', destx: 15, desty: 28 }) ?? -1
// Later: move it one tile right.
if (i >= 0) lvl!.setlevellink(i, { x: 31 })Every change sends the level's new link list to everyone who sees the level, so walking into a new link works straight away. A link that appears under a player only fires once they step out and back in. An edit that would leave an invalid link (no destlevel, an empty rect, a rect outside the level) fails and changes nothing. The destination isn't checked: a link to a missing level does nothing when touched. Indices shift down after removelevellink, so re-read getlevellinks() afterwards. Like everything else, links reach the .glvl file on savelevel().
Saving, reloading and resetting
A level has two states on the server: its file and its in-memory copy, which exists once anything edits it (tiles, tile types, NPCs, links, or createlevel). Three level-object methods move between them:
| Method | Effect |
|---|---|
| savelevel() | Writes the in-memory copy to the .glvl file. Running NPCs and players aren't touched. The level's NPC scripts are type-checked in the background (errors go to the server log). true when there was nothing to save. |
| reloadlevel() | Applies the current server state to everything running: the level's NPCs are recreated from its NPC list (onCreated fires again) and every player who sees it reloads it in place. This is how staged NPC edits go live all at once; reloadlevelnpc(index) restarts a single NPC without reloading the level. |
| resetlevel() | Discards all unsaved edits and reloads from the file, exactly like reloadlevel. Fails for a createlevel level that was never saved. |
ts
// The editor's buttons
const lvl = player.level
if (action === 'save') lvl?.savelevel()
if (action === 'apply') lvl?.reloadlevel()
if (action === 'revert') lvl?.resetlevel()On a gmap object the three apply to the members that need it: savelevel saves every member with unsaved edits, reloadlevel reloads every member with staged NPC changes, and resetlevel resets every edited member. Objects for an instance can read the shared level but never edit it.
Tile and tile-type edits are live as soon as they're made, so reloadlevel mostly matters for NPC edits made without live (or use reloadlevelnpc for one NPC) — but reloading also re-sends the whole level, which is a handy resync after many edits. Local NPCs from putnpc survive both reloadlevel and resetlevel.
Gate editing yourself
None of the editing methods check staff rights — they're serverside, so only your scripts can call them, and it's up to the script to decide who may. Check player.hasstaffright('leveleditor') and/or player.hasright('w', 'levels/' + name) before acting on a client's request.
Creating levels
createlevel(name, fillTile, width?, height?) makes a blank <name>.glvl, filled with fillTile on layer 0 (use 65535 for empty). Rules:
nameis 1-32 letters, digits,_or-, without the extension.- Sizes are 8-512 tiles per side; the default is 64×64.
- It fails if any level — saved or not — already uses the name.
- The new level has one tile layer and all-walkable collision.
It returns '' on success or a player-presentable error message, and the level can be warped into (or opened with findlevel) immediately. Like tile edits, it only reaches disk when saved; unsaved levels vanish on restart.
ts
const error = createlevel('arena', 0, 32, 32)
if (error === '')
player.warpto('arena.glvl', 16, 16)
else
triggerClient('weapon', this.name, 'error', error)Copying and deleting levels
copylevel(original, newName) writes a copy of a level to a new .glvl file in the same folder as the original. It works differently from createlevel:
- The copy goes straight to disk, so there's no
savelevelstep. - It copies the original's current state, unsaved in-game edits included: tiles, collision, NPCs, links and tileset.
newNamefollows the createlevel rules. The.glvlextension is optional.- It fails if any level, saved or not, already uses the new name.
deletelevel(name) removes a level for good, both its file and any unsaved in-memory copy. Players don't block it:
- Everyone in the level is warped to the server's start position first. That includes players in an instance of it.
- The level's NPCs, local ones included, are then unloaded and their timers stop.
- It refuses gmaps, gmap members and the server's start level.
Both return '' on success or a player-presentable error message. Neither checks rights, so gate them the same way as the editing methods. Run deletelevel from a weapon or another level's NPC, because an NPC that deletes its own level unloads itself.
ts
// A personal arena per player, cloned from a template level
const arena = 'arena_' + player.account
if (!findlevel(arena + '.glvl')) {
const error = copylevel('arena_template.glvl', arena)
if (error !== '')
return triggerClient('weapon', this.name, 'error', error)
}
player.warpto(arena + '.glvl', 16, 16)
// ...and when it's no longer needed
deletelevel(arena + '.glvl')Moving between levels
Only the server moves players between levels: player.warpto(level, x, y) (returns false when the level doesn't exist). There is no clientside warp — a client that wants to travel asks its server half via triggerServer.
Levels can also carry links (drawn with the Level Editor's Link tool): when a player's feet enter a link rectangle the server validates it and warps them to the link's destination, optionally keeping the X or Y coordinate. Links need no scripting.
For a private, replayable copy of a level or gmap (a quest dungeon, a boss arena), warp players into an instance. Links between an instance's member levels stay inside it, and links to anything else lead back to the shared world.
When a player changes level, the client keeps showing the old level until the new one — its tiles, tileset, and on a gmap the surrounding cells — has fully downloaded, then swaps. Clients cache recently visited levels and only re-download a level whose bytes have changed.
Hot reload
Saving a .glvl from GRC's Level Editor reloads the level for everyone in it: players stay where they are (clamped if the level shrank), and its NPCs are re-created, so their onCreated runs again.
Gmaps
A gmap arranges member levels in a grid (created and resized in GRC's gmap editor). All members must have the same dimensions, and every cell must be filled. Players walk across member seams continuously, and scripts see a single world:
- While a player is on a gmap, their level is the gmap:
player.levelnames the.gmapfile (e.g.world.gmap), andplayer.x/player.yare gmap-global tiles. - Clientside,
gettile,tiletype,tiletyperectandonwallall take gmap-global coordinates too. - Warping to a member level (
player.warpto('world-b1.glvl', 10, 10)) automatically translates onto the gmap; you don't need to convert first. - One-tile edge links between neighbouring members (from levels that were designed stand-alone) are ignored on a gmap, so walking across a seam never warp-loops. Interior links still work.
- Clients stream a window of member levels around the player (5×5 by default; the
gmapLoadDistanceserver option), and players see each other within a smaller window (gmapPlayerDistance, 3×3 by default).
Converting coordinates
When you need to know which member a gmap position is in — or where a member-local position lands on the gmap — convert explicitly:
- gmaptolevel(gmap, gx, gy) →
{ level, x, y }: the member level under a gmap-global point, and the member-local coordinates.nullif the gmap doesn't exist or the point is off the grid. - leveltogmap(level, x, y) →
{ gmap, x, y }: the reverse.nullwhen the level belongs to no gmap.
ts
// server: which member level is this player standing in?
const where = gmaptolevel(player.level!.name, player.x, player.y)
if (where) echo(`${player.account} is in ${where.level} at ${where.x},${where.y}`)Member file names don't necessarily map to grid positions the way you'd guess — check with leveltogmap rather than assuming an origin.
Both functions exist clientside with the same shapes (gmaptolevel, leveltogmap), but the client only knows its current gmap: pass '' (or the current gmap's name) as gmapName, and leveltogmap only resolves members of the current gmap. Anything else — or not being on a gmap — returns null.
Tile edits on a gmap
A gmap's level object (player.level while on the gmap, or findlevel('world.gmap')) takes gmap-global tiles for every tile method and routes each one to the member level underneath, so painting needs no conversion:
ts
export function onActionServerSide(player: Player, action: string, gx: number, gy: number, tile: number) {
if (action !== 'paint' || !player.level) return
player.level.setleveltile(0, gx, gy, tile) // gmap or plain level alike
}Everyone viewing that member — on the gmap or in the member level directly — receives the change. A setleveltiletypes rect may span several members; it fails if any part lands on a hole or outside the gmap.
NPC and link editing need one level, so the NPC and link methods return null/false/-1 on a gmap object. Open the member instead — gmaptolevel tells you which:
ts
const m = gmaptolevel('world.gmap', player.x, player.y)
const member = m ? findlevel(m.level) : null
member?.addlevelnpc({ x: m!.x, y: m!.y, image: 'images/sign.png' })Other gmap-aware APIs
- ServerLevel from
player.levelon a gmap is bound to the gmap: itsonwall,shoot,putnpc,findPlayers*andfindPathtake gmap-global coordinates (putnpcdrops the NPC into the member under the point). - An NPC's this.level is its member level: coordinates are member-local.
- Projectiles fly in gmap-global space and cross seams; see Projectiles.
See also
- NPCs — the scripts baked into levels,
putnpc, blocking - Camera, input & movement — screen vs world space, custom movement
- Players —
player.warpto, positions - Reference: server globals, client globals