A shared value belongs to no behavior, so it becomes an asset of its own (#91)
game-maker / suite (push) Failing after 8m59s
game-maker / suite (push) Failing after 8m59s
This reverses the design of the previous commit. `PropertyDef.scope` is gone without replacement: a behavior variable is an object property again, without exception, and sharing is something you point at rather than something you declare twice. A **Variables asset** is declarations and nothing else -- no graph, no position, never placed, no tab of its own (`TAB_OF_ASSET` is already `Partial<>`, so that costs no line). A `Variables` node names one through its new `variablesAsset` setting, empty meaning the behavior's own variables as before, and then reads and writes one value per key for the whole run. Its property list follows the chosen source, which was the actual request: a value another graph declared is now offerable, where before every participant had to declare the name itself. That deletes more than it adds (-72 lines net): `globalConflicts` and its run-stopping kind clash, `SetVariableScopeCommand`, the scope picker, both object-inspector filters and the "shared by the whole run" note on the placed object. A typo is no longer a silent second variable -- naming an asset is picking from a list. And two Variables assets may carry the same key without meeting, because the asset is the key, not the name. The one shape it copies is the one that already existed: `Set state` picks its machine the same way. Lifetime is unchanged. The bag is built at `BehaviorSystem.start()` from the declarations and never written back, so playing in the editor cannot change the project -- which is the whole reason a counter must not be an object property (`World.syncToModel` would `commit()` it, and the coin counter would stand at 7 for good). Full symmetry across that gap, without mirroring anything. Two named wires: `Inspector.liveVariables` shows the values as they stand while a run is on and falls back to the declarations when it ends, and `adoptDeclarations()` takes a key into the run *only where the document changed it since the last sync*. Without that condition any unrelated project edit would snap a running counter back to its starting value; a counter-test bends exactly that and falls over. Nothing was ever cached, incidentally: `GraphInterpreter` only pulse-caches nodes without a `read`, and the variable node has one. The wire was missing. The state machine `Scope` row is now an ordinary `enum` row through `createFieldRow`. It was hand-built from an `HStack` with a ghost dropdown and began ~34px left of every value above it; the explanations beside `Local` and `Global` are gone from the menu, the sentence below the row keeps saying it. Machine scope is otherwise untouched -- `StateMachineAsset.scope`, `SetMachineScopeCommand`, `stateBag()` and its persistence all stand, so #59 stays closed as built and `Scope` as a type now carries machines alone. Six counter-proofs, each biting at its own guard. One of them did not bite at first: the suggestion list was computed twice, in `nodeSection` and `portSection`, so only the browser test covered the request itself. It is one named function now, `variableChoices`, with a unit test of its own. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
d1fcfe8b81
commit
8143130ed2
37 files changed
+679
-419
No files matched your search
@@ -7,7 +7,6 @@ import {
|
||||
} from '../model/Properties';
|
||||
import { overrideSites } from '../model/instance';
|
||||
import type { Project } from '../model/Project';
|
||||
import { type Scope, scopeOf, setScope } from '../model/Scope';
|
||||
import type { VariableKind } from '../properties/kinds';
|
||||
import {
|
||||
emptyFieldValue,
|
||||
@@ -20,7 +19,7 @@ import {
|
||||
import { type Removed, removeFirst, restoreAt } from '../util/list';
|
||||
import { type Command, CommandError } from './Command';
|
||||
import { renameVariablePorts } from './portCommands';
|
||||
import { requireBehavior } from './require';
|
||||
import { requireVariableOwner } from './require';
|
||||
|
||||
/** The variable's default value is written to `props` as well. */
|
||||
export class AddVariableCommand implements Command {
|
||||
@@ -37,7 +36,7 @@ export class AddVariableCommand implements Command {
|
||||
}
|
||||
|
||||
execute(project: Project): void {
|
||||
const asset = requireBehavior(project, this.assetId);
|
||||
const asset = requireVariableOwner(project, this.assetId);
|
||||
if (!isValidVariableName(this.def.key)) throw new CommandError(INVALID_NAME);
|
||||
if (asset.variables.some((v) => v.key === this.def.key))
|
||||
throw new CommandError(`Variable "${this.def.key}" already exists`);
|
||||
@@ -46,7 +45,7 @@ export class AddVariableCommand implements Command {
|
||||
}
|
||||
|
||||
undo(project: Project): void {
|
||||
const asset = requireBehavior(project, this.assetId);
|
||||
const asset = requireVariableOwner(project, this.assetId);
|
||||
asset.variables = asset.variables.filter((v) => v.key !== this.def.key);
|
||||
delete asset.props[this.def.key];
|
||||
}
|
||||
@@ -100,7 +99,7 @@ export class RemoveVariableCommand implements Command {
|
||||
}
|
||||
|
||||
execute(project: Project): void {
|
||||
const asset = requireBehavior(project, this.assetId);
|
||||
const asset = requireVariableOwner(project, this.assetId);
|
||||
const def = removeFirst(asset.variables, (v) => v.key === this.key);
|
||||
if (!def) throw new CommandError(`Variable "${this.key}" not found`);
|
||||
this.removed = { def, value: asset.props[this.key] };
|
||||
@@ -110,46 +109,13 @@ export class RemoveVariableCommand implements Command {
|
||||
|
||||
undo(project: Project): void {
|
||||
if (!this.removed) return;
|
||||
const asset = requireBehavior(project, this.assetId);
|
||||
const asset = requireVariableOwner(project, this.assetId);
|
||||
restoreAt(asset.variables, [this.removed.def]);
|
||||
if (this.removed.value !== undefined) asset.props[this.key] = this.removed.value;
|
||||
restoreOverrides(project, this.assetId, this.key, this.overrides);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A shared variable has no per-instance value, so going `global` takes the deviations placed objects
|
||||
* kept for it. Undo gives them back.
|
||||
*/
|
||||
export class SetVariableScopeCommand implements Command {
|
||||
readonly label: string;
|
||||
private previous: Scope = 'local';
|
||||
private overrides: TakenOverride[] = [];
|
||||
|
||||
constructor(
|
||||
private readonly assetId: string,
|
||||
private readonly key: string,
|
||||
private readonly scope: Scope,
|
||||
) {
|
||||
this.label = `Make variable ${key} ${scope}`;
|
||||
}
|
||||
|
||||
execute(project: Project): void {
|
||||
const def = variableOf(project, this.assetId, this.key);
|
||||
if (!def) throw new CommandError(`Variable "${this.key}" not found`);
|
||||
this.previous = scopeOf(def);
|
||||
setScope(def, this.scope);
|
||||
this.overrides = this.scope === 'global' ? takeOverrides(project, this.assetId, this.key) : [];
|
||||
}
|
||||
|
||||
undo(project: Project): void {
|
||||
const def = variableOf(project, this.assetId, this.key);
|
||||
if (!def) return;
|
||||
setScope(def, this.previous);
|
||||
restoreOverrides(project, this.assetId, this.key, this.overrides);
|
||||
}
|
||||
}
|
||||
|
||||
/** Renames everywhere: definition, default, instance overrides and the graph nodes referring to it. */
|
||||
export class RenameVariableCommand implements Command {
|
||||
readonly label: string;
|
||||
@@ -171,7 +137,7 @@ export class RenameVariableCommand implements Command {
|
||||
}
|
||||
|
||||
private rename(project: Project, from: string, to: string): void {
|
||||
const asset = requireBehavior(project, this.assetId);
|
||||
const asset = requireVariableOwner(project, this.assetId);
|
||||
if (from === to) return;
|
||||
if (!isValidVariableName(to)) throw new CommandError(INVALID_NAME);
|
||||
const def = asset.variables.find((v) => v.key === from);
|
||||
@@ -202,7 +168,7 @@ interface FieldOwner {
|
||||
}
|
||||
|
||||
function variableOf(project: Project, assetId: string, key: string): PropertyDef | undefined {
|
||||
return requireBehavior(project, assetId).variables.find((v) => v.key === key);
|
||||
return requireVariableOwner(project, assetId).variables.find((v) => v.key === key);
|
||||
}
|
||||
|
||||
function requireFieldOwner(project: Project, assetId: string, key: string): FieldOwner {
|
||||
@@ -244,7 +210,7 @@ interface ValueHolder {
|
||||
}
|
||||
|
||||
function valueHolders(project: Project, assetId: string, key: string): ValueHolder[] {
|
||||
const asset = requireBehavior(project, assetId);
|
||||
const asset = requireVariableOwner(project, assetId);
|
||||
const def = asset.variables.find((v) => v.key === key);
|
||||
const holders: ValueHolder[] = [];
|
||||
if (def)
|
||||
|
||||
Reference in new issue
Block a user