Appearance
Gani scripts
clientsideA .gan animation can carry its own script. It runs on the client whenever a player plays that animation and stops when they switch to another one. Gani scripts are for effects tied to an animation: sparks on a sword spin, dust while running, or a glow around a player holding a lantern.
A server script can also attach a gani's script to a player or NPC through an attribute, so the script runs whatever animation is playing. See Attaching a gani with an attribute.
Write the script in GRC's GANI editor: open the animation and press Script… to switch from the preview to the script editor. Save the animation as usual. The script is stored inside the .gan file, so it travels with the animation and needs no weapon or NPC.
Porting Graal ganis
Classic Graal .gani files embed GScript in a SCRIPT … SCRIPTEND block. An imported gani keeps that text, but it won't run: rewrite it in JavaScript in the Script tab.
When it runs
- It starts when a player starts the animation. That includes the local player and every other player you can see. If the
.ganstill has to download, the script starts once it arrives (and only if the player is still playing that animation). - Each player has one animation script at a time. Changing animation stops the old script and starts the new animation's script, if it has one. The script also stops when the player leaves your view. Attached ganis run beside it and aren't affected by animation changes.
- Stopping cleans up. The images a gani script creates, along with their particle emitters and lights, are removed when its script stops. You don't need an exit handler.
- Every start is fresh. Each time the script starts, its top-level code runs again with new variables. Nothing carries over from a previous play of the animation or from another player playing it. The script's timers stop along with it.
onCreated() runs when the script starts, and onUpdate(dt) runs every frame like in a weapon. It gets the same broadcast events as weapons (for example onPlayerChats), and every clientside global is available: findimg, echo, timers and so on. It's plain JavaScript, so leave out type annotations. As in weapons, top-level functions are handlers whether or not you export them, and a script can import shared lib modules.
Compiled by the server
The server compiles a gani's script when it sends the .gan to players. Each script is compiled once and recompiled only when its text changes, so starting and stopping a gani is cheap however often players do it. A script that fails to compile is left out of the .gan players receive, and its errors appear in the server console. Top-level await isn't supported: await inside onCreated instead.
player is the animation's owner
Inside a gani script, player is the player playing the animation, not the local player. It's a read-only, live ClientPlayer view (the same kind of handle as the entries in players): id, name, nick, x, y, dir, chat, ani, head, body, colors, islocal and attr (the player's attributes). Reads always return the player's current values. Use player.islocal when something should only happen for your own character.
A gani script also sees a global params array. When the script runs because the animation is playing, it holds the setAni params: the values after the name in setAni('carry', 'pot.png'), or [] when there were none. For an attached gani it holds the attribute's parameters. Either way it's read-only and deep-frozen. If setAni replays the same animation with new params, params changes in place without restarting the script.
Keep gani scripts cosmetic
The script runs on every client that can see the animation, once per player playing it. If it changed game state (for example triggered the server), each of those clients would do it. Use gani scripts for visuals and sounds, and keep what they draw deterministic so every player sees the same thing. Gameplay belongs in a weapon.
Example: a spin-attack burst
A one-off spray of sparks when the animation starts. Particles fly on their own, so nothing has to follow the player:
js
export function onCreated() {
const img = findimg(1)
img.x = player.x + 1 // the player's body is 2×2 tiles; +1 is its center
img.y = player.y + 1
const e = img.emitter
e.emitautomatically = false
e.nrofparticles = 24
e.particle.lifetime = 0.5
e.particle.speed = 6
e.particle.red = 1; e.particle.green = 0.85; e.particle.blue = 0.45 // golden
e.addlocalmodifier('once', 0, 0, 'angle', 'replace', 0, Math.PI * 2) // random direction
e.addlocalmodifier('range', 0, 0.5, 'alpha', 'replace', 1, 0) // fade out
e.emit()
}ATTR and PARAM sprites
Besides image files and the player's head and body, a gani can draw sprites whose image is picked per player at draw time. Set a sprite's kind in the GANI editor's sprite list:
| Kind | Draws |
|---|---|
| ATTR + attribute name | The image held by the owner's attribute of that name, e.g. hat draws player.attr.hat. NPCs read this.attr. |
| PARAM + slot | The image at that slot (0-based) of the animation's setAni params. Slot 0 is the first value after the name. |
The sprite's rect (x, y, W, H) is cut from that image like any other sprite. When the attribute or param is missing, or isn't a string, the sprite draws nothing. Projectiles have no attributes or params, so their ATTR/PARAM sprites stay empty. GUI actors take their own attr and aniParams, or mirror a live character's attributes.
ts
// Serverside: every gani with an ATTR "hat" sprite now draws hat1.png
player.attr.hat = 'hat1.png'Attributes live on the player and suit equipment that shows in every animation (a hat drawn in idle, walk and sword). Params belong to one setAni call and suit things tied to a single action, like the item being carried or thrown.
In the editor, each ATTR attribute and PARAM slot the animation uses gets a Preview attr.name / Preview params[n] box in the right panel. Type an image name there to see the sprite in the preview. Previews only exist in the editor and aren't saved in the .gan.
Imported Graal ganis
Graal numbered its attributes (ATTR1) and params (PARAM1). The converter turns ATTR1 into the attribute named "1" and PARAM1 into slot 0.
setAni params
Any value after the animation name in setAni is a param. Params travel with the animation to everyone who sees the player, so PARAM sprites and the gani script see the same values on every client:
ts
// Clientside (weapon): the local player carries a pot
setAni('carry', 'pot.png')
// Serverside: same thing on any player, or on an NPC
player.setAni('carry', 'pot.png')
this.setAni('carry', 'barrel.png')- Up to 16 values of any JSON (2048 characters of JSON in total). Only strings draw as PARAM sprites; the script sees every value.
- Every
setAnireplaces them.setAni('idle')clears the params, and so does assigningplayer.anior the built-in idle/walk switching. ASETBACKTOchain keeps them. - Read them back with
player.aniParams(also on players handles and NPCs'this.aniParams).
Attaching a gani with an attribute
A player attribute whose value names a .gan attaches that gani's script to the player. Every client that can see the player runs it, the player's own client included. The player's animation and sprites don't change: only the script runs.
ts
// Serverside
player.attr.aura = 'aura.gan' // no parameters
player.attr.light = ['light.gan', 255, 0, 0] // params = [255, 0, 0]
delete player.attr.light // stops it everywhere- What counts as a gani. A string ending in
.gan(any case), or an array whose first element is one. Any other value is ordinary data, soattr.title = 'light'orattr.list = ['a', 'b']never start a script. paramsis the rest of the array:[255, 0, 0]above,[]for the plain-string form. Parameters can be any JSON (numbers, strings, objects). Like the attribute itself, they're read-only and deep-frozen.- Several at once. Each attribute runs its own script, next to the animation's script. Attribute scripts each get their own
findimgids, so two attached ganis never collide overfindimg(1). - Changing the value restarts the script. A different gan or different parameters stop the old script (removing its images and lights) and start it again, so
onCreatedruns with the newparams. Assigning the same value again does nothing. - It follows visibility. The script starts when the player comes into view (or the attribute is set) and stops when they leave your view or the attribute is removed or changed to a non-gani value.
- It persists. Attributes are saved with the account, so an attached gani is back after the player relogs. Remove the attribute when a temporary effect ends.
Otherwise it's an ordinary gani script: player is the player it's attached to, and the same runtime rules and cleanup apply.
On NPCs
NPC attributes work the same way:
ts
// In a serverside NPC script (or on a putnpc handle)
this.attr.light = ['light.gan', 255, 200, 120]Inside the script, player is the NPC: a read-only, live view with the same members as a player handle (x, y, dir, ani, chat, nick, head, body, colors, attr, plus image). On an NPC, player.isnpc is true, islocal is false and account is ''. The script stops when the NPC leaves your view, is destroyed, or its level reloads. Scripts written against player.x/player.y, like the light below, work on players and NPCs alike.
Example: a light that follows the player
Any image can be a light source (see Image lights). An image with no picture that calls drawaslight() draws nothing itself, so it's a light and nothing else. Attach it with a color, and move it with the player every frame. This is light.gan's script:
js
// params: [red, green, blue], e.g. player.attr.light = ['light.gan', 255, 0, 0]
export function onCreated() {
const [r = 255, g = 190, b = 110] = params // warm lantern by default
const light = findimg(1)
light.lightcolor = `${r},${g},${b}`
light.lightradius = 7
light.drawaslight()
follow()
}
export function onUpdate() {
follow()
findimg(1).lightintensity = 0.9 + Math.random() * 0.15 // gentle flicker
}
function follow() {
const light = findimg(1)
light.x = player.x + 1 // center of the 2×2-tile body
light.y = player.y + 1
}Turn it on from any server script, and recolor or remove it the same way:
ts
player.attr.light = ['light.gan', 255, 0, 0] // red
player.attr.light = ['light.gan', 80, 140, 255] // blue (restarts the script)
delete player.attr.lightLights only show where setambient has darkened the world, unless you set lightmode = 1.
Because the light is attached rather than tied to an animation, it stays on while the player idles, walks, or swings a sword. The same script also works as a regular animation script: there, params holds the setAni params, so setAni('light', 80, 140, 255) plays it blue, and plain setAni('light') gets the default color.
See also
- Images, text & particles: everything
findimgcan draw. - Lighting: ambient light, light shapes and modes.
- Players:
setAni,replaceAniand how animations play. - Player attributes: the values ATTR sprites draw.