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

+54 -13
View File
@@ -2,7 +2,7 @@ import { blockProblem, findMode, MATCH_INPUT, matchOf, searchOutcome, stopsAtHit
import type { Graph, GraphNode } from './Graph';
import { type GraphCache, type GraphIndex, searchingIndex } from './graphIndex';
import { type GraphLookup, NO_LOOKUP } from './lookup';
import { BEHAVIOR_END, callEntries, terminalNode } from './nested';
import { BEHAVIOR_END, BEHAVIOR_START, callEntries } from './nested';
import { fieldValue, splitField, withSplitValues } from './portSplits';
import type { PropertyBag, PropertyValue } from '../model/Properties';
import type { NodeContext } from './NodeContext';
@@ -41,6 +41,14 @@ interface Frame {
index: GraphIndex;
outputCache: Map<string, Record<string, Value>>;
dataCache: Map<string, Record<string, Value>>;
/** The flow input this graph's end node was reached by last, once it was reached at all (#294). */
ended?: string;
}
/** How a graph was left: through which flow input of its end node, and with what. */
export interface Ending {
port: string;
values: Record<string, Value>;
}
/**
@@ -83,6 +91,11 @@ export class GraphInterpreter {
private callSites: string[] = [];
private steps = 0;
trace: TraceListener | null = null;
/**
* What the start node hands out while this graph runs on its own events and not as a call: the
* inputs of the `On behavior` node listening to it (#294). Set by whoever sends the pulses.
*/
given: Record<string, Value> | null = null;
constructor(
readonly graph: Graph,
@@ -101,6 +114,14 @@ export class GraphInterpreter {
return { graph, index: this.indexOf(graph), outputCache: new Map(), dataCache: new Map() };
}
/** A frame of the graph itself, its start node holding what was given from outside. */
private ownFrame(): Frame {
const frame = this.emptyFrame(this.graph);
const start = this.given ? frame.index.ofType(BEHAVIOR_START)[0] : undefined;
if (start && this.given) frame.outputCache.set(start.id, { ...this.given });
return frame;
}
eventNodes(type: string): readonly GraphNode[] {
return this.indexOf(this.graph).ofType(type);
}
@@ -110,7 +131,7 @@ export class GraphInterpreter {
* through – a state event names one.
*/
pulse(node: GraphNode, ctx: NodeContext, outputs: Record<string, Value> = {}, port = 'exec'): void {
this.frame = this.emptyFrame(this.graph);
this.frame = this.ownFrame();
this.callStack = [];
this.callSites = [];
this.steps = 0;
@@ -125,12 +146,30 @@ export class GraphInterpreter {
* the node at all (#160). Null for an input the node does not have.
*/
eventInput(node: GraphNode, key: string, ctx: NodeContext): Value {
this.frame = this.emptyFrame(this.graph);
this.frame = this.ownFrame();
this.steps = 0;
const port = this.def(node)?.inputs.find((p) => p.key === key);
return port ? this.inputValue(node, port, ctx) : null;
}
/** Every data input of an event node, read ahead of a pulse like `eventInput`. */
eventInputs(node: GraphNode, ctx: NodeContext): Record<string, Value> {
this.frame = this.ownFrame();
this.steps = 0;
return this.collectInputs(node, ctx);
}
/** How the last pulse left this graph, or null when it never reached the end node. */
get ending(): Ending | null {
return this.endingOf(this.frame);
}
private endingOf(frame: Frame): Ending | null {
if (frame.ended === undefined) return null;
const end = frame.index.ofType(BEHAVIOR_END)[0];
return { port: frame.ended, values: (end && frame.outputCache.get(end.id)) ?? {} };
}
/**
* The `On state entered` / `On state exited` nodes watching one machine. A node whose machine
* input carries a running value rather than a pick has no state outputs at all, so it is never
@@ -161,30 +200,33 @@ export class GraphInterpreter {
if (!def?.run) return;
const inputs = this.collectInputs(node, ctx);
const result = def.run(ctx, inputs, node.params, def, entry, this.lookup);
const returned = result.call ? this.call(result.call, node.id, ctx, depth) : {};
const produced = { ...result.outputs, ...returned };
const returned = result.call ? this.call(result.call, node.id, ctx, depth) : null;
const produced = { ...result.outputs, ...returned?.values };
if (node.type === BEHAVIOR_END) this.frame.ended = entry;
if (!def.read) this.frame.outputCache.set(node.id, produced);
this.trace?.onNode(node.id);
if (this.trace?.onValues) this.record(node.id, def.read ? this.readAll(node, def, ctx) : produced);
if (result.repeat) this.loop(node, result.repeat, ctx, depth);
if (result.find) this.search(node, result.find, ctx, depth);
const next = result.next === undefined ? [] : Array.isArray(result.next) ? result.next : [result.next];
for (const port of next) this.follow(node, port, ctx, depth);
// The way out the called graph took comes first, so `exec` reads as "and then" (#294).
const ways = returned && returned.port !== 'exec' ? [returned.port, ...next] : next;
for (const port of ways) this.follow(node, port, ctx, depth);
}
/**
* Runs another behavior from its start node (or, lacking one, its `On start` nodes) to its end
* node and returns what the end node was handed. The nested graph gets its own frame and a fresh
* node and returns how it was left; null when it never got there. The nested graph gets its own frame and a fresh
* copy of its variable defaults, so it is a call and not an inlining: nothing of it survives the
* return but the values it hands back – and the variables it keeps, which wait for the next call
* from the same site.
*/
private call(call: CallResult, site: string, ctx: NodeContext, depth: number): Record<string, Value> {
private call(call: CallResult, site: string, ctx: NodeContext, depth: number): Ending | null {
const program = this.lookup.behavior(call.behaviorId);
if (!program) return {};
if (!program) return null;
if (this.callStack.includes(call.behaviorId)) throw new Error(`Behavior "${program.name}" runs itself`);
const entries = callEntries(program.graph);
if (entries.length === 0) return {};
if (entries.length === 0) return null;
const sites = [...this.callSites, site].join('/');
const kept = program.kept ?? [];
const variables = resumedVariables(program.defaults, this.keptCalls.get(sites), kept);
@@ -212,10 +254,9 @@ export class GraphInterpreter {
try {
for (const entry of entries) {
this.trace?.onNode(entry.id);
this.follow(entry, 'exec', inner, depth + 1);
this.follow(entry, call.entry, inner, depth + 1);
}
const end = terminalNode(program.graph, BEHAVIOR_END);
return (end && this.frame.outputCache.get(end.id)) ?? {};
return this.endingOf(this.frame);
} finally {
this.callStack.pop();
this.callSites.pop();
+2
View File
@@ -61,6 +61,8 @@ export interface RepeatResult {
export interface CallResult {
behaviorId: string;
inputs: Record<string, Value>;
/** The flow input the call came in by, which is the output its start node leaves through (#294). */
entry: string;
}
export interface RunResult {
+45
View File
@@ -20,6 +20,7 @@ import { agreedType, type SharedPortGroup } from './portTypes';
export const BEHAVIOR_START = 'BehaviorStart';
export const BEHAVIOR_END = 'BehaviorEnd';
export const RUN_BEHAVIOR = 'RunBehavior';
export const ON_BEHAVIOR = 'OnBehavior';
export const VARIABLES = 'Variables';
export const PROPERTIES = 'Properties';
const ON_START = 'OnStart';
@@ -123,6 +124,16 @@ export function isTerminalNode(node: GraphNode): boolean {
return isTerminalType(node.type);
}
/** The two nodes that stand for another behavior: the one running it, the one listening to it (#294). */
export function namesBehavior(type: string): boolean {
return type === RUN_BEHAVIOR || type === ON_BEHAVIOR;
}
/** A further flow port of a start or end node, next to its fixed `exec` (#294). */
export function isFlowPort(port: DeclaredPort): boolean {
return port.kind === 'flow';
}
/**
* 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.
@@ -324,6 +335,7 @@ export function ghostProblem(node: GraphNode, graph: Graph, def: NodeDef): strin
* `Lerp` depends on another asset as well, the curve it eases along (#247), and says so here too.
*/
export function callProblem(node: GraphNode, lookup: GraphLookup, ownerId = ''): string | null {
if (node.type === ON_BEHAVIOR) return listenProblem(node, lookup, ownerId);
if (node.type !== RUN_BEHAVIOR) return lerpProblem(node, lookup);
const id = textOf(node.params[BEHAVIOR_PARAM] ?? '');
if (!id) return 'No behavior picked';
@@ -334,6 +346,39 @@ export function callProblem(node: GraphNode, lookup: GraphLookup, ownerId = ''):
return null;
}
/** Why an `On behavior` never fires: it hears nothing, itself, or a graph without an end to reach. */
function listenProblem(node: GraphNode, lookup: GraphLookup, ownerId: string): string | null {
const id = textOf(node.params[BEHAVIOR_PARAM] ?? '');
if (!id) return 'No behavior picked';
const program = lookup.behavior(id);
if (!program) return 'That behavior is gone';
if (ownerId && listensTo(lookup, id, ownerId)) return 'A behavior cannot listen to itself';
if (!terminalNode(program.graph, BEHAVIOR_END)) return 'No End node – never fires';
return null;
}
/** The behaviors the `On behavior` nodes of that graph name, picked or not found alike. */
export function heardBehaviors(graph: Graph): string[] {
return graph.nodes
.filter((n) => n.type === ON_BEHAVIOR)
.map((n) => textOf(n.params[BEHAVIOR_PARAM] ?? ''))
.filter((id) => id !== '');
}
/** Whether `from` is `target` or hears it, however many behaviors lie between. */
export function listensTo(
lookup: GraphLookup,
from: string,
target: string,
seen: Set<string> = new Set(),
): boolean {
if (from === target) return true;
if (seen.has(from)) return false;
seen.add(from);
const program = lookup.behavior(from);
return program ? heardBehaviors(program.graph).some((id) => listensTo(lookup, id, target, seen)) : false;
}
/**
* Everything that changes how a node looks, as one string. Views and the inspector rebuild when it
* changes – a port list that is derived rather than fixed has no other way of being noticed.
+32
View File
@@ -0,0 +1,32 @@
import type { NodeDef } from '../../NodeRegistry';
import { textOf } from '../../../model/Properties';
import { BEHAVIOR_END, BEHAVIOR_PARAM, BEHAVIOR_START, declaredPorts, ON_BEHAVIOR } from '../../nested';
import { exec } from '../shared';
/**
* Another behavior used as an event (#294): it lives at this node for as long as the behavior around
* it runs, its own events fire, and each of its pulses that reaches its end node goes on here. The
* running game keeps that second behavior; the interpreter only ever sees the pulse arriving.
*/
export const OnBehavior: NodeDef = {
type: ON_BEHAVIOR,
label: 'On behavior',
category: 'event',
description:
'Listens to another behavior. That behavior runs here with its own events and variables, and ' +
'whenever one of its flows reaches its End, this node fires: through the output named like ' +
'the flow port the End was reached by last, with the values the End was handed. The inputs ' +
'are what its Start hands out.',
inputs: [],
outputs: [exec()],
params: [{ key: BEHAVIOR_PARAM, label: 'Behavior', kind: 'asset', default: '', assetTypes: ['behavior'] }],
derivePorts: ({ node, lookup }) => {
const program = lookup.behavior(textOf(node.params[BEHAVIOR_PARAM] ?? ''));
return {
label: program ? `On ${program.name}` : undefined,
// No flow in: the behavior starts its own pulses, so the ways into its Start have no use here.
inputs: declaredPorts(program?.graph, BEHAVIOR_START).filter((port) => port.type !== 'exec'),
outputs: declaredPorts(program?.graph, BEHAVIOR_END),
};
},
};
+2 -1
View File
@@ -13,7 +13,8 @@ export const BehaviorEnd: NodeDef = {
category: 'flow',
description:
'Ends this behavior and hands its results back to the graph that ran it. Every output declared ' +
'here becomes an output port on that node.',
'here becomes an output port on that node. An output of the kind Flow is a further way out: ' +
'the node that ran or hears this behavior goes on through the one reached last.',
inputs: [exec()],
outputs: [],
params: [],
+2 -1
View File
@@ -12,7 +12,8 @@ export const BehaviorStart: NodeDef = {
category: 'event',
description:
'Where this behavior begins when another graph runs it as a node. Every input declared here ' +
'becomes an input port on that node.',
'becomes an input port on that node. An input of the kind Flow is a further way in: a call ' +
'arriving through it leaves here through the output of that name and not through the plain one.',
inputs: [],
outputs: [exec()],
params: [],
+5 -3
View File
@@ -16,7 +16,9 @@ export const RunBehavior: NodeDef = {
'Runs another behavior as a step of this one, passing values in and taking results out. It ' +
'begins at the Behavior start node, or at On start if that behavior has none. A ' +
'variable marked Keep in that behavior carries on from the last call of this node on this ' +
'object; the others start at their defaults every time.',
'object; the others start at their defaults every time. A flow port declared at that ' +
"behavior's Start is a further way in, one declared at its End a further way out: the " +
'call leaves through the one its End was reached by last, and then through the plain one.',
inputs: [exec()],
outputs: [exec()],
params: [{ key: BEHAVIOR_PARAM, label: 'Behavior', kind: 'asset', default: '', assetTypes: ['behavior'] }],
@@ -29,8 +31,8 @@ export const RunBehavior: NodeDef = {
outputs: declaredPorts(program?.graph, BEHAVIOR_END),
};
},
run: (_ctx, inputs, params) => ({
call: { behaviorId: textOf(params[BEHAVIOR_PARAM] ?? ''), inputs },
run: (_ctx, inputs, params, _def, entry) => ({
call: { behaviorId: textOf(params[BEHAVIOR_PARAM] ?? ''), inputs, entry },
next: 'exec',
}),
};
+2
View File
@@ -106,6 +106,7 @@ import { SetItem } from './data/SetItem';
import { SubList } from './data/SubList';
import { TextureSize } from './data/TextureSize';
import { OnAnimationFinished } from './events/OnAnimationFinished';
import { OnBehavior } from './events/OnBehavior';
import { OnClick } from './events/OnClick';
import { OnMouseWheel } from './events/OnMouseWheel';
import { OnCollisionEnter } from './events/OnCollisionEnter';
@@ -160,6 +161,7 @@ export const BUILT_IN_NODES: readonly NodeDef[] = [
OnClick,
OnMouseWheel,
OnAnimationFinished,
OnBehavior,
OnStateEntered,
OnStateExited,
BehaviorStart,