Appearance
Players
Players look different depending on which side you're scripting:
- Serverside, each player-scoped event hands you a Player object for the player involved. The server has authority, so it can warp players, grant weapons, check staff rights and write any flag.
- Clientside, the global player is always the local player, the one running the script. Every loaded player, you included, is in the read-only players array (or look one up with findplayer), and events like
onPlayerChatshand you ChatPlayer snapshots.
Server scripts can attach public values to a player, such as health or team, with player attributes. Every client that can see the player reads them.
Serverside: the Player object
You get a Player as the first argument of onPlayerJoined, onPlayerLeft, onActionServerSide, and player-carrying NPC events (onPlayerEnters, onPlayerChats, onAction<Name>).
Finding a player by account
Outside a player event (in a timer, onInitialized, or a chat command naming someone else), look a player up with findplayer. It returns the same full Player object, flags included, or null when that account isn't online on this server. The account name is case-insensitive.
ts
// Serverside: pay out a bounty to whoever is named, if they're here.
const winner = findplayer('SomeAccount')
if (winner) {
winner.clientr.gold = (winner.clientr.gold ?? 0) + 100
sendPM(winner, 'You collected the bounty!')
}The result is a snapshot like any other Player: call findplayer again rather than holding on to it across awaits or timer ticks. To search by position instead, see Finding players.
Every online player: allplayers
allplayers is an array of every player online on this server, as full Player objects. Like findplayer, it works without a player context.
ts
// Serverside: a daily login reward for everyone currently online.
const online = allplayers
for (const p of online) {
p.clientr.gold = (p.clientr.gold ?? 0) + 50
sendPM(p, `Daily reward! ${online.length} players online.`)
}Each read builds a fresh snapshot, so store it in a local instead of reading allplayers[i] inside a loop. Two reads give different objects for the same player, so compare players by account or id rather than ===.
Identity
| Member | Notes |
|---|---|
id | Server-assigned, unique per login. A relog gets a new id |
account | The account name. Stable, so use it as a key for anything persistent |
nick | Display name under the player, read/write. Defaults to the account. Assign '' to reset. Sanitized like chat and trimmed to 40 characters |
nickcolor, nickshadow, nickshadowcolor | Name tag style, read/write: text color 'r,g,b' (default '255,255,255'), drop shadow on/off (default true), shadow color (default '0,0,0'). Only scripts can change these, and they're saved in the account file. Invalid colors are ignored |
ts
// A staff member's name in gold, without a shadow.
player.nickcolor = '255,215,0'
player.nickshadow = falsePosition and level (snapshots)
x, y (tiles) and level are captured when the event fired. They don't update while your handler runs, and a Player kept across an await or in a timer may be stale.
- player.level is a ServerLevel object (or
nullbefore the first level). Useplayer.level.namefor the file name, or act on the level directly (player.level.putnpc(...),.shoot(...),.onwall(...)), or find other players in it (.players,.findPlayersCircle(...),.findPath(...); see Finding players). On a gmap, it's the gmap, andx/yare gmap-global. Usegmaptolevelto find the member level. See Levels & gmaps. - Scripts can't set
x/ydirectly. The typings don't mark themreadonly, but the host has no setter. To move a player, warp them.
Warping
player.warpto(level, x, y) is the only way for scripts to move a player to another level, or to reposition them within one. There's no clientside warp. It follows the same path as link touches and RC warps: warping to a gmap member level lands on the gmap with translated coordinates, and the client switches over once it has downloaded the destination. It returns false if the level doesn't exist.
Inside an instance, a plain warpto stays in the instance when the level is one of its members. Pass { instance: inst } to warp into an instance, or { instance: null } to force the shared world. player.instance is the player's current instance (or null), and the clientside player.instanced flag tells their own scripts whether they're in one.
ts
// weapons/travel.ts: a client asks to go home; the server decides.
function onActionServerSide(player: Player, action: string) {
if (action === 'home')
player.warpto(serverOptions.startLevel, serverOptions.startX, serverOptions.startY)
}Appearance and animation (live)
These read and write the player's current state. Writes apply immediately for everyone in their level:
| Member | Notes |
|---|---|
chat | The chat bubble. Assigning shows it to everyone in the level, the player included. '' clears it. It fades 5 s after the player moves. See Chat & PMs |
ani / setAni(name) | Current .gan animation name, no extension. player.setAni('sword') is the same as assigning ani |
head, body | Image names like 'head0.png'. '' means the client default. Persisted |
colors | Five 'r,g,b' strings: [0] skin, [1] coat, [2] sleeves, [3] shoes, [4] belt. Assign one slot (player.colors[1] = '255,0,0') or an array. Invalid values are ignored. Persisted |
ts
function onPlayerJoined(player: Player) {
if (player.account === 'Astram') {
player.colors[1] = '200,40,40' // red coat for the admin
player.chat = 'The admin has arrived'
}
}Weapons
addWeapon, removeWeapon and hasWeapon manage the player's weapon grants, which are persisted in their account. See Weapons.
Flags
player.client and player.clientr are the player's flag bags. Both are persisted in the account file. client is also writable by the player's own client, and clientr is server-write-only. See Flags.
player.attr is a third bag. Its values reach every client that can see the player, not just the player's own client. See Player attributes.
Staff rights
player.hasright(mode, path) checks this player's folder rights on a data/-relative path ('r', 'w' or 'rw'). Use it to gate staff-only chat commands or tools in-game:
ts
function onActionServerSide(player: Player, action: string, level: string) {
if (action === 'resetlevel' && !player.hasright('w', 'levels/' + level)) return
// ...
}Staff-list accounts without a rights record pass every check. Everyone else without a record fails. See Staff rights & server options.
Clientside: the local player
The global player is the player running the script. Reads are live.
| Member | Access | Notes |
|---|---|---|
id, name, account | read | name and account are both the account name |
nick | read | Change it with the built-in setnick <name> chat command, or from a server script. Your own name tag is hidden, and showname flashes it for 5 s |
level | read | The current level (or gmap) name as a string, not an object |
x, y | read/write | Tiles. Writes are batched and clamped to the level bounds when they apply |
dir | read/write | 'up' | 'down' | 'left' | 'right' |
chat | read/write | Assigning shows the bubble locally and sends it to everyone in the level |
head, body, colors | read/write | Like the server's, synced to everyone and saved to the account. Invalid names are dropped |
ani | read | The gani actually playing, including replacements (see below) |
replacedAnis | read | Current replaceAni table, e.g. { idle: 'handgun-idle' } |
zoomfactor | read/write | Clientside world zoom (default 1; 2 = twice as big). Applies directly, not batched. See Zoom |
hasWeapon(name) | call | Whether the server has granted the weapon (true even while it downloads) |
Assigning anything else throws. Writes to x/y/dir/chat/head/body/colors are batched. You read your own writes back immediately, and many writes in one handler fold into one update.
Moving the player by script usually goes with turning off the built-in arrow-key movement. See Camera, input & movement.
Animations: setAni, replaceAni, clearAnis
setAni(name) plays a .gan on the local player, and the server relays it to everyone else in the level. Re-setting the current animation restarts it, unless it's a continuous one like the default walk. When a non-looping animation ends, its SETBACKTO chain (usually back to idle) resumes the built-in idle/walk switching.
ts
function onKeyPressed(key: string) {
if (key === 'S') setAni('sword')
}Values after the name are the animation's params, sent along to everyone: setAni('carry', 'pot.png'). The gani's PARAM sprites draw the images they name, and its script reads them as params. Every setAni replaces them, and player.aniParams reads them back. See setAni params.
replaceAni(from, to) swaps a default animation for this player. Wherever from would play (setAni(from), the built-in idle/walk switching, a SETBACKTO, a server-set animation), to plays instead, and everyone else sees to. This is how a gun changes your idle and walk poses:
ts
function onEquipped() {
replaceAni('idle', 'handgun-idle')
replaceAni('walk', 'handgun-walk')
}
function onUnequipped() {
clearAnis('idle', 'walk') // or clearAnis() to reset every replacement
}Rules worth knowing:
- Replacements are one level deep and don't chain.
replaceAni(from, from)removes one. - They last until clearAnis or the end of the session. Removing the weapon that set them doesn't undo them, so clean up yourself.
replaceAni/clearAnisare direct calls.player.replacedAnisreflects them immediately. The visible switch happens on the next frame.player.anireports the gani actually playing. With idle replaced, it reads'handgun-idle', not'idle'. Compare against the logical name like this:
ts
const isIdle = player.ani === (player.replacedAnis.idle ?? 'idle')Tilesets have the same kind of per-player swap, replaceTiles / clearTiles, for seasonal looks.
On the server, player.setAni / player.ani = ... plays an animation on any player (player.setAni(name, ...params) sends params too). On the local player it goes through the same replacement table.
Other players on the client: players
The global players array holds a read-only ClientPlayer handle for every player loaded on this client: the local player first, then everyone else it currently draws (the players sharing your level or gmap).
| Member | Notes |
|---|---|
id, name, account | name and account are both the account name. id matches player.id and ChatPlayer.id |
nick, nickcolor, nickshadow, nickshadowcolor | The name tag text and style, as set by setnick and server scripts |
x, y, dir | Tiles, in the same space as player.x/y and world-mode findimg |
chat, ani, head, body, colors | Current chat bubble, playing gani, look and body colors |
aniParams | The playing gani's setAni params, read-only |
islocal | true for your own entry |
attr | The player's attributes, read-only |
ts
for (const p of players)
if (!p.islocal && p.chat === 'help') echo(`${p.nick} needs help`)
// From an event snapshot to a live handle:
function onPlayerChats(who: ChatPlayer, chat: string) {
const p = players.find(h => h.id === who.id)
}- Reads are live. A handle always shows the player's current state, so you can keep one between frames.
- Handles are stable. Each read of
playersreturns a new array, but the same player is the same object every time, so===andMapkeys work. - Read-only. Assigning a handle property throws. Change your own player through player, and other players from a server script.
- When a player leaves your view, they drop out of
players. A handle you kept then reads defaults ('',0).
Event snapshots are still handy. onPlayerChats(player, chat) and clientside NPC onPlayerEnters(player) hand you a frozen ChatPlayer, the player as they were at that moment (attr included).
Finding a visible player: findplayer
findplayer(account) returns the handle of the player logged in with that account, or null. It only searches the players this client can see: the same set as players, including you. The account name is case-insensitive, and the result is the same object players holds.
ts
const rival = findplayer('SomeAccount')
if (rival) echo(`${rival.nick} has ${rival.attr.health ?? '?'} HP`)The serverside findplayer is different: it finds any player online on the server and returns a full Player.
Hiding the built-in name tags: disabledefnicks
disabledefnicks() hides the built-in name tags, the nicknames drawn on the ground under every player and NPC (your own showname/setnick flash included). Use it for name-free modes (hide-and-seek, cutscenes) or to draw your own tags. enabledefnicks() shows them again. Both are batched. The setting stays until a script changes it: removing the weapon that called disabledefnicks() doesn't bring the tags back. Nicknames and their styles keep syncing either way, so players can draw replacements:
ts
// weapons/nametags.client.ts: draws everyone's tag in their own nick color.
const BASE_ID = 1000
let drawn = new Set<number>()
export function onCreated() {
disabledefnicks()
}
export function onUpdate() {
const now = new Set<number>()
for (const p of players) {
const tag = findimg(BASE_ID + p.id)
tag.text = p.nick
tag.style = 'bc' // bold, centered on x
tag.tint = p.nickcolor
tag.textshadow = p.nickshadow
tag.shadowcolor = p.nickshadowcolor
tag.layer = 0 // on the ground, under the players
tag.x = p.x + 1 // the body is 2 tiles wide
tag.y = p.y + 2.1 // just below the feet
now.add(p.id)
}
// Players who left the view: remove their tags.
for (const id of drawn)
if (!now.has(id)) hideimg(BASE_ID + id)
drawn = now
}Player attributes: attr
Attributes are public per-player values. Only server scripts can write them, and every client that can see the player reads them. That includes the player's own client. Use them for anything other players need to draw: health, team, title, a status icon.
- Serverside, player.attr is a read/write bag on every
Player, whether it comes from an event,findplayer,level.playersor anywhere else. - Clientside,
.attris a read-only bag on every handle: your own player.attr, each entry ofplayers,findplayer(...)results andChatPlayersnapshots. In a gani script,player.attris the attributes of the player playing the animation. - In a GUI, a GuiImageCtrl actor draws ATTR sprites from its own
actor.attr, or from a live player's attributes withactor.player.
ts
// Serverside
player.attr.health = 100
player.attr.team = 'red'
delete player.attr.team // or assign null / undefined
// Clientside: every visible player, you included
for (const p of players)
if (p.attr.team === player.attr.team) echo(`${p.nick} is on your team`)The rules:
- Values are JSON, with the same rules as flags. Names are 1-128 characters and each value can be up to 8192 characters of JSON. A player's attributes together (names plus values) are capped at 16384 characters. A write that breaks a limit throws.
- Reassign to write. A read returns a copy, so mutating a nested value does nothing. Build the new value and assign it.
- Every change is broadcast right away to everyone who can see the player. Assigning the value an attribute already has sends nothing. Still, don't write every tick: change an attribute when the value actually changes.
- Visibility follows the player list. Clients get a player's current attributes when the player comes into view (same level, or nearby on a gmap) and live changes after that.
- Read-only on the client. Values are deep-frozen, and assigning to
attrthrows. A client asks the server to change something withtriggerServer, and the server decides. - Persisted. Attributes are saved in the account file (as
Attrs) and restored on the next login. Staff can edit them in RC's player attributes editor; changes to an online player apply live.
An attribute can also attach a gani script to the player: player.attr.light = ['light.gan', 255, 0, 0] runs light.gan's script on every client that sees the player, with params = [255, 0, 0]. See Attaching a gani with an attribute.
A string attribute can also dress the player's animations: a gani's ATTR sprite draws the image its attribute holds, so player.attr.hat = 'hat1.png' shows the hat in every animation with an ATTR hat sprite.
Attributes vs clientr: clientr is private to the player's own client. Attributes are public to everyone who can see the player.
Example: healthbars
The server keeps health in player.attr.health:
ts
// Serverside: health lives in player.attr, so every client that can see
// the player gets it (and it's saved with the account).
export function onPlayerJoined(player: Player) {
player.attr.health ??= 100
}
// The clientside half asks for damage with triggerServer(..., 'hurt', n).
export function onActionServerSide(player: Player, action: string, amount: number) {
if (action !== 'hurt') return
const hp = Math.max(0, (player.attr.health ?? 100) - Math.abs(Number(amount) || 0))
player.attr.health = hp === 0 ? 100 : hp // "respawn" at full health
}Each client draws a bar over every visible player that has the attribute. Typing hurt in chat costs you 10 HP, and everyone near you sees your bar shrink:
ts
// Clientside: a bar above every visible player that has attr.health.
const BAR_W = 32 // px: the 2x2-tile body is 32px wide
const bars = new Set<number>()
export function onUpdate() {
const seen = new Set<number>()
for (const p of players) {
const hp = p.attr.health
if (typeof hp !== 'number') continue
seen.add(p.id)
const back = findimg(1000 + p.id * 2)
back.x = p.x
back.y = p.y - 0.5
back.width = BAR_W
back.height = 4
back.tint = '0,0,0'
const fill = findimg(1001 + p.id * 2)
fill.x = p.x
fill.y = p.y - 0.5
fill.width = Math.max(1, Math.round(BAR_W * Math.min(1, hp / 100)))
fill.height = 4
fill.tint = hp > 30 ? '60,200,60' : '220,50,50'
fill.layer = 5
}
// Players who left view (or lost the attribute) lose their bar.
for (const id of bars)
if (!seen.has(id)) { hideimg(1000 + id * 2); hideimg(1001 + id * 2) }
bars.clear()
for (const id of seen) bars.add(id)
}
// Typing "hurt" in chat damages yourself, to try it out.
export function onPlayerChats(who: ChatPlayer, chat: string) {
if (who.id === player.id && chat === 'hurt')
triggerServer('weapon', this.name, 'hurt', 10)
}