Skip to content

Live

State that survives code changes in a Build live session: hand state to the next version of a script, and read what the last one handed over.

  • Get it: import { Live } from '@rally/sdk'

In a Build live session a script restarts whenever its code changes, while everyone keeps playing. The world stays as it is: parts, values and attributes don’t change. Only what the script kept in its own variables is lost, unless it hands it over with onSwap. Keep game state in the world where you can (a NumberValue in leaderstats, an attribute), and use Live for the rest. Outside build sessions building is false, onSwap handlers never run and carried is undefined, so a game can use Live everywhere.

Type: boolean · Access: read-only

True while the game runs in a Build live session.

Type: unknown · Access: read-only

What this script’s previous version handed over through onSwap, or undefined.

onSwap(fn: () => unknown): void

Runs fn just before this script’s code is replaced; what it returns becomes carried in the next version. Return plain JSON (numbers, strings, booleans, arrays, objects; at most 64 KB), not instances or promises. It may run more than once, so only read state there. Objects returned by several handlers are merged.

A round counter that survives code changes

Section titled “A round counter that survives code changes”
import { Live, print, task } from '@rally/sdk';
const before = Live.carried as { round: number } | undefined;
let round = before?.round ?? 1;
Live.onSwap(() => ({ round }));
task.spawn(async () => {
while (true) {
print(`Round ${round}`);
await task.wait(60);
round++;
}
});