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
@@ -0,0 +1,160 @@
|
||||
/**
|
||||
* A persistent Variables asset keeps its values past the end of the run (#166): the next run starts
|
||||
* where the last one left off, from whatever area it is handed – played and editor runs get their own.
|
||||
*/
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { SetVariablesPersistentCommand } from '../../src/core/commands/assetCommands';
|
||||
import { AddVariableCommand, RemoveVariableCommand } from '../../src/core/commands/variableCommands';
|
||||
import { restoreSaved, type SaveArea, type SavedValues, VARIABLES_PARAM } from '../../src/core/graph';
|
||||
import type { VariablesAsset } from '../../src/core/model/Asset';
|
||||
import { deserializeProject, serializeProject } from '../../src/core/store/serialize';
|
||||
import {
|
||||
graphEditor,
|
||||
makeBehavior,
|
||||
makeSpriteScene,
|
||||
makeVariables,
|
||||
place,
|
||||
type SceneFixture,
|
||||
startWorld,
|
||||
} from '../helpers';
|
||||
|
||||
/** An area in memory that counts its writes. */
|
||||
function memoryArea(): SaveArea & { saved: SavedValues; writes: number } {
|
||||
const area = {
|
||||
saved: {} as SavedValues,
|
||||
writes: 0,
|
||||
read: () => structuredClone(area.saved),
|
||||
write: (saved: SavedValues) => {
|
||||
area.writes++;
|
||||
area.saved = structuredClone(saved);
|
||||
},
|
||||
};
|
||||
return area;
|
||||
}
|
||||
|
||||
/** One object whose behavior adds one to `score` of the asset on start. */
|
||||
function counting(persistent = true) {
|
||||
const f = makeSpriteScene();
|
||||
const shared = makeVariables(f, 'Progress', ['score']);
|
||||
if (persistent) f.stack.execute(new SetVariablesPersistentCommand(shared.id, true));
|
||||
const behavior = makeBehavior(f, 'Counter');
|
||||
const editor = graphEditor(f, behavior);
|
||||
const params = { [VARIABLES_PARAM]: shared.id };
|
||||
const start = editor.addNode('OnStart');
|
||||
const read = editor.variables(['score'], { params });
|
||||
const add = editor.addNode('Add', { b: 1 });
|
||||
const write = editor.variables(['score'], { write: true, params });
|
||||
editor.wire([
|
||||
[start, 'exec', write, 'exec'],
|
||||
[read, 'score', add, 'a'],
|
||||
[add, 'result', write, 'score'],
|
||||
]);
|
||||
place(f, behavior.id, { x: 0, y: 0 });
|
||||
return { f, shared };
|
||||
}
|
||||
|
||||
function run(f: SceneFixture, saves: SaveArea) {
|
||||
const started = startWorld(f, { behaviors: true, saves });
|
||||
started.frame();
|
||||
return started;
|
||||
}
|
||||
|
||||
const scoreOf = (started: ReturnType<typeof run>, id: string) =>
|
||||
started.behaviors!.shared.variables[id]!.score;
|
||||
|
||||
describe('a persistent Variables asset', () => {
|
||||
it('starts the next run where the last one ended', () => {
|
||||
const { f, shared } = counting();
|
||||
const area = memoryArea();
|
||||
expect(scoreOf(run(f, area), shared.id)).toBe(1);
|
||||
expect(scoreOf(run(f, area), shared.id)).toBe(2);
|
||||
expect(area.saved).toEqual({ [shared.id]: { score: 2 } });
|
||||
});
|
||||
|
||||
it('keeps played and editor runs apart', () => {
|
||||
const { f, shared } = counting();
|
||||
const played = memoryArea();
|
||||
const editor = memoryArea();
|
||||
run(f, played);
|
||||
run(f, played);
|
||||
expect(scoreOf(run(f, editor), shared.id)).toBe(1);
|
||||
expect(played.saved[shared.id]!.score).toBe(2);
|
||||
});
|
||||
|
||||
it('writes only in a frame that changed something', () => {
|
||||
const { f } = counting();
|
||||
const area = memoryArea();
|
||||
const started = run(f, area);
|
||||
const after = area.writes;
|
||||
started.frame();
|
||||
started.frame();
|
||||
expect(after).toBe(1);
|
||||
expect(area.writes).toBe(1);
|
||||
});
|
||||
|
||||
it('starts from the declared value again once the save is gone', () => {
|
||||
const { f, shared } = counting();
|
||||
const area = memoryArea();
|
||||
run(f, area);
|
||||
area.saved = {};
|
||||
expect(scoreOf(run(f, area), shared.id)).toBe(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('a Variables asset that is not persistent', () => {
|
||||
it('neither reads nor writes a save', () => {
|
||||
const { f, shared } = counting(false);
|
||||
const area = memoryArea();
|
||||
area.saved = { [shared.id]: { score: 40 } };
|
||||
expect(scoreOf(run(f, area), shared.id)).toBe(1);
|
||||
expect(area.writes).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('restoring a save', () => {
|
||||
function project() {
|
||||
const f = makeSpriteScene();
|
||||
const shared = makeVariables(f, 'Progress', ['score', 'lives']);
|
||||
f.stack.execute(new SetVariablesPersistentCommand(shared.id, true));
|
||||
return { f, shared };
|
||||
}
|
||||
|
||||
it('follows the declaration as it is now, not as it was saved', () => {
|
||||
const { f, shared } = project();
|
||||
f.stack.execute(new RemoveVariableCommand(shared.id, 'lives'));
|
||||
f.stack.execute(new AddVariableCommand(shared.id, 'name', 'text'));
|
||||
const variables = { [shared.id]: { score: 0, name: '' } };
|
||||
restoreSaved(f.project, variables, { [shared.id]: { score: '12', lives: 3, name: 7 } });
|
||||
// A key no longer declared stays out, and each value takes the kind declared today.
|
||||
expect(variables[shared.id]).toEqual({ score: 12, name: '7' });
|
||||
});
|
||||
|
||||
it('leaves a key the save does not name at its declared value', () => {
|
||||
const { f, shared } = project();
|
||||
const variables = { [shared.id]: { score: 0, lives: 3 } };
|
||||
restoreSaved(f.project, variables, { [shared.id]: { score: 5 } });
|
||||
expect(variables[shared.id]).toEqual({ score: 5, lives: 3 });
|
||||
});
|
||||
});
|
||||
|
||||
describe('the persistent switch', () => {
|
||||
it('is one undo step', () => {
|
||||
const f = makeSpriteScene();
|
||||
const shared = makeVariables(f, 'Progress', ['score']);
|
||||
f.stack.execute(new SetVariablesPersistentCommand(shared.id, true));
|
||||
expect(shared.persistent).toBe(true);
|
||||
f.stack.undo();
|
||||
expect(shared.persistent).toBeUndefined();
|
||||
});
|
||||
|
||||
it('survives saving and loading', () => {
|
||||
const f = makeSpriteScene();
|
||||
const shared = makeVariables(f, 'Progress', ['score']);
|
||||
f.stack.execute(new SetVariablesPersistentCommand(shared.id, true));
|
||||
const loaded = deserializeProject(JSON.parse(JSON.stringify(serializeProject(f.project))));
|
||||
const asset = loaded.assets.find(
|
||||
(a): a is VariablesAsset => a.id === shared.id && a.type === 'variables',
|
||||
);
|
||||
expect(asset?.persistent).toBe(true);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user