Give a behavior more ways in and out, and hear it as an event (#294)
game-maker / gate (push) Successful in 2m48s
game-maker / suite (push) Failing after 13m34s

A behavior run by another one had a single flow port at its Start
and at its End, so it could not branch for its caller. And it could
not be used as an event: a call needs a pulse at its flow input,
and the events of a called graph never fire. An input like
"Confirm" (Space or Enter) had to be built again in every graph.

- A declared port of a Start or an End node can be of the kind
  Flow. It sits above the values and shares their names, exec stays
  reserved. Only these two nodes declare one, a struct does not.
- Run behavior shows a flow port of the Start as a further flow
  input. A call arriving through it leaves the Start through the
  output of that name and not through exec.
- Run behavior shows a flow port of the End as a further flow
  output. The call goes on through the one the End was reached by
  last, and then through exec. exec fires always, as before.
- On behavior is a new event node without a flow input. The
  behavior it names runs at the node for as long as the behavior
  around it runs, with all of its own events and one set of
  variables per node.
- Its outputs are those of the End. It fires once the inner pulse
  is through, by the output the End was reached by last, exec only
  for End.exec. A pulse that never reaches the End is not seen.
- The value ports of the Start are its inputs, read anew for each
  inner pulse. The Start itself never fires there.
- It nests to any depth. Under a Run behavior it stays silent. A
  behavior hearing itself, however far round, is not started and
  the node says "A behavior cannot listen to itself".
- The palette offers every other behavior with an End once more as
  "On <name>". A node naming one without End says so.
- An On node removed while the game runs ends its behavior with On
  disable and On destroy. The sounds of the entity end only with
  the behavior placed on it.
- Renaming and deleting a declared port follows the edges at On
  behavior nodes as it does at Run behavior nodes.
- The flow outputs of the Start of a behavior placed on an object
  never fire, like its exec.
- The engine prompt of the benchmark names flow ports and On
  behavior.

Tests:
- Gate on melektaus-atx right before this commit: 215 files, 3319
  tests (3278 + 41), exit 0.
- e2e 19 on melektaus-atx: 39 checks (+4), all pass. e2e
  1-6,17-24,26,36,51: 628 checks, all pass.
- 19 unit tests in flow-ports.test.ts, 19 in on-behavior.test.ts
  on a real world, the rest in the node catalog.
- Four new e2e checks in module 19: a flow port declared by mouse
  at the End, On Confirm in the graph, the call leaving through the
  further output while playing, Enter and Space on a real keyboard.
- 19 unit counter-probes all fail their tests. One did not at first
  (a removed node kept its instance), a test was added. Two browser
  counter-probes fail their checks.
- One screenshot looked at: On Confirm with exec and pressed, the
  second flow pin at the calling node.
- The e2e sum of 1680 is counted (1676 + 4), the full suite and
  the smokes did not run.
- Not seen in the browser: flow inputs at the Start, nesting, the
  ring, values through On behavior, the palette entry. No test
  holds that the sounds of the entity go on when a heard behavior
  ends.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
adminandClaude Opus 5.5 committed 2026-10-02 19:33:34 +02:00
1 parent b5339b96c2
commit 46c620ae3b
26 files changed
+1211 -42

No files matched your search

+98 -3
View File
@@ -1,16 +1,19 @@
import {
adoptDeclarations,
asBoolean,
BEHAVIOR_PARAM,
buildSharedState,
carriedForward,
declaredVariables,
emptySharedState,
type Graph,
GraphInterpreter,
type GraphNode,
hierarchyRanks,
inEventOrder,
isHandleEnabled,
type NodeContext,
ON_BEHAVIOR,
restartedAssets,
restoreSaved,
type SaveArea,
@@ -356,6 +359,7 @@ export class BehaviorSystem implements System {
this.instances.push(instance);
born.add(instance);
}
if (born.size > 0 || this.unranked) this.syncHeard(world, born);
if (born.size > 0 || this.unranked) this.rank(world);
const fresh = this.instances.filter((i) => born.has(i));
for (const instance of fresh) this.enterStartStates(instance, (starts ??= startStates(this.project)));
@@ -365,6 +369,66 @@ export class BehaviorSystem implements System {
this.flushStateChanges();
}
/**
* One further instance per `On behavior` node (#294), on the entity of the behavior holding it and
* for as long as that one runs – and so on down, since the behavior heard may listen in turn. An
* edit to a graph while it runs adds and drops them like objects coming and going.
*/
private syncHeard(world: World, born: Set<Instance>): void {
const held = new Map<Instance, Instance[]>();
for (const instance of this.instances) {
const host = instance.host?.instance;
if (host) held.set(host, [...(held.get(host) ?? []), instance]);
}
const dropped: Instance[] = [];
const drop = (instance: Instance) => {
dropped.push(instance);
for (const below of held.get(instance) ?? []) drop(below);
};
const visit = (host: Instance, chain: readonly string[]) => {
const below = held.get(host) ?? [];
const kept = new Set<Instance>();
for (const node of host.interpreter.eventNodes(ON_BEHAVIOR)) {
const behaviorId = textOf(node.params[BEHAVIOR_PARAM] ?? '');
// A behavior hearing itself, however far round, would be built without end.
if (chain.includes(behaviorId) || !findAssetOf(this.project, behaviorId, 'behavior')) continue;
const heard =
below.find((i) => i.host?.nodeId === node.id && i.host.behaviorId === behaviorId) ??
this.hear(host, node.id, behaviorId, world);
if (!heard) continue;
if (!below.includes(heard)) born.add(heard);
kept.add(heard);
visit(heard, [...chain, behaviorId]);
}
for (const instance of below) if (!kept.has(instance)) drop(instance);
};
for (const instance of [...this.instances])
if (!instance.host) visit(instance, [instance.entity.assetId]);
if (dropped.length === 0) return;
this.instances = this.instances.filter((i) => !dropped.includes(i));
this.finish(dropped);
}
/** A running copy of that behavior for one `On behavior` node, with variables of its own. */
private hear(host: Instance, nodeId: string, behaviorId: string, world: World): Instance | null {
const lookup = graphLookup(this.project);
const program = lookup.behavior(behaviorId);
if (!program) return null;
const heard: Instance = {
entity: host.entity,
interpreter: new GraphInterpreter(program.graph, lookup, this.graphs),
trace: { edges: new Map(), nodes: new Map() },
values: new Map(),
states: {},
active: true,
host: { instance: host, nodeId, behaviorId },
variables: structuredClone(program.defaults),
};
this.listen(heard, world);
this.instances.push(heard);
return heard;
}
/** Has the instance note what it runs, timed by the world it runs in. */
private listen({ interpreter, trace, values }: Instance, world: World): void {
interpreter.trace = {
@@ -488,7 +552,8 @@ export class BehaviorSystem implements System {
this.runPulses(pulsesOf(instances, 'OnDestroy'), contextOf);
// After `On destroy`, not before: that graph may still say something, and a sound it wants to
// outlive the object says so itself by being detached.
for (const instance of instances) this.audio.stopOwner(instance.entity.id);
// Its sounds are the entity's, so they end with the behavior placed there and not with one it hears.
for (const instance of instances) if (!instance.host) this.audio.stopOwner(instance.entity.id);
}
/**
@@ -579,7 +644,7 @@ export class BehaviorSystem implements System {
}
runningIds(): string[] {
return this.instances.map((i) => i.entity.objectId);
return this.instances.filter((i) => !i.host).map((i) => i.entity.objectId);
}
/**
@@ -594,7 +659,8 @@ export class BehaviorSystem implements System {
const graph = findAssetOf(this.project, assetId, 'behavior')?.graph;
const newest = new Map<string, Produced>();
for (const instance of this.instances) {
if (instance.entity.assetId !== assetId && !instance.interpreter.calledBehaviors.has(assetId)) continue;
const own = instance.host?.behaviorId ?? instance.entity.assetId;
if (own !== assetId && !instance.interpreter.calledBehaviors.has(assetId)) continue;
latest(merged.edges, instance.trace.edges);
latest(merged.nodes, instance.trace.nodes);
for (const [id, produced] of (graph && instance.values.get(graph)) ?? []) {
@@ -617,7 +683,9 @@ export class BehaviorSystem implements System {
for (const { instance, node, outputs, port } of inEventOrder(pulses)) {
if (living && !world.entities.includes(instance.entity)) continue;
try {
this.give(instance, contextOf);
instance.interpreter.pulse(node, contextOf(instance), outputs, port);
this.surface(instance, contextOf);
} catch (error) {
world.log.push(
`${ERROR_PREFIX} "${instance.entity.name}": ${error instanceof Error ? error.message : String(error)}`,
@@ -627,6 +695,33 @@ export class BehaviorSystem implements System {
}
}
/** The `On behavior` node an instance is heard by, as the graph holds it now. */
private hearing(instance: Instance): { host: Instance; node: GraphNode } | null {
const host = instance.host;
const node = host?.instance.interpreter.eventNodes(ON_BEHAVIOR).find((n) => n.id === host.nodeId);
return host && node ? { host: host.instance, node } : null;
}
/** A heard behavior's start node hands out the inputs of the node hearing it, read anew for each pulse. */
private give(instance: Instance, contextOf: (instance: Instance) => NodeContext): void {
const heard = this.hearing(instance);
if (heard)
instance.interpreter.given = heard.host.interpreter.eventInputs(heard.node, contextOf(heard.host));
}
/**
* A pulse of a heard behavior that reached its end node goes on at the node hearing it, through
* the output named like the way it took – and from there further up, if that graph ends in turn.
*/
private surface(instance: Instance, contextOf: (instance: Instance) => NodeContext): void {
for (let inner = instance; inner.host; inner = inner.host.instance) {
const ending = inner.interpreter.ending;
const heard = this.hearing(inner);
if (!ending || !heard) return;
heard.host.interpreter.pulse(heard.node, contextOf(heard.host), ending.values, ending.port);
}
}
/** One context per instance for as long as the answer is used – a frame, or one round of events. */
private contexts(world: World, dt: number): (instance: Instance) => NodeContext {
const made = new Map<Instance, NodeContext>();
+1 -1
View File
@@ -52,7 +52,7 @@ export function behaviorContext(
return {
self: entity,
owner: entity.parent,
variables: entity.props,
variables: instance.variables ?? entity.props,
states,
shared: run.shared,
stateChanged: (machineId, from, to) => run.stateChanged(machineId, from, to),
+8 -1
View File
@@ -6,7 +6,7 @@
import { asNumber, type Graph, type GraphInterpreter, type GraphNode, type Value } from '../../core/graph';
import { stepKeyRepeat } from '../../core/graph/keyRepeat';
import { keyName } from '../../core/keys';
import { textOf } from '../../core/model/Properties';
import { type PropertyBag, textOf } from '../../core/model/Properties';
import { parentChain } from '../../core/model/walk';
import type { Entity } from '../Entity';
import type { AnimationSystem } from './AnimationSystem';
@@ -29,6 +29,13 @@ export interface Instance {
states: Record<string, string>;
/** Whether the behavior was effectively on when last looked at; a change fires On enable/On disable. */
active: boolean;
/**
* Set on a behavior an `On behavior` node listens to (#294): it runs on the same entity as the
* one holding that node, which hears every pulse of it that reaches the end node.
*/
host?: { instance: Instance; nodeId: string; behaviorId: string };
/** Its own variables, where it has a host; the entity's belong to the behavior placed on it. */
variables?: PropertyBag;
}
/** One event node about to run for one instance: what the event hands it, and the output it leaves by. */