Files
adminandClaude Opus 5.5 1aa824ecb4
game-maker / gate (push) Successful in 2m37s
game-maker / suite (push) Successful in 13m2s
Keep objects through a Load scene with a switch (#165)
A Load scene destroyed every object of the old scene. A player, a HUD
or a music object had to be built again in each scene, with its state
pushed through shared variables. A top-level object with the new switch
Keep on scene load now travels into the next scene as the same entity,
with everything below it.

- The switch is a common property right under Enabled, off by default,
  on the asset and on a placed object. The inspector shows the row on
  top-level objects of a scene only, as answered in the issue.
- runtime/keptObjects.ts holds the rules as pure functions.
  keptEntities picks what travels, objectsToBring copies the stored
  objects as the run parents them, and bringObjects puts them into the
  next scene.
- World.instantiate takes a handed entity as it is instead of making
  one. The next syncToModel then writes the run's values into the
  scene.
- BehaviorSystem.release takes the running instances out before the
  old world is destroyed, and the next system adopts them. They hear
  no On destroy and no second On start, and their sounds play on.
- PhysicsSystem.resume gives a dynamic body the velocity and spin it
  had.
- Duplicates: the object brought along wins. The next scene does not
  build its own object with the same id, which is the way back into
  the scene the object came from.
- Decided here: the next scene does not build a top-level object of
  the same asset that is kept itself either. The issue only names the
  id. With the id alone a level that places its own player would hold
  two after a swap.
- Decided here: kept objects land behind the scene's own, the switch
  is no animation row, and collisions are found anew.

A finding, fixed along with it: Load scene did not build a scene as it
is stored, but as the run had left it. A moved object stayed moved and
a destroyed one stayed gone. The way back into a scene needs a clean
build.

- GameRun copies a scene's objects and groups before it first builds
  it and puts them back before it builds it again.
- GameRun.end puts every such scene back. The player calls it on stop,
  so spawned objects no longer stay for "Click to play again".
- The consequence: an edit made in a scene during Play is gone the
  next time that scene is loaded.

Tests:
- Gate: 199 files, 2944 tests (2929 + 15), exit 0
- e2e, full run: 1625 checks passed (1619 + 6), exit 0
- Smokes: 24 / 19 / 21, exit 0
- 15 unit tests in tests/core/keep-on-scene-load.test.ts
- Six e2e checks in module 35: the switch is set with a real mouse,
  the child has no row, the object is in the new scene, its counter
  still reads 1, its sound plays on, and the stored scenes are
  unchanged after Stop
- Eleven tests with fixed row lists only count the new schema row
- 16 unit counter-probes all fail tests, run on stein2. One did not
  fail at first (same id not left out), it has its own test now.
- The browser counter-probe fails 4 of the 6 new checks
- Not looked at in the browser: the switch as a port of the Properties
  node, and a released game with a kept object

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-01 17:30:16 +02:00

130 lines
5.1 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { describe, expect, it } from 'vitest';
import { AddColliderCommand } from '../../src/core/commands/colliderCommands';
import { SetOverrideCommand } from '../../src/core/commands/sceneObjectCommands';
import { AddVariableCommand } from '../../src/core/commands/variableCommands';
import { isHandleEnabled } from '../../src/core/graph';
import { findAsset, findScene } from '../../src/core/model/Project';
import { schemaFor } from '../../src/core/properties/PropertySchema';
import { resolveSceneObjectProps } from '../../src/core/properties/resolve';
import {
fakeHandle,
graphEditor,
makeBehavior,
makeProject,
makeSpriteScene,
place,
roundTrip,
runNode,
startWorld,
} from '../helpers';
/** A 100×100 wall at x = 200 and a 100×100 mover at the origin, both with box colliders, in a running world. */
function wallAndMover() {
const f = makeSpriteScene();
const moverId = place(f, f.spriteId, { x: 0, y: 0 });
const wallId = place(f, f.spriteId, { x: 200, y: 0 });
const wallCollider = new AddColliderCommand(f.sceneId, wallId);
f.stack.execute(new AddColliderCommand(f.sceneId, moverId));
f.stack.execute(wallCollider);
const { world, collisions } = startWorld(f, { collisions: true });
return {
world,
collisions,
mover: world.findByObjectId(moverId)!,
wall: world.findByObjectId(wallCollider.objectId)!,
};
}
describe('the enabled switch', () => {
it('is a common property of every placeable type, defaults to on and overrides per object', () => {
expect(schemaFor('sprite')[0]).toMatchObject({ key: 'enabled', kind: 'boolean', default: true });
expect(schemaFor('behavior').map((d) => d.key)).toEqual([
'enabled',
'keepOnSceneLoad',
'runInEditor',
'runWhilePaused',
]);
expect(schemaFor('scene').some((d) => d.key === 'enabled')).toBe(false);
const f = makeSpriteScene();
const { project, stack, sceneId, spriteId, object } = f;
const box = place(f, spriteId, { x: 0, y: 0 });
expect(resolveSceneObjectProps(project, findAsset(project, spriteId)!, object(box)).enabled).toBe(true);
// Switching an instance off is an ordinary override – and survives a round trip.
stack.execute(new SetOverrideCommand(sceneId, box, 'enabled', false));
expect(object(box).overrides).toEqual({ enabled: false });
expect(findScene(roundTrip(project), sceneId)!.objects[0]!.overrides).toEqual({ enabled: false });
stack.undo();
expect(object(box).overrides).toEqual({});
});
it('is inherited: a child of a switched-off parent is off as well', () => {
const parent = fakeHandle({ enabled: false });
const child = fakeHandle({ enabled: true }, parent);
expect(isHandleEnabled(child)).toBe(false);
expect(child.props.enabled).toBe(true); // its own switch is untouched
parent.props.enabled = true;
expect(isHandleEnabled(child)).toBe(true);
expect(runNode('IsEnabled', { object: child })).toEqual({ result: true, own: true });
});
it('cannot be shadowed by a behavior variable', () => {
const f = makeProject();
const behavior = makeBehavior(f);
expect(() => new AddVariableCommand(behavior.id, 'enabled', 'number').execute(f.project)).toThrow();
});
it('takes a collider out of the collision system while it is off', () => {
const { world, collisions, wall, mover } = wallAndMover();
// The wall blocks: moving 200 to the right stops at 100 units of distance (two 100-wide boxes).
collisions.moveAndSlide(world, mover, 200, 0);
expect(mover.x).toBeCloseTo(100, 5);
// Switched off, the wall is not there at all.
mover.x = 0;
wall.props.enabled = false;
collisions.moveAndSlide(world, mover, 200, 0);
expect(mover.x).toBeCloseTo(200, 5);
});
it('stops a behavior while it is off and runs On start when it is switched on', () => {
const f = makeSpriteScene();
const behavior = makeBehavior(f, 'Counter', ['ticks']);
// On start and On update both add one to `ticks`.
const edit = graphEditor(f, behavior);
const start = edit.addNode('OnStart');
const update = edit.addNode('OnUpdate');
const one = edit.addNode('Number', { value: 1 });
const add = edit.addNode('Add');
const get = edit.variables(['ticks']);
const set = edit.variables(['ticks'], { write: true });
edit.wire([
[update, 'exec', set, 'exec'],
[get, 'ticks', add, 'a'],
[one, 'value', add, 'b'],
[add, 'result', set, 'ticks'],
[start, 'exec', set, 'exec'],
]);
const counter = place(f, behavior.id, { x: 0, y: 0 });
f.stack.execute(new SetOverrideCommand(f.sceneId, counter, 'enabled', false));
const { world } = startWorld(f, { behaviors: true });
const entity = world.findByObjectId(counter)!;
world.tick(0.1);
world.tick(0.1);
expect(entity.props.ticks).toBe(0); // off: no On start, no ticks
entity.props.enabled = true; // what Properties with `enabled` set does
world.tick(0.1);
expect(entity.props.ticks).toBe(2); // On start plus this frame's On update
world.tick(0.1);
expect(entity.props.ticks).toBe(3);
entity.props.enabled = false;
world.tick(0.1);
expect(entity.props.ticks).toBe(3); // frozen again, state kept
});
});