Variables assets can keep their values from one run to the next (#166)
game-maker / suite (push) Successful in 8m21s
game-maker / gate (push) Failing after 3h5m52s

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:
adminandClaude Opus 5 committed 2026-09-22 10:23:57 +02:00
1 parent bbbf34109b
commit 41772e53f7
17 files changed
+619 -19

No files matched your search

+43 -2
View File
@@ -1,13 +1,20 @@
import { createEmptyGraph } from '../graph/Graph';
import { newId } from '../ids';
import type { Asset, FontAsset, SoundAsset, StateMachineAsset, TextureAsset } from '../model/Asset';
import type {
Asset,
FontAsset,
SoundAsset,
StateMachineAsset,
TextureAsset,
VariablesAsset,
} from '../model/Asset';
import type { AssetType } from '../model/AssetTypes';
import { isFontFile } from '../model/font';
import { DEFAULT_PALETTE, PIXEL_CANVAS_DEFAULT } from '../model/pixelart';
import { isSoundFile } from '../model/sound';
import { removeSubtrees } from '../model/hierarchy';
import type { PropertyBag, PropertyValue } from '../model/Properties';
import { objectHolders, type Project } from '../model/Project';
import { findAssetOf, objectHolders, type Project } from '../model/Project';
import { type Scope, scopeOf, setScope } from '../model/Scope';
import type { SceneObject } from '../model/SceneObject';
import { isTextureFile } from '../model/texture';
@@ -270,6 +277,40 @@ export class SetMachineScopeCommand implements Command {
}
}
/** Whether a Variables asset keeps its values past the end of the run (#166). */
export class SetVariablesPersistentCommand implements Command {
readonly label: string;
private previous = false;
constructor(
private readonly assetId: string,
private readonly persistent: boolean,
) {
this.label = persistent ? 'Make variables persistent' : 'Make variables per run';
}
execute(project: Project): void {
const asset = this.target(project);
this.previous = asset.persistent === true;
setPersistent(asset, this.persistent);
}
undo(project: Project): void {
setPersistent(this.target(project), this.previous);
}
private target(project: Project): VariablesAsset {
const asset = findAssetOf(project, this.assetId, 'variables');
if (!asset) throw new CommandError(`Variables ${this.assetId} not found`);
return asset;
}
}
function setPersistent(asset: VariablesAsset, on: boolean): void {
if (on) asset.persistent = true;
else delete asset.persistent;
}
/**
* Which state a machine starts in, as a state node's id – `''` takes the mark off again, and the
* machine is back to starting in no state. Only one state can be the start because there is only
+1
View File
@@ -8,6 +8,7 @@ export * from './nested';
export * from './NodeContext';
export * from './NodeRegistry';
export * from './propertyPorts';
export * from './savedValues';
export * from './sharedState';
export * from './stateMachine';
export * from './values';
+55
View File
@@ -0,0 +1,55 @@
import type { VariablesAsset } from '../model/Asset';
import type { PropertyBag, PropertyValue } from '../model/Properties';
import type { Project } from '../model/Project';
import { coerceValue } from '../properties/PropertySchema';
/** What a save area holds: the values of each persistent Variables asset, keyed by asset id. */
export type SavedValues = Record<string, PropertyBag>;
/**
* Where the values of persistent Variables assets outlive the run (#166). Played and editor runs
* are handed different areas, so trying a behavior in the editor leaves the player's save alone.
*/
export interface SaveArea {
read(): SavedValues;
write(saved: SavedValues): void;
}
export function isPersistent(asset: VariablesAsset): boolean {
return asset.persistent === true;
}
function persistentAssets(project: Project): VariablesAsset[] {
return project.assets.filter((a): a is VariablesAsset => a.type === 'variables' && isPersistent(a));
}
/**
* Puts what was saved over the declared start values. The declaration is the truth: a key it no
* longer has is ignored, and a value is coerced to the kind it declares now.
*/
export function restoreSaved(
project: Project,
variables: Record<string, PropertyBag>,
saved: SavedValues,
): void {
for (const asset of persistentAssets(project)) {
const stored = saved[asset.id];
const bag = variables[asset.id];
if (!stored || !bag) continue;
for (const def of asset.variables)
if (def.key in stored) bag[def.key] = coerceValue(def, stored[def.key] as PropertyValue);
}
}
/** The declared values of every persistent asset as they stand in the run – what a save writes. */
export function savedSnapshot(project: Project, variables: Record<string, PropertyBag>): SavedValues {
const saved: SavedValues = {};
for (const asset of persistentAssets(project)) {
const bag = variables[asset.id];
if (!bag) continue;
const values: PropertyBag = {};
for (const def of asset.variables) if (def.key in bag) values[def.key] = bag[def.key]!;
saved[asset.id] = values;
}
return saved;
}
+4
View File
@@ -40,10 +40,14 @@ export interface BehaviorAsset extends AssetBase {
* Declarations shared by the whole run: a `Variables` node naming this asset reads and writes one
* value per key, however many graphs point at it. Nothing is placed, so nothing overrides it –
* `props` holds the starting values, as a behavior's does for its own variables.
*
* A `persistent` asset keeps its values past the end of the run (#166): the next run starts from
* what the last one left, not from `props`. Absent is not persistent.
*/
export interface VariablesAsset extends AssetBase {
type: 'variables';
variables: PropertyDef[];
persistent?: boolean;
}
/** Placed as a child of the object it belongs to; its position is the offset from that body. */
+4 -1
View File
@@ -29,6 +29,7 @@ import type {
TextureFrame,
GradientStop,
TilemapLayer,
VariablesAsset,
VectorGradient,
VectorPaint,
VectorPoint,
@@ -169,7 +170,9 @@ export function deserializeProject(raw: unknown, fallbackId = ''): Project {
graph: normalizeGraph(a.graph),
});
} else if (type === 'variables') {
assets.push({ id, type, name, props, variables: variables.get(id) ?? [] });
const shared: VariablesAsset = { id, type, name, props, variables: variables.get(id) ?? [] };
if (a.persistent === true) shared.persistent = true;
assets.push(shared);
} else if (type === 'statemachine') {
const machine: StateMachineAsset = {
id,