Let structs and lists hold objects across behavior runs (#238)
An object could only live in a dedicated object variable, kept in slots
beside the variable bag. Struct fields and list items could not hold
one, so a spawned object packed into a struct or list was lost. This
replaces the slots with one mechanism for all three places.
- "No object" has one resting form everywhere: {"$object": ""}. A
running game can tell an object slot from an empty text that way.
- During play the object handle lives directly in the value, also
nested in structs and lists. objectSlots.ts and the special paths for
Keep and Properties are gone.
- liveObjects.ts strips objects to (null) at every exit: overrides the
editor run writes back to the scene, saved values of persistent
Variables assets, live values in the inspector, and the world
snapshot of the test hooks (objects as names there).
- A destroyed object reads as (null) through isAlive, alone, in a
struct field or as a list item.
- "+ Field" and "+ Item" offer Object. The inspector and the list tab
show a fixed (null) row without input.
- Lists from Raycast all stay objects when written to a variable.
Before they turned into names.
- A struct that only changes by an object writes no override. A list
that grows by an object writes [(null)], its length really changed.
Found on the way: CI at 98211a8 failed only in e2e 21d. #237 added
asset to the field kinds without updating that fixed expectation. 21d
now expects ...,animation,asset,object,struct.
No project on the server has object variables, so no old data is
affected.
Tests:
- ./check.sh: 169 files, 2475 tests, CHECK EXIT 0.
- e2e 6, 21, 23, 32: ALL PASSED, 213 checks.
- object-variable.test.ts rewritten: variable, struct field and list
item over several frames, destroyed objects, nothing in scene, file
or save, editor run reload, Keep.
- New e2e 21d-ii adds an Object item by click. 21d and 23k updated.
- 7 counter-probes, each fails 1 to 9 tests. Originals back via cmp.
- Not tested on its own: stripping of inspector live values and the
names in the test snapshot, the "+ Field -> Object" menu by click.
- e2e check total 1407, only calculated.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
98211a8d54
commit
7979b8ff19
24 files changed
+510
-193
No files matched your search
@@ -1,7 +1,7 @@
|
||||
import { edgesInto, edgesOutOf, type Graph, type GraphNode } from './Graph';
|
||||
import { type GraphLookup, NO_LOOKUP } from './lookup';
|
||||
import { BEHAVIOR_END, callEntries, resolveDef, terminalNode } from './nested';
|
||||
import type { PropertyBag } from '../model/Properties';
|
||||
import type { PropertyBag, PropertyValue } from '../model/Properties';
|
||||
import type { NodeContext } from './NodeContext';
|
||||
import {
|
||||
type CallResult,
|
||||
@@ -12,9 +12,8 @@ import {
|
||||
type PortDef,
|
||||
type RepeatResult,
|
||||
} from './NodeRegistry';
|
||||
import { carryObjectSlots } from './objectSlots';
|
||||
import { machineIdOf } from './stateMachine';
|
||||
import type { Value } from './values';
|
||||
import { isObjectPlace, type Value } from './values';
|
||||
|
||||
/** How much work one pulse may do. A Repeat over a 10×20 board is 200 rounds, so this is generous. */
|
||||
const MAX_STEPS_PER_FIRE = 100_000;
|
||||
@@ -57,12 +56,16 @@ export function resumedVariables(
|
||||
const variables: PropertyBag = {};
|
||||
for (const [key, value] of Object.entries(defaults)) {
|
||||
const was = kept.includes(key) ? left?.[key] : undefined;
|
||||
variables[key] = was !== undefined && typeof was === typeof value ? was : value;
|
||||
variables[key] = was !== undefined && sameSort(was, value) ? was : value;
|
||||
}
|
||||
if (left) carryObjectSlots(left, variables, kept);
|
||||
return variables;
|
||||
}
|
||||
|
||||
/** Whether `was` may stand where `value` is declared – an object's place counts as a sort of its own. */
|
||||
function sameSort(was: PropertyValue, value: PropertyValue): boolean {
|
||||
return typeof was === typeof value && isObjectPlace(was) === isObjectPlace(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs one behavior graph for one instance. An event node starts a *pulse*; the interpreter follows
|
||||
* exec edges and pulls data inputs from the connected output, evaluating pure data nodes lazily and
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { isList, type PropertyBag, type PropertyValue } from '../model/Properties';
|
||||
import { isList, type ListValue, type PropertyBag, type PropertyValue } from '../model/Properties';
|
||||
import { isKindWith, plainKindOf } from '../properties/kinds';
|
||||
import { flatDef, type PropertyDef } from '../properties/PropertySchema';
|
||||
import type { DeclaredPort, Graph, GraphNode, PortType } from './Graph';
|
||||
import type { GraphLookup } from './lookup';
|
||||
import type { NodeContext } from './NodeContext';
|
||||
import type { SharedPortGroup } from './portTypes';
|
||||
import { asList, storedList, type Value } from './values';
|
||||
import { asList, namedValue, type Value } from './values';
|
||||
|
||||
export interface PortDef {
|
||||
key: string;
|
||||
@@ -192,9 +192,9 @@ export function withInlineDefault(port: PortDef): PortDef {
|
||||
export function inlineInputDef(port: PortDef): PropertyDef {
|
||||
if (port.field) return { ...port.field, key: port.key, label: port.label };
|
||||
const kind = port.type === 'any' ? 'text' : plainKindOf(port.type);
|
||||
// A declared default is a stored value, never a live handle (see `storedList`).
|
||||
// A declared default is a stored value, never a live handle.
|
||||
if (kind === 'list')
|
||||
return { key: port.key, label: port.label, kind, default: storedList(asList(port.default)) };
|
||||
return { key: port.key, label: port.label, kind, default: namedValue(asList(port.default)) as ListValue };
|
||||
const flat = kind !== undefined && isKindWith('field', kind) ? kind : 'number';
|
||||
const def = flatDef(port.key, flat, { label: port.label, value: port.default });
|
||||
return def.kind === 'number' ? { ...def, step: 1 } : def;
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
import {
|
||||
isList,
|
||||
isStruct,
|
||||
NO_OBJECT,
|
||||
type PropertyBag,
|
||||
type PropertyValue,
|
||||
tagOf,
|
||||
} from '../model/Properties';
|
||||
import type { EntityHandle } from './NodeContext';
|
||||
import type { PortDef } from './NodeRegistry';
|
||||
import { isEntity, isObjectPlace, type Value, writeTyped } from './values';
|
||||
|
||||
/**
|
||||
* During a run a bag may hold live objects – in an object variable, a struct field, a list item. None
|
||||
* of it may leave the run: `World.syncToModel` writes a behavior's changed values into the scene, a
|
||||
* save is written as JSON, the inspector and the test hooks show what they are handed. Each of those
|
||||
* ways out passes the value through here, where every object becomes the empty one (`NO_OBJECT`).
|
||||
*/
|
||||
export function withoutObjects<T extends PropertyValue | undefined>(value: T): T {
|
||||
return mapObjects(value, () => NO_OBJECT) as T;
|
||||
}
|
||||
|
||||
export function bagWithoutObjects(bag: PropertyBag): PropertyBag {
|
||||
const out: PropertyBag = {};
|
||||
for (const [key, value] of Object.entries(bag)) out[key] = withoutObjects(value);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* A bag's value as a graph reads it: an object destroyed since it was written is gone, so a graph
|
||||
* never goes on acting on one that has left the game. Alone it reads as nothing (`null`), inside a
|
||||
* list or struct as the empty object.
|
||||
*/
|
||||
export function liveValue(
|
||||
value: PropertyValue | undefined,
|
||||
isAlive: (target: EntityHandle) => boolean,
|
||||
): Value {
|
||||
if (value === undefined || tagOf(value) === 'object') return null;
|
||||
if (isEntity(value)) return isAlive(value) ? value : null;
|
||||
return mapObjects(value, (held) => (isAlive(held) ? held : NO_OBJECT)) as Value;
|
||||
}
|
||||
|
||||
/** What `outputs` read of a bag, each value live as `liveValue` says. */
|
||||
export function readLive(
|
||||
bag: PropertyBag,
|
||||
outputs: readonly PortDef[],
|
||||
isAlive: (target: EntityHandle) => boolean,
|
||||
): Record<string, Value> {
|
||||
const values: Record<string, Value> = {};
|
||||
for (const port of outputs) if (port.key in bag) values[port.key] = liveValue(bag[port.key], isAlive);
|
||||
return values;
|
||||
}
|
||||
|
||||
/** Writes what arrives on `port` into the bag, typed as the slot is; an object port always makes an object's place. */
|
||||
export function writePort(bag: PropertyBag, port: PortDef, value: Value | undefined): void {
|
||||
if (port.type === 'object' && !isObjectPlace(bag[port.key])) bag[port.key] = NO_OBJECT;
|
||||
writeTyped(bag, port.key, value);
|
||||
}
|
||||
|
||||
/** `value` with each object replaced by what `swap` says; untouched parts keep their identity. */
|
||||
function mapObjects(value: unknown, swap: (held: EntityHandle) => unknown): unknown {
|
||||
if (isEntity(value as Value)) return swap(value as EntityHandle);
|
||||
if (isList(value)) {
|
||||
const items = value.map((item) => mapObjects(item, swap));
|
||||
return items.some((item, i) => item !== value[i]) ? items : value;
|
||||
}
|
||||
if (isStruct(value) && tagOf(value) === undefined) {
|
||||
const entries = Object.entries(value);
|
||||
const mapped = entries.map(([key, field]) => [key, mapObjects(field, swap)] as const);
|
||||
return mapped.some(([, field], i) => field !== entries[i]![1]) ? Object.fromEntries(mapped) : value;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
@@ -110,10 +110,15 @@ export function isTerminalNode(node: GraphNode): boolean {
|
||||
return isTerminalType(node.type);
|
||||
}
|
||||
|
||||
/** A tagged kind is one the value cannot show, so its port carries the field that edits it. */
|
||||
/**
|
||||
* A tagged kind is one the value cannot show, so its port carries the field that edits it – except
|
||||
* an object, which nothing can type in.
|
||||
*/
|
||||
export function portDefOf(port: DeclaredPort): PortDef {
|
||||
const def: PortDef = { key: port.key, label: port.key, type: declaredPortType(port.kind) };
|
||||
return isTaggedKind(port.kind) ? { ...def, field: flatDef(port.key, port.kind) } : def;
|
||||
return isTaggedKind(port.kind) && port.kind !== 'object'
|
||||
? { ...def, field: flatDef(port.key, port.kind) }
|
||||
: def;
|
||||
}
|
||||
|
||||
/** The ports the start (or end) node of `graph` declares, as seen from a node calling it. */
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import type { NodeDef } from '../../NodeRegistry';
|
||||
import { asStruct } from '../../values';
|
||||
import { asStruct, unplaced, type Value } from '../../values';
|
||||
import { struct } from '../shared';
|
||||
|
||||
/**
|
||||
@@ -19,5 +19,9 @@ export const ExpandStruct: NodeDef = {
|
||||
params: [],
|
||||
declaresPorts: 'outputs',
|
||||
// A declared output the struct has no field for is simply absent from the cache, and reads as empty.
|
||||
run: (_ctx, inputs) => ({ outputs: { ...asStruct(inputs.value) } }),
|
||||
run: (_ctx, inputs) => {
|
||||
const outputs: Record<string, Value> = {};
|
||||
for (const [key, field] of Object.entries(asStruct(inputs.value))) outputs[key] = unplaced(field) ?? null;
|
||||
return { outputs };
|
||||
},
|
||||
};
|
||||
@@ -1,6 +1,6 @@
|
||||
import { untag } from '../../../model/Properties';
|
||||
import type { NodeDef } from '../../NodeRegistry';
|
||||
import { asList, asNumber, isEntity } from '../../values';
|
||||
import { asList, asNumber, isEntity, unplaced } from '../../values';
|
||||
import { any, int, list } from '../shared';
|
||||
|
||||
export const ItemAt: NodeDef = {
|
||||
@@ -18,6 +18,7 @@ export const ItemAt: NodeDef = {
|
||||
// An object comes out as the object it is, not as its name: that is the point of a list of
|
||||
// objects, as `Raycast all` hands one out (#157).
|
||||
if (isEntity(item)) return { outputs: { item } };
|
||||
if (unplaced(item) === null) return { outputs: { item: null } };
|
||||
return { outputs: { item: untag(item) ?? 0 } };
|
||||
},
|
||||
params: [],
|
||||
|
||||
@@ -6,7 +6,7 @@ import {
|
||||
propertyPort,
|
||||
propertyTargetOf,
|
||||
} from '../../propertyPorts';
|
||||
import { readWithObjects, writeWithObjects } from '../../objectSlots';
|
||||
import { readLive, writePort } from '../../liveObjects';
|
||||
import { asEntity } from '../../values';
|
||||
import { exec, obj } from '../shared';
|
||||
|
||||
@@ -28,16 +28,15 @@ export const Properties: NodeDef = {
|
||||
const shape = ports.some((p) => p.write) ? { inputs: [exec()], outputs: [exec()] } : {};
|
||||
return node.collapsed ? { ...shape, label: collapsedTitle(ports, 'properties') } : shape;
|
||||
},
|
||||
// A behavior's variables are its props, so an object variable is read from its slot there too.
|
||||
read: (ctx, _params, input, def) => {
|
||||
const target = asEntity(input('object'));
|
||||
return target ? readWithObjects(target.props, def.outputs, ctx.isAlive) : {};
|
||||
return target ? readLive(target.props, def.outputs, ctx.isAlive) : {};
|
||||
},
|
||||
run: (_ctx, inputs, _params, def) => {
|
||||
const target = asEntity(inputs.object);
|
||||
if (target)
|
||||
for (const port of def.inputs)
|
||||
if (port.type !== 'exec' && port.key !== 'object') writeWithObjects(target.props, port, inputs[port.key]);
|
||||
if (port.type !== 'exec' && port.key !== 'object') writePort(target.props, port, inputs[port.key]);
|
||||
return { next: 'exec' };
|
||||
},
|
||||
};
|
||||
@@ -1,6 +1,6 @@
|
||||
import { collapsedTitle, portDefOf, VARIABLES, VARIABLES_SETTING, variablesSourceOf } from '../../nested';
|
||||
import { type NodeDef, withInlineDefault } from '../../NodeRegistry';
|
||||
import { readWithObjects, writeWithObjects } from '../../objectSlots';
|
||||
import { readLive, writePort } from '../../liveObjects';
|
||||
import { variableBag } from '../../sharedState';
|
||||
import { exec } from '../shared';
|
||||
|
||||
@@ -26,10 +26,10 @@ export const Variables: NodeDef = {
|
||||
return node.collapsed ? { ...shape, label: collapsedTitle(ports, 'variables') } : shape;
|
||||
},
|
||||
read: (ctx, params, _input, def) =>
|
||||
readWithObjects(variableBag(ctx, variablesSourceOf(params)), def.outputs, ctx.isAlive),
|
||||
readLive(variableBag(ctx, variablesSourceOf(params)), def.outputs, ctx.isAlive),
|
||||
run: (ctx, inputs, params, def) => {
|
||||
const bag = variableBag(ctx, variablesSourceOf(params));
|
||||
for (const port of def.inputs) if (port.type !== 'exec') writeWithObjects(bag, port, inputs[port.key]);
|
||||
for (const port of def.inputs) if (port.type !== 'exec') writePort(bag, port, inputs[port.key]);
|
||||
return { next: 'exec' };
|
||||
},
|
||||
};
|
||||
@@ -1,67 +0,0 @@
|
||||
import type { PropertyBag } from '../model/Properties';
|
||||
import type { EntityHandle } from './NodeContext';
|
||||
import type { PortDef } from './NodeRegistry';
|
||||
import { asEntity, type Value, writeTyped } from './values';
|
||||
|
||||
/**
|
||||
* The objects the object variables of a bag hold, beside the bag rather than in it: what sits in a
|
||||
* bag reaches the scene (`World.syncToModel` writes a behavior's changed values back as overrides),
|
||||
* the saves and the inspector, and none of them can keep a live handle. Keyed weakly by the bag, so
|
||||
* a run that ends takes its objects along.
|
||||
*/
|
||||
const slots = new WeakMap<PropertyBag, Map<string, EntityHandle>>();
|
||||
|
||||
/**
|
||||
* The object the variable holds, null for none – or for one destroyed since it was written, so a
|
||||
* graph never goes on acting on an object that has left the game.
|
||||
*/
|
||||
export function readObjectSlot(
|
||||
bag: PropertyBag,
|
||||
key: string,
|
||||
isAlive: (target: EntityHandle) => boolean,
|
||||
): EntityHandle | null {
|
||||
const held = slots.get(bag)?.get(key);
|
||||
if (!held) return null;
|
||||
if (isAlive(held)) return held;
|
||||
slots.get(bag)!.delete(key);
|
||||
return null;
|
||||
}
|
||||
|
||||
export function writeObjectSlot(bag: PropertyBag, key: string, target: EntityHandle | null): void {
|
||||
let held = slots.get(bag);
|
||||
if (!target) {
|
||||
held?.delete(key);
|
||||
return;
|
||||
}
|
||||
if (!held) slots.set(bag, (held = new Map<string, EntityHandle>()));
|
||||
held.set(key, target);
|
||||
}
|
||||
|
||||
/** Hands the objects `keys` hold in `from` on to `to` – a kept variable of the next call (#229). */
|
||||
export function carryObjectSlots(from: PropertyBag, to: PropertyBag, keys: readonly string[]): void {
|
||||
const held = slots.get(from);
|
||||
if (!held) return;
|
||||
for (const key of keys) {
|
||||
const target = held.get(key);
|
||||
if (target && key in to) writeObjectSlot(to, key, target);
|
||||
}
|
||||
}
|
||||
|
||||
/** The bag as a graph reads it through `outputs`: an object port answers with the object held now. */
|
||||
export function readWithObjects(
|
||||
bag: PropertyBag,
|
||||
outputs: readonly PortDef[],
|
||||
isAlive: (target: EntityHandle) => boolean,
|
||||
): Record<string, Value> {
|
||||
const objects = outputs.filter((port) => port.type === 'object');
|
||||
if (objects.length === 0) return bag;
|
||||
const values: Record<string, Value> = { ...bag };
|
||||
for (const port of objects) values[port.key] = readObjectSlot(bag, port.key, isAlive);
|
||||
return values;
|
||||
}
|
||||
|
||||
/** Writes one value arriving on `port`: an object into its slot, anything else typed into the bag. */
|
||||
export function writeWithObjects(bag: PropertyBag, port: PortDef, value: Value | undefined): void {
|
||||
if (port.type === 'object') writeObjectSlot(bag, port.key, asEntity(value));
|
||||
else writeTyped(bag, port.key, value);
|
||||
}
|
||||
@@ -3,6 +3,7 @@ import type { PropertyBag, PropertyValue } from '../model/Properties';
|
||||
import type { Project } from '../model/Project';
|
||||
import { sharedScopeOf } from '../model/Scope';
|
||||
import { coerceValue } from '../properties/PropertySchema';
|
||||
import { withoutObjects } from './liveObjects';
|
||||
|
||||
/** What a save area holds: the values of each persistent Variables asset, keyed by asset id. */
|
||||
export type SavedValues = Record<string, PropertyBag>;
|
||||
@@ -54,7 +55,8 @@ export function savedSnapshot(project: Project, variables: Record<string, Proper
|
||||
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]!;
|
||||
// No object outlives the run, so a save keeps the empty one in its place.
|
||||
for (const def of asset.variables) if (def.key in bag) values[def.key] = withoutObjects(bag[def.key]!);
|
||||
saved[asset.id] = values;
|
||||
}
|
||||
return saved;
|
||||
|
||||
+56
-12
@@ -5,7 +5,10 @@ import {
|
||||
type ListValue,
|
||||
type PropertyBag,
|
||||
type PropertyValue,
|
||||
NO_OBJECT,
|
||||
type StructValue,
|
||||
type TaggedItem,
|
||||
tagOf,
|
||||
textOf,
|
||||
booleanOf,
|
||||
} from '../model/Properties';
|
||||
@@ -13,8 +16,7 @@ import { ENTITY_HANDLE, type EntityHandle } from './NodeContext';
|
||||
|
||||
/**
|
||||
* A list travelling along a data edge. Unlike a stored one it may hold live object handles, which is
|
||||
* what lets `Raycast all` hand out the objects it met (#157). Nothing of the sort reaches a
|
||||
* PropertyBag: everything written into one goes through `storedList`.
|
||||
* what lets `Raycast all` hand out the objects it met (#157).
|
||||
*/
|
||||
export type ValueList = readonly (number | string | boolean | StructValue | EntityHandle)[];
|
||||
|
||||
@@ -58,13 +60,49 @@ export function asList(value: Value | undefined): ValueList {
|
||||
if (isValueList(value)) return value;
|
||||
if (value === null || value === undefined) return [];
|
||||
if (isEntity(value)) return [value];
|
||||
if (isStructValue(value)) return Object.values(value).map(asListItem);
|
||||
if (isStructValue(value)) return Object.values(value).map((field) => asValueItem(field as Value));
|
||||
return [value];
|
||||
}
|
||||
|
||||
/** A list as a PropertyBag may hold it: an object handle becomes its name, since nothing stored is live. */
|
||||
export function storedList(items: ValueList): ListValue {
|
||||
return items.map(asListItem);
|
||||
/**
|
||||
* A handle as a run's bag holds it. Types say a bag is what a file holds, and at rest that is
|
||||
* true: every way out of a run goes through `withoutObjects` (`liveObjects.ts`).
|
||||
*/
|
||||
export function heldObject(handle: EntityHandle): TaggedItem {
|
||||
return handle as unknown as TaggedItem;
|
||||
}
|
||||
|
||||
/** What an object's place in a bag takes: the object that arrives, or the empty one. */
|
||||
function objectPlaceOf(value: Value | undefined): TaggedItem {
|
||||
return isEntity(value) ? heldObject(value) : NO_OBJECT;
|
||||
}
|
||||
|
||||
/** Whether a slot of a run's bag is an object's place: holding one, or holding none (`NO_OBJECT`). */
|
||||
export function isObjectPlace(value: unknown): boolean {
|
||||
return isEntity(value as Value) || tagOf(value) === 'object';
|
||||
}
|
||||
|
||||
/** A value taken out of a list or struct: the empty object's place reads as no object at all. */
|
||||
export function unplaced(value: Value | undefined): Value | undefined {
|
||||
return tagOf(value) === 'object' ? null : value;
|
||||
}
|
||||
|
||||
/** A list as a run's bag holds it: an object stays the object. */
|
||||
function heldList(items: ValueList): ListValue {
|
||||
return items.map((item) => (isEntity(item) ? heldObject(item) : asListItem(item)));
|
||||
}
|
||||
|
||||
/** `value` with every object in it named instead – how a value reads as text, however deep it sits. */
|
||||
export function namedValue(value: Value | undefined): PropertyValue | undefined {
|
||||
if (value === null || value === undefined) return undefined;
|
||||
if (isEntity(value)) return value.name;
|
||||
if (isValueList(value)) return value.map((item) => namedValue(item) as ListValue[number]);
|
||||
if (isStructValue(value) && tagOf(value) === undefined) {
|
||||
const fields: Record<string, FieldValue> = {};
|
||||
for (const [key, field] of Object.entries(value)) fields[key] = namedValue(field) as FieldValue;
|
||||
return fields;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
/** A value narrowed to what a *travelling* list may hold: unlike `asListItem`, a handle stays one. */
|
||||
@@ -78,9 +116,13 @@ function asListItem(value: Value | undefined): number | string | boolean | Struc
|
||||
return asPrimitive(value);
|
||||
}
|
||||
|
||||
/** A value narrowed to what a struct field may hold: a list stays a list, anything deeper flattens. */
|
||||
/**
|
||||
* A value narrowed to what a struct field may hold: a list stays a list and an object the object,
|
||||
* anything deeper flattens.
|
||||
*/
|
||||
export function asField(value: Value | undefined): FieldValue {
|
||||
return isList(value) ? value : asPrimitive(value);
|
||||
if (isEntity(value)) return heldObject(value);
|
||||
return isValueList(value) ? heldList(value) : asPrimitive(value);
|
||||
}
|
||||
|
||||
function asPrimitive(value: Value | undefined): number | string | boolean {
|
||||
@@ -96,9 +138,9 @@ export function asBoolean(value: Value | undefined): boolean {
|
||||
/** Entities read as their name, so a handle can be logged or joined into text directly. */
|
||||
export function asString(value: Value | undefined): string {
|
||||
if (value === null) return '';
|
||||
if (isEntity(value)) return value.name;
|
||||
// A handle among the items would otherwise be printed as the object it is made of.
|
||||
return textOf(isValueList(value) ? storedList(value) : value);
|
||||
// A handle, at the top or in a list or struct, would otherwise be printed as what it is made of.
|
||||
if (isObjectPlace(value)) return isEntity(value) ? value.name : '';
|
||||
return textOf(namedValue(value));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -148,7 +190,9 @@ function mergeStruct(current: StructValue, value: Value | undefined): StructValu
|
||||
|
||||
/** `value` converted to the type the slot already holds, so a number stays a number. */
|
||||
function asTypeOf(current: PropertyValue | undefined, value: Value | undefined): PropertyValue {
|
||||
if (isList(current)) return storedList(asList(value));
|
||||
// First: an object's place is a struct to the eye, and one holding a handle all the more.
|
||||
if (isObjectPlace(current)) return objectPlaceOf(value);
|
||||
if (isList(current)) return heldList(asList(value));
|
||||
if (isStruct(current)) return mergeStruct(current, value);
|
||||
if (typeof current === 'number') return asNumber(value);
|
||||
if (typeof current === 'boolean') return asBoolean(value);
|
||||
|
||||
@@ -44,6 +44,13 @@ export function tagged(kind: TaggedKind, value: number | string): TaggedItem {
|
||||
return { [TAG + kind]: value } as TaggedItem;
|
||||
}
|
||||
|
||||
/**
|
||||
* What an object's place holds at rest – a variable, a struct field, a list item alike. Always
|
||||
* nothing, since an object exists only while the game runs, but tagged, so a run tells the place
|
||||
* apart from an empty text (`graph/liveObjects.ts`).
|
||||
*/
|
||||
export const NO_OBJECT: TaggedItem = tagged('object', '');
|
||||
|
||||
/** A tagged item as what it wraps; anything else as itself. */
|
||||
export function untag(value: PropertyValue | undefined): PropertyValue | undefined {
|
||||
const tag = tagOf(value);
|
||||
@@ -88,6 +95,9 @@ export function colorOf(value: PropertyValue, fallback: number): number {
|
||||
|
||||
/** Values are compared by content, because two equal lists (or structs) are never the same object. */
|
||||
export function sameValue(a: PropertyValue | undefined, b: PropertyValue | undefined): boolean {
|
||||
if (a === b) return true;
|
||||
// A live handle (a run's bag may hold one, branded by a symbol) is only ever itself.
|
||||
if (isBranded(a) || isBranded(b)) return false;
|
||||
if (isList(a) || isList(b)) {
|
||||
if (!isList(a) || !isList(b) || a.length !== b.length) return false;
|
||||
return a.every((item, i) => sameValue(item, b[i]));
|
||||
@@ -100,6 +110,9 @@ export function sameValue(a: PropertyValue | undefined, b: PropertyValue | undef
|
||||
return a === b;
|
||||
}
|
||||
|
||||
const isBranded = (value: unknown): boolean =>
|
||||
isStruct(value) && Object.getOwnPropertySymbols(value).length > 0;
|
||||
|
||||
/** `undefined` removes the key, which is how an undo of "no value" works. */
|
||||
export function setBagValue(bag: PropertyBag, key: string, value: PropertyValue | undefined): void {
|
||||
if (value === undefined) delete bag[key];
|
||||
|
||||
@@ -7,6 +7,7 @@ import {
|
||||
type ListValue,
|
||||
type PropertyBag,
|
||||
type PropertyValue,
|
||||
NO_OBJECT,
|
||||
type StructValue,
|
||||
textOf,
|
||||
} from '../model/Properties';
|
||||
@@ -86,12 +87,13 @@ type Extras<K extends FlatKind> = K extends 'number'
|
||||
: unknown;
|
||||
|
||||
/**
|
||||
* The definition of a flat kind: its default is a value of that kind, as the table says.
|
||||
* The definition of a flat kind: its default is a value of that kind, as the table says. Not an
|
||||
* object's: that default is no value of the kind but its tagged place (`ObjectDef`).
|
||||
* `shownWith` names a switch on the same bag; the inspector shows the row only while it is on
|
||||
* (see `isRowShown`), everything else – graph, defaults, files – always has it.
|
||||
*/
|
||||
export type FlatDef<K extends FlatKind = FlatKind> = {
|
||||
[P in K]: {
|
||||
[P in Exclude<K, 'object'>]: {
|
||||
key: string;
|
||||
label: string;
|
||||
kind: P;
|
||||
@@ -100,7 +102,7 @@ export type FlatDef<K extends FlatKind = FlatKind> = {
|
||||
initial?: ValueOf<P>;
|
||||
shownWith?: string;
|
||||
} & Extras<P>;
|
||||
}[K];
|
||||
}[Exclude<K, 'object'>];
|
||||
|
||||
/** The value a new asset starts with, which is also what Reset goes back to. */
|
||||
export function initialOf(def: PropertyDef): PropertyValue {
|
||||
@@ -161,9 +163,7 @@ export type PropertyDef =
|
||||
| FlatDef
|
||||
/**
|
||||
* A list declares nothing about its items: each one carries its own kind (see `listItems.ts`),
|
||||
* and the graph types a list as a list, so one `list` port fits them all. There is no `object`
|
||||
* item kind: a list is stored in the project file, and an object is a live handle that no reload
|
||||
* survives.
|
||||
* and the graph types a list as a list, so one `list` port fits them all.
|
||||
*/
|
||||
| { key: string; label: string; kind: 'list'; default: ListValue }
|
||||
/**
|
||||
@@ -177,10 +177,7 @@ export type PropertyDef =
|
||||
default: StructValue;
|
||||
fields: readonly StructFieldDef[];
|
||||
}
|
||||
/**
|
||||
* An object variable holds a live handle, which no file and no save can keep: the bag carries only
|
||||
* this empty placeholder, and the handle sits beside it for the run (see `graph/objectSlots.ts`).
|
||||
*/
|
||||
/** An object variable holds a live handle during a run, and nothing at rest (`NO_OBJECT`). */
|
||||
| ObjectDef;
|
||||
|
||||
export interface ObjectDef {
|
||||
@@ -190,8 +187,7 @@ export interface ObjectDef {
|
||||
default: typeof NO_OBJECT;
|
||||
}
|
||||
|
||||
/** What an object variable stores – always nothing, since an object exists only while the game runs. */
|
||||
export const NO_OBJECT = '';
|
||||
export { NO_OBJECT };
|
||||
|
||||
export interface PrimitiveFieldDef {
|
||||
key: string;
|
||||
@@ -769,6 +765,7 @@ export function variableDef(
|
||||
|
||||
/** The empty value of a kind, so a field that is not there yet still has one. */
|
||||
export function emptyFieldValue(field: StructFieldDef): FieldValue {
|
||||
if (field.kind === 'object') return NO_OBJECT;
|
||||
return field.kind === 'list' ? [] : emptyOf(field.kind);
|
||||
}
|
||||
|
||||
@@ -811,5 +808,6 @@ export function coerceValue(def: PropertyDef, value: PropertyValue): PropertyVal
|
||||
|
||||
/** A struct field as its declaration promises. A list field takes only a list – see `ListFieldDef`. */
|
||||
export function coerceFieldValue(value: PropertyValue, field: StructFieldDef): FieldValue {
|
||||
if (field.kind === 'object') return NO_OBJECT;
|
||||
return field.kind === 'list' ? flatListOf(value) : coerceField(value, field.kind);
|
||||
}
|
||||
@@ -158,8 +158,21 @@ export const KINDS = [
|
||||
field: true,
|
||||
port: true,
|
||||
},
|
||||
/** A variable of it holds a live object for the run only; nothing of it is stored (`graph/objectSlots.ts`). */
|
||||
{ kind: 'object', label: 'Object', travels: 'object', variable: true, port: true },
|
||||
/**
|
||||
* A live object, held for the run only. At rest – in a file, a save, the inspector – it is always
|
||||
* the empty one, tagged wherever it sits so a run can tell its place from a text (`NO_OBJECT`).
|
||||
*/
|
||||
{
|
||||
kind: 'object',
|
||||
label: 'Object',
|
||||
travels: 'object',
|
||||
empty: '',
|
||||
coerce: () => '',
|
||||
tagged: true,
|
||||
variable: true,
|
||||
field: true,
|
||||
port: true,
|
||||
},
|
||||
{ kind: 'list', label: 'List', travels: 'list', variable: true, port: true },
|
||||
{ kind: 'struct', label: 'Struct', travels: 'struct', variable: true, port: true },
|
||||
// Schema-only kinds: a setting has them, nothing a user declares does.
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
* from the runtime copy `state.enterPlay()` swaps in and `exitPlay()` throws away.
|
||||
*/
|
||||
import { emptySharedState, type SharedState, worldPositionOf } from '../core/graph';
|
||||
import { namedValue } from '../core/graph/values';
|
||||
import {
|
||||
addGameSystems,
|
||||
type BehaviorSystem,
|
||||
@@ -259,6 +260,7 @@ export class PlaySession {
|
||||
if (this.host.stateMachineEditor.visible) this.host.stateMachineEditor.canvas.redraw();
|
||||
}
|
||||
|
||||
/** The check scripts get the run as JSON, so an object a bag holds is handed over as its name. */
|
||||
snapshot(): WorldSnapshot | null {
|
||||
const world = this.world;
|
||||
if (!world) return null;
|
||||
@@ -274,12 +276,24 @@ export class PlaySession {
|
||||
worldX: w.x,
|
||||
worldY: w.y,
|
||||
parent: e.parent?.objectId ?? '',
|
||||
props: { ...e.props },
|
||||
props: namedBag(e.props),
|
||||
animations: structuredClone(e.animations ?? []),
|
||||
};
|
||||
}),
|
||||
shared: this.behaviors?.shared ?? emptySharedState(),
|
||||
shared: namedShared(this.behaviors?.shared ?? emptySharedState()),
|
||||
log: [...world.log],
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
function namedBag(bag: PropertyBag): Record<string, unknown> {
|
||||
const out: Record<string, unknown> = {};
|
||||
for (const [key, value] of Object.entries(bag)) out[key] = namedValue(value);
|
||||
return out;
|
||||
}
|
||||
|
||||
function namedShared(shared: SharedState): SharedState {
|
||||
const variables: Record<string, PropertyBag> = {};
|
||||
for (const [id, bag] of Object.entries(shared.variables)) variables[id] = namedBag(bag) as PropertyBag;
|
||||
return { variables, states: shared.states };
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import {
|
||||
SetVariablesPersistentCommand,
|
||||
SetVariablesScopeCommand,
|
||||
} from '../../core/commands/assetCommands';
|
||||
import { bagWithoutObjects } from '../../core/graph/liveObjects';
|
||||
import { isPersistent } from '../../core/graph/savedValues';
|
||||
import { nextFieldName, nextVariableName } from '../../core/commands/naming';
|
||||
import {
|
||||
@@ -179,7 +180,7 @@ function assetHandlers(host: InspectorHost, assetId: string, def: PropertyDef):
|
||||
export function shownValues(host: InspectorHost, asset: Asset): PropertyBag {
|
||||
const values = resolveAssetProps(host.state.project, asset);
|
||||
const live = asset.type === 'variables' ? host.liveVariables(asset.id) : null;
|
||||
return live ? { ...values, ...live } : values;
|
||||
return live ? { ...values, ...bagWithoutObjects(live) } : values;
|
||||
}
|
||||
|
||||
/** Declared variables, each with its declaration editable – plus an "add" menu. */
|
||||
|
||||
+11
-4
@@ -1,7 +1,8 @@
|
||||
import { ENTITY_HANDLE, isHandleEnabled, type PointerState, worldMatrixOf } from '../core/graph';
|
||||
import { newId } from '../core/ids';
|
||||
import type { Asset, TilemapAsset } from '../core/model/Asset';
|
||||
import type { PropertyBag, Vec2 } from '../core/model/Properties';
|
||||
import { withoutObjects } from '../core/graph/liveObjects';
|
||||
import { type PropertyBag, sameValue, type Vec2 } from '../core/model/Properties';
|
||||
import { isPlaceableType } from '../core/model/AssetTypes';
|
||||
import { bakeInstances, materializeInstance, objectRef, resolveObjects } from '../core/model/instance';
|
||||
import { treeOrder } from '../core/model/hierarchy';
|
||||
@@ -245,7 +246,9 @@ export class World {
|
||||
entity.z = object.position.z;
|
||||
const resolved = resolveSceneObjectProps(this.project, asset, object);
|
||||
for (const key of Object.keys(entity.props)) if (!(key in resolved)) delete entity.props[key];
|
||||
Object.assign(entity.props, resolved);
|
||||
// The scene only ever got the objects' empty places, so a value that differs by them alone stays.
|
||||
for (const [key, value] of Object.entries(resolved))
|
||||
if (!sameValue(value, withoutObjects(entity.props[key]))) entity.props[key] = value;
|
||||
return entity;
|
||||
}
|
||||
|
||||
@@ -305,14 +308,18 @@ export class World {
|
||||
const p = object.position;
|
||||
const moved = p.x !== entity.x || p.y !== entity.y || p.z !== entity.z;
|
||||
const base = resolveSceneObjectProps(this.project, asset, object);
|
||||
const drifted = Object.keys(entity.props).filter((k) => k in base && base[k] !== entity.props[k]);
|
||||
// What a run holds of an object stays in the run; the scene gets the empty one.
|
||||
const drifted = Object.keys(entity.props).filter(
|
||||
(k) =>
|
||||
k in base && base[k] !== entity.props[k] && !sameValue(base[k], withoutObjects(entity.props[k])),
|
||||
);
|
||||
if (!moved && drifted.length === 0) continue;
|
||||
// Only now, because a ref for resolved content costs a resolve of its own – and because a
|
||||
// direct write to such an object would land in the freshly built copy and be lost.
|
||||
const ref = objectRef(this.project, scene, object.id);
|
||||
if (!ref) continue;
|
||||
if (moved) ref.setPosition({ x: entity.x, y: entity.y, z: entity.z });
|
||||
for (const key of drifted) ref.setOverride(key, entity.props[key]);
|
||||
for (const key of drifted) ref.setOverride(key, withoutObjects(entity.props[key]));
|
||||
values = true;
|
||||
}
|
||||
return { structure: spawned || scene.objects.length !== before, values };
|
||||
|
||||
Reference in new issue
Block a user