Variables assets can keep their values from one run to the next (#166)
A game needs a high score or an unlocked level to survive the end of
a run. As agreed in the issue there are no save or load nodes. A
Variables asset gets a switch instead, and the existing Variables
nodes read and write the values as before.
- A "Persistent" checkbox in the Variables inspector. Toggling it is
one undo step, and absent means not persistent.
- The behavior system loads the saved values at start and writes them
after every frame that changed one, so a closed tab loses nothing.
- Values live in localStorage per project, not in the project file.
Another browser or person starts fresh.
- Play and "Run in editor" save apart, so trying a behavior in the
editor leaves the Play save alone.
- Saved values are read through the current declaration. A removed
variable drops out, a new one starts from its default, and a changed
kind is converted.
- "Clear saved values" in the inspector clears this asset in both
areas. The main menu gets "Clear saved data" for the whole project.
Clearing takes effect from the next run on.
Decisions:
- The switch covers the whole asset, not single variables. Mixing
both means two assets.
- A changed default in the inspector shows only after the save is
cleared. That is what persistent means.
Findings:
- The first e2e run died at "debug hook present" right after a Vulkan
error from Chrome. The second run passed, so it was a GPU flake at
start-up.
Tests:
- ./check.sh: 140 files, 2070 tests, exit 0 (before 138 / 2055).
- 15 new unit tests in tests/core/saved-values.test.ts and
tests/editor/saves.test.ts.
- GM_STEPS=32,46 fresh: ALL PASSED, 24 checks. Six new checks in
module 32 go through the UI. Module 46 knows the new menu entry.
- Counter-checks both fail as expected. Loading switched off: 2 unit
tests and 2 e2e checks fail. Editor run writing into the Play area:
1 e2e check fails.
- Check total 1278 (1272 measured by CI at bbbf341, plus 6).
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
bbbf34109b
commit
41772e53f7
17 files changed
+619
-19
No files matched your search
@@ -11,6 +11,9 @@ import {
|
||||
isHandleEnabled,
|
||||
type NodeContext,
|
||||
type PhysicsControl,
|
||||
restoreSaved,
|
||||
type SaveArea,
|
||||
savedSnapshot,
|
||||
type SharedState,
|
||||
startStates,
|
||||
type TraceListener,
|
||||
@@ -107,6 +110,8 @@ export interface GameSystemOptions {
|
||||
* run shares the clock with the previews, and a behavior there must not slow the editor down.
|
||||
*/
|
||||
clock?: TimeScaled | null;
|
||||
/** Where persistent Variables assets are read from at the start and written to as they change (#166). */
|
||||
saves?: SaveArea | null;
|
||||
}
|
||||
|
||||
export interface TimeScaled {
|
||||
@@ -118,11 +123,19 @@ export function addGameSystems(
|
||||
world: World,
|
||||
project: Project,
|
||||
input: Input,
|
||||
{ shouldRun, audio = SILENT_AUDIO, carried = null, physics = null, clock = null }: GameSystemOptions = {},
|
||||
{
|
||||
shouldRun,
|
||||
audio = SILENT_AUDIO,
|
||||
carried = null,
|
||||
physics = null,
|
||||
clock = null,
|
||||
saves = null,
|
||||
}: GameSystemOptions = {},
|
||||
): BehaviorSystem {
|
||||
const collisions = physics ? new PhysicsSystem(physics) : new CollisionSystem();
|
||||
const animations = new AnimationSystem();
|
||||
const behaviors = new BehaviorSystem(project, input, { collisions, animations, audio, clock }, shouldRun);
|
||||
const sources = { collisions, animations, audio, clock, saves };
|
||||
const behaviors = new BehaviorSystem(project, input, sources, shouldRun);
|
||||
if (carried) behaviors.carryOver(carried);
|
||||
world.addSystem(collisions);
|
||||
world.addSystem(animations);
|
||||
@@ -137,6 +150,7 @@ export interface EventSources {
|
||||
/** Where `Play sound` ends up. Absent is a run that plays silently, which is what a test wants. */
|
||||
audio?: AudioSink | null;
|
||||
clock?: TimeScaled | null;
|
||||
saves?: SaveArea | null;
|
||||
}
|
||||
|
||||
/** A behavior's `runWhilePaused` switch: whether it is ticked while Pause game holds the world. */
|
||||
@@ -165,6 +179,9 @@ export class BehaviorSystem implements System {
|
||||
private readonly animations: AnimationSystem | null;
|
||||
private readonly audio: AudioSink;
|
||||
private readonly clock: TimeScaled | null;
|
||||
private readonly saves: SaveArea | null;
|
||||
/** What was last written to `saves`, so a frame that changed nothing writes nothing. */
|
||||
private lastSaved = '';
|
||||
|
||||
constructor(
|
||||
private readonly project: Project,
|
||||
@@ -180,6 +197,7 @@ export class BehaviorSystem implements System {
|
||||
this.animations = sources.animations ?? null;
|
||||
this.audio = sources.audio ?? SILENT_AUDIO;
|
||||
this.clock = sources.clock ?? null;
|
||||
this.saves = sources.saves ?? null;
|
||||
}
|
||||
|
||||
/** The values `global` declarations share this run – read-only to callers, rebuilt by `start`. */
|
||||
@@ -219,6 +237,9 @@ export class BehaviorSystem implements System {
|
||||
this.carried = null;
|
||||
// Built fresh for every run: a shared variable starts at its declared value each time Play does.
|
||||
this.sharedState = carried ?? buildSharedState(this.project);
|
||||
// A persistent asset picks up where the last run left it; after a scene swap it already has.
|
||||
if (!carried && this.saves) restoreSaved(this.project, this.sharedState.variables, this.saves.read());
|
||||
this.lastSaved = JSON.stringify(savedSnapshot(this.project, this.sharedState.variables));
|
||||
this.declared = declaredVariables(this.project);
|
||||
// A `global` machine is entered once for the whole run, before the first instance exists – so
|
||||
// the move is queued here and every instance the first sync creates hears that one entry.
|
||||
@@ -236,11 +257,26 @@ export class BehaviorSystem implements System {
|
||||
this.finish(instance);
|
||||
this.audio.stopOwner(instance.entity.id);
|
||||
}
|
||||
// After the goodbyes: what `On destroy` writes is the last thing the run did.
|
||||
this.persist();
|
||||
this.instances = [];
|
||||
this.pending = [];
|
||||
this.world = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes the persistent values when they differ from the last write. Every frame rather than at
|
||||
* the end, because a tab closed mid-game has no end to wait for.
|
||||
*/
|
||||
private persist(): void {
|
||||
if (!this.saves) return;
|
||||
const snapshot = savedSnapshot(this.project, this.sharedState.variables);
|
||||
const text = JSON.stringify(snapshot);
|
||||
if (text === this.lastSaved) return;
|
||||
this.lastSaved = text;
|
||||
this.saves.write(snapshot);
|
||||
}
|
||||
|
||||
/**
|
||||
* One instance per behavior entity. A switched-off behavior gets its instance and `On start` only when
|
||||
* switched on, which is what lets a hidden screen wake up when shown.
|
||||
@@ -437,6 +473,7 @@ export class BehaviorSystem implements System {
|
||||
}
|
||||
this.reap(world);
|
||||
this.flushStateChanges();
|
||||
this.persist();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user