Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

feat(logic): a graph can reach a keyboard, another entity, and who an event named

esengineb865c707f16c
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 3036795ea..691fd345c 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -42,9 +42,26 @@ published separately; it ships inside the editor.
A `.esgraph` IS the runtime definition. Nothing compiles it, so the graph the
editor runs and the graph the package ships are the same bytes, and a graph is
attached to an entity the way an FSM is: a `ScriptGraphAgent` naming it.
- `examples/script-graph-demo` is two squares that move because of what they
- were drawn to do, one of them through a value node the project registered
- itself.
+
+ The vocabulary reaches what a game needs: the keyboard (`input.axis`,
+ `input.down`, `input.pressed`, `input.pointer`), another entity
+ (`entity.byName`, and an optional target on `property.get`/`property.set`),
+ who else an event named (`event.on` publishes `other`, which is the contact's
+ partner on a physics event), and `entity.despawn`. Input arrives through the
+ context every authored surface shares, so a state machine's hook and a
+ behaviour tree's leaf can read the keyboard now too — `AiContext` was
+ documented as a `BehaviorContext` superset and was missing the one thing
+ gameplay is written against.
+
+ Two projects carry it. `examples/script-graph-demo` is two squares that move
+ because of what they were drawn to do, one of them through a value node the
+ project registered itself. `examples/dodge-graph` is a GAME — move with the
+ arrows, dodge the falling blocks, score, lose, press Space to start over —
+ with no gameplay code at all: one component declares what the run remembers
+ (graph variables belong to one entity; a score three blocks and a HUD agree on
+ does not), and three graphs do the rest. A gate plays it: holds a key and the
+ player moves, releases it and they stop, lets a block through and the HUD says
+ so, presses Space and the run begins again.
### Fixed
diff --git a/desktop b/desktop
index 16aef840f..ee2dd9cab 160000
--- a/desktop
+++ b/desktop
@@ -1 +1 @@
-Subproject commit 16aef840f29a9b93ea6182e6640645943f4bfb05
+Subproject commit ee2dd9cabac51c270209c0fec27e172980e50ce1
diff --git a/examples/dodge-graph/.gitignore b/examples/dodge-graph/.gitignore
new file mode 100644
index 000000000..359472ac2
--- /dev/null
+++ b/examples/dodge-graph/.gitignore
@@ -0,0 +1,7 @@
+node_modules/
+dist/
+.vscode/
+.idea/
+.DS_Store
+Thumbs.db
+.esengine/cache/
diff --git a/examples/dodge-graph/assets/logic/block.esgraph b/examples/dodge-graph/assets/logic/block.esgraph
new file mode 100644
index 000000000..1e813db36
--- /dev/null
+++ b/examples/dodge-graph/assets/logic/block.esgraph
@@ -0,0 +1,764 @@
+{
+ "version": "1.0",
+ "name": "Block",
+ "nodes": [
+ {
+ "id": "upd",
+ "kind": "event.update",
+ "x": 0,
+ "y": 60
+ },
+ {
+ "id": "step",
+ "kind": "call",
+ "x": 68,
+ "y": 440,
+ "ref": "math.multiply",
+ "literals": {
+ "a": -320
+ }
+ },
+ {
+ "id": "getY",
+ "kind": "var.get",
+ "x": 68,
+ "y": 630,
+ "literals": {
+ "name": "y"
+ }
+ },
+ {
+ "id": "nextY",
+ "kind": "call",
+ "x": 284,
+ "y": 250,
+ "ref": "math.add"
+ },
+ {
+ "id": "setY",
+ "kind": "var.set",
+ "x": 500,
+ "y": 60,
+ "literals": {
+ "name": "y"
+ }
+ },
+ {
+ "id": "writeY",
+ "kind": "call",
+ "x": 750,
+ "y": 60,
+ "ref": "property.set",
+ "literals": {
+ "path": "Transform.position.y"
+ }
+ },
+ {
+ "id": "below",
+ "kind": "call",
+ "x": 784,
+ "y": 250,
+ "ref": "compare.less",
+ "literals": {
+ "b": -340
+ }
+ },
+ {
+ "id": "wrap",
+ "kind": "flow.branch",
+ "x": 1000,
+ "y": 60
+ },
+ {
+ "id": "top",
+ "kind": "var.set",
+ "x": 1250,
+ "y": 60,
+ "literals": {
+ "name": "y"
+ }
+ },
+ {
+ "id": "topVal",
+ "kind": "lit.number",
+ "x": 1034,
+ "y": 250,
+ "literals": {
+ "value": 360
+ }
+ },
+ {
+ "id": "newX",
+ "kind": "call",
+ "x": 1284,
+ "y": 250,
+ "ref": "math.random",
+ "literals": {
+ "min": -340,
+ "max": 340
+ }
+ },
+ {
+ "id": "setX",
+ "kind": "var.set",
+ "x": 1500,
+ "y": 60,
+ "literals": {
+ "name": "x"
+ }
+ },
+ {
+ "id": "writeX",
+ "kind": "call",
+ "x": 1750,
+ "y": 60,
+ "ref": "property.set",
+ "literals": {
+ "path": "Transform.position.x"
+ }
+ },
+ {
+ "id": "getX",
+ "kind": "var.get",
+ "x": 1534,
+ "y": 250,
+ "literals": {
+ "name": "x"
+ }
+ },
+ {
+ "id": "game",
+ "kind": "call",
+ "x": -216,
+ "y": 250,
+ "ref": "entity.byName",
+ "literals": {
+ "name": "Game"
+ }
+ },
+ {
+ "id": "score",
+ "kind": "call",
+ "x": 1568,
+ "y": 440,
+ "ref": "property.get",
+ "literals": {
+ "path": "GameState.score"
+ }
+ },
+ {
+ "id": "plus",
+ "kind": "call",
+ "x": 1784,
+ "y": 250,
+ "ref": "math.add",
+ "literals": {
+ "b": 1
+ }
+ },
+ {
+ "id": "bump",
+ "kind": "call",
+ "x": 2000,
+ "y": 60,
+ "ref": "property.set",
+ "literals": {
+ "path": "GameState.score"
+ }
+ },
+ {
+ "id": "hit",
+ "kind": "flow.branch",
+ "x": 1250,
+ "y": 60
+ },
+ {
+ "id": "player",
+ "kind": "call",
+ "x": -216,
+ "y": 440,
+ "ref": "entity.byName",
+ "literals": {
+ "name": "Player"
+ }
+ },
+ {
+ "id": "px",
+ "kind": "call",
+ "x": -216,
+ "y": 630,
+ "ref": "property.get",
+ "literals": {
+ "path": "Transform.position.x"
+ }
+ },
+ {
+ "id": "py",
+ "kind": "call",
+ "x": -216,
+ "y": 820,
+ "ref": "property.get",
+ "literals": {
+ "path": "Transform.position.y"
+ }
+ },
+ {
+ "id": "dx",
+ "kind": "call",
+ "x": -216,
+ "y": 1010,
+ "ref": "math.subtract"
+ },
+ {
+ "id": "dy",
+ "kind": "call",
+ "x": -216,
+ "y": 1200,
+ "ref": "math.subtract"
+ },
+ {
+ "id": "adx",
+ "kind": "call",
+ "x": -216,
+ "y": 1390,
+ "ref": "math.abs"
+ },
+ {
+ "id": "ady",
+ "kind": "call",
+ "x": -216,
+ "y": 1580,
+ "ref": "math.abs"
+ },
+ {
+ "id": "nearX",
+ "kind": "call",
+ "x": 818,
+ "y": 630,
+ "ref": "compare.less",
+ "literals": {
+ "b": 62
+ }
+ },
+ {
+ "id": "nearY",
+ "kind": "call",
+ "x": 818,
+ "y": 820,
+ "ref": "compare.less",
+ "literals": {
+ "b": 62
+ }
+ },
+ {
+ "id": "both",
+ "kind": "call",
+ "x": 1034,
+ "y": 440,
+ "ref": "logic.and"
+ },
+ {
+ "id": "out",
+ "kind": "call",
+ "x": 1500,
+ "y": 60,
+ "ref": "property.set",
+ "literals": {
+ "path": "GameState.alive",
+ "value": "false"
+ }
+ },
+ {
+ "id": "start",
+ "kind": "event.start",
+ "x": 0,
+ "y": 960
+ },
+ {
+ "id": "seed",
+ "kind": "call",
+ "ref": "property.get",
+ "x": 34,
+ "y": 1150,
+ "literals": {
+ "path": "Transform.position.y"
+ }
+ },
+ {
+ "id": "seedSet",
+ "kind": "var.set",
+ "x": 250,
+ "y": 960,
+ "literals": {
+ "name": "y"
+ }
+ },
+ {
+ "id": "alive",
+ "kind": "call",
+ "ref": "property.get",
+ "x": 34,
+ "y": 250,
+ "literals": {
+ "path": "GameState.alive"
+ }
+ },
+ {
+ "id": "live",
+ "kind": "flow.branch",
+ "x": 250,
+ "y": 60
+ },
+ {
+ "id": "waitY",
+ "kind": "call",
+ "ref": "math.random",
+ "x": 284,
+ "y": 440,
+ "literals": {
+ "min": 380,
+ "max": 820
+ }
+ },
+ {
+ "id": "park",
+ "kind": "var.set",
+ "x": 500,
+ "y": 60,
+ "literals": {
+ "name": "y"
+ }
+ },
+ {
+ "id": "parkW",
+ "kind": "call",
+ "ref": "property.set",
+ "x": 750,
+ "y": 60,
+ "literals": {
+ "path": "Transform.position.y"
+ }
+ },
+ {
+ "id": "getY_1",
+ "kind": "var.get",
+ "x": 534,
+ "y": 250,
+ "literals": {
+ "name": "y"
+ }
+ },
+ {
+ "id": "getY_2",
+ "kind": "var.get",
+ "x": 568,
+ "y": 440,
+ "literals": {
+ "name": "y"
+ }
+ },
+ {
+ "id": "getY_3",
+ "kind": "var.get",
+ "x": -432,
+ "y": 1390,
+ "literals": {
+ "name": "y"
+ }
+ },
+ {
+ "id": "getY_4",
+ "kind": "var.get",
+ "x": 534,
+ "y": 440,
+ "literals": {
+ "name": "y"
+ }
+ },
+ {
+ "id": "getX_1",
+ "kind": "var.get",
+ "x": -432,
+ "y": 1200,
+ "literals": {
+ "name": "x"
+ }
+ },
+ {
+ "id": "game_1",
+ "kind": "call",
+ "x": 1784,
+ "y": 440,
+ "ref": "entity.byName",
+ "literals": {
+ "name": "Game"
+ }
+ },
+ {
+ "id": "game_2",
+ "kind": "call",
+ "x": 1284,
+ "y": 440,
+ "ref": "entity.byName",
+ "literals": {
+ "name": "Game"
+ }
+ },
+ {
+ "id": "game_3",
+ "kind": "call",
+ "x": -182,
+ "y": 440,
+ "ref": "entity.byName",
+ "literals": {
+ "name": "Game"
+ }
+ },
+ {
+ "id": "player_1",
+ "kind": "call",
+ "x": -432,
+ "y": 1010,
+ "ref": "entity.byName",
+ "literals": {
+ "name": "Player"
+ }
+ }
+ ],
+ "edges": [
+ {
+ "id": "upd.dt->step.b",
+ "from": "upd",
+ "fromPort": "dt",
+ "to": "step",
+ "toPort": "b"
+ },
+ {
+ "id": "getY.value->nextY.a",
+ "from": "getY",
+ "fromPort": "value",
+ "to": "nextY",
+ "toPort": "a"
+ },
+ {
+ "id": "step.result->nextY.b",
+ "from": "step",
+ "fromPort": "result",
+ "to": "nextY",
+ "toPort": "b"
+ },
+ {
+ "id": "nextY.result->setY.value",
+ "from": "nextY",
+ "fromPort": "result",
+ "to": "setY",
+ "toPort": "value"
+ },
+ {
+ "id": "setY.then->writeY.",
+ "from": "setY",
+ "fromPort": "then",
+ "to": "writeY",
+ "toPort": ""
+ },
+ {
+ "id": "getY_1.value->writeY.value",
+ "from": "getY_1",
+ "fromPort": "value",
+ "to": "writeY",
+ "toPort": "value"
+ },
+ {
+ "id": "writeY.then->wrap.",
+ "from": "writeY",
+ "fromPort": "then",
+ "to": "wrap",
+ "toPort": ""
+ },
+ {
+ "id": "getY_2.value->below.a",
+ "from": "getY_2",
+ "fromPort": "value",
+ "to": "below",
+ "toPort": "a"
+ },
+ {
+ "id": "below.result->wrap.cond",
+ "from": "below",
+ "fromPort": "result",
+ "to": "wrap",
+ "toPort": "cond"
+ },
+ {
+ "id": "wrap.true->top.",
+ "from": "wrap",
+ "fromPort": "true",
+ "to": "top",
+ "toPort": ""
+ },
+ {
+ "id": "topVal.value->top.value",
+ "from": "topVal",
+ "fromPort": "value",
+ "to": "top",
+ "toPort": "value"
+ },
+ {
+ "id": "top.then->setX.",
+ "from": "top",
+ "fromPort": "then",
+ "to": "setX",
+ "toPort": ""
+ },
+ {
+ "id": "newX.result->setX.value",
+ "from": "newX",
+ "fromPort": "result",
+ "to": "setX",
+ "toPort": "value"
+ },
+ {
+ "id": "setX.then->writeX.",
+ "from": "setX",
+ "fromPort": "then",
+ "to": "writeX",
+ "toPort": ""
+ },
+ {
+ "id": "getX.value->writeX.value",
+ "from": "getX",
+ "fromPort": "value",
+ "to": "writeX",
+ "toPort": "value"
+ },
+ {
+ "id": "writeX.then->bump.",
+ "from": "writeX",
+ "fromPort": "then",
+ "to": "bump",
+ "toPort": ""
+ },
+ {
+ "id": "game.entity->score.entity",
+ "from": "game",
+ "fromPort": "entity",
+ "to": "score",
+ "toPort": "entity"
+ },
+ {
+ "id": "score.value->plus.a",
+ "from": "score",
+ "fromPort": "value",
+ "to": "plus",
+ "toPort": "a"
+ },
+ {
+ "id": "plus.result->bump.value",
+ "from": "plus",
+ "fromPort": "result",
+ "to": "bump",
+ "toPort": "value"
+ },
+ {
+ "id": "game_1.entity->bump.entity",
+ "from": "game_1",
+ "fromPort": "entity",
+ "to": "bump",
+ "toPort": "entity"
+ },
+ {
+ "id": "wrap.false->hit.",
+ "from": "wrap",
+ "fromPort": "false",
+ "to": "hit",
+ "toPort": ""
+ },
+ {
+ "id": "player.entity->px.entity",
+ "from": "player",
+ "fromPort": "entity",
+ "to": "px",
+ "toPort": "entity"
+ },
+ {
+ "id": "player_1.entity->py.entity",
+ "from": "player_1",
+ "fromPort": "entity",
+ "to": "py",
+ "toPort": "entity"
+ },
+ {
+ "id": "px.value->dx.b",
+ "from": "px",
+ "fromPort": "value",
+ "to": "dx",
+ "toPort": "b"
+ },
+ {
+ "id": "getX_1.value->dx.a",
+ "from": "getX_1",
+ "fromPort": "value",
+ "to": "dx",
+ "toPort": "a"
+ },
+ {
+ "id": "py.value->dy.b",
+ "from": "py",
+ "fromPort": "value",
+ "to": "dy",
+ "toPort": "b"
+ },
+ {
+ "id": "getY_3.value->dy.a",
+ "from": "getY_3",
+ "fromPort": "value",
+ "to": "dy",
+ "toPort": "a"
+ },
+ {
+ "id": "dx.result->adx.a",
+ "from": "dx",
+ "fromPort": "result",
+ "to": "adx",
+ "toPort": "a"
+ },
+ {
+ "id": "dy.result->ady.a",
+ "from": "dy",
+ "fromPort": "result",
+ "to": "ady",
+ "toPort": "a"
+ },
+ {
+ "id": "adx.result->nearX.a",
+ "from": "adx",
+ "fromPort": "result",
+ "to": "nearX",
+ "toPort": "a"
+ },
+ {
+ "id": "ady.result->nearY.a",
+ "from": "ady",
+ "fromPort": "result",
+ "to": "nearY",
+ "toPort": "a"
+ },
+ {
+ "id": "nearX.result->both.a",
+ "from": "nearX",
+ "fromPort": "result",
+ "to": "both",
+ "toPort": "a"
+ },
+ {
+ "id": "nearY.result->both.b",
+ "from": "nearY",
+ "fromPort": "result",
+ "to": "both",
+ "toPort": "b"
+ },
+ {
+ "id": "both.result->hit.cond",
+ "from": "both",
+ "fromPort": "result",
+ "to": "hit",
+ "toPort": "cond"
+ },
+ {
+ "id": "hit.true->out.",
+ "from": "hit",
+ "fromPort": "true",
+ "to": "out",
+ "toPort": ""
+ },
+ {
+ "id": "game_2.entity->out.entity",
+ "from": "game_2",
+ "fromPort": "entity",
+ "to": "out",
+ "toPort": "entity"
+ },
+ {
+ "id": "start.then->seedSet.",
+ "from": "start",
+ "fromPort": "then",
+ "to": "seedSet",
+ "toPort": ""
+ },
+ {
+ "id": "seed.value->seedSet.value",
+ "from": "seed",
+ "fromPort": "value",
+ "to": "seedSet",
+ "toPort": "value"
+ },
+ {
+ "id": "upd.then->live.",
+ "from": "upd",
+ "fromPort": "then",
+ "to": "live",
+ "toPort": ""
+ },
+ {
+ "id": "alive.value->live.cond",
+ "from": "alive",
+ "fromPort": "value",
+ "to": "live",
+ "toPort": "cond"
+ },
+ {
+ "id": "live.true->setY.",
+ "from": "live",
+ "fromPort": "true",
+ "to": "setY",
+ "toPort": ""
+ },
+ {
+ "id": "game_3.entity->alive.entity",
+ "from": "game_3",
+ "fromPort": "entity",
+ "to": "alive",
+ "toPort": "entity"
+ },
+ {
+ "id": "live.false->park.",
+ "from": "live",
+ "fromPort": "false",
+ "to": "park",
+ "toPort": ""
+ },
+ {
+ "id": "waitY.result->park.value",
+ "from": "waitY",
+ "fromPort": "result",
+ "to": "park",
+ "toPort": "value"
+ },
+ {
+ "id": "park.then->parkW.",
+ "from": "park",
+ "fromPort": "then",
+ "to": "parkW",
+ "toPort": ""
+ },
+ {
+ "id": "getY_4.value->parkW.value",
+ "from": "getY_4",
+ "fromPort": "value",
+ "to": "parkW",
+ "toPort": "value"
+ }
+ ],
+ "variables": [
+ {
+ "name": "y",
+ "type": "number",
+ "default": 340
+ },
+ {
+ "name": "x",
+ "type": "number",
+ "default": 0
+ }
+ ]
+}
\ No newline at end of file
diff --git a/examples/dodge-graph/assets/logic/block.esgraph.meta b/examples/dodge-graph/assets/logic/block.esgraph.meta
new file mode 100644
index 000000000..595ba2e16
--- /dev/null
+++ b/examples/dodge-graph/assets/logic/block.esgraph.meta
@@ -0,0 +1,6 @@
+{
+ "uuid": "7c0d0b10-2c44-4a8e-9f21-7c6b1d3e4b02",
+ "version": "2.0",
+ "type": "scriptgraph",
+ "importer": {}
+}
\ No newline at end of file
diff --git a/examples/dodge-graph/assets/logic/hud.esgraph b/examples/dodge-graph/assets/logic/hud.esgraph
new file mode 100644
index 000000000..8b2501a19
--- /dev/null
+++ b/examples/dodge-graph/assets/logic/hud.esgraph
@@ -0,0 +1,247 @@
+{
+ "version": "1.0",
+ "name": "Hud",
+ "nodes": [
+ {
+ "id": "upd",
+ "kind": "event.update",
+ "x": 0,
+ "y": 60
+ },
+ {
+ "id": "game",
+ "kind": "call",
+ "x": -182,
+ "y": 440,
+ "ref": "entity.byName",
+ "literals": {
+ "name": "Game"
+ }
+ },
+ {
+ "id": "alive",
+ "kind": "call",
+ "x": 34,
+ "y": 250,
+ "ref": "property.get",
+ "literals": {
+ "path": "GameState.alive"
+ }
+ },
+ {
+ "id": "score",
+ "kind": "call",
+ "x": 68,
+ "y": 440,
+ "ref": "property.get",
+ "literals": {
+ "path": "GameState.score"
+ }
+ },
+ {
+ "id": "gate",
+ "kind": "flow.branch",
+ "x": 250,
+ "y": 60
+ },
+ {
+ "id": "label",
+ "kind": "call",
+ "x": 284,
+ "y": 250,
+ "ref": "string.concat",
+ "literals": {
+ "a": "SCORE "
+ }
+ },
+ {
+ "id": "live",
+ "kind": "call",
+ "x": 500,
+ "y": 60,
+ "ref": "property.set",
+ "literals": {
+ "path": "Text.content"
+ }
+ },
+ {
+ "id": "over",
+ "kind": "call",
+ "x": 500,
+ "y": 60,
+ "ref": "property.set",
+ "literals": {
+ "path": "Text.content",
+ "value": "GAME OVER - SPACE"
+ }
+ },
+ {
+ "id": "space",
+ "kind": "call",
+ "x": 534,
+ "y": 250,
+ "ref": "input.pressed",
+ "literals": {
+ "key": "Space"
+ }
+ },
+ {
+ "id": "again",
+ "kind": "flow.branch",
+ "x": 750,
+ "y": 60
+ },
+ {
+ "id": "reset",
+ "kind": "call",
+ "x": 1000,
+ "y": 60,
+ "ref": "property.set",
+ "literals": {
+ "path": "GameState.alive",
+ "value": "true"
+ }
+ },
+ {
+ "id": "zero",
+ "kind": "call",
+ "x": 1250,
+ "y": 60,
+ "ref": "property.set",
+ "literals": {
+ "path": "GameState.score",
+ "value": "0"
+ }
+ },
+ {
+ "id": "game_1",
+ "kind": "call",
+ "x": -148,
+ "y": 630,
+ "ref": "entity.byName",
+ "literals": {
+ "name": "Game"
+ }
+ },
+ {
+ "id": "game_2",
+ "kind": "call",
+ "x": 784,
+ "y": 250,
+ "ref": "entity.byName",
+ "literals": {
+ "name": "Game"
+ }
+ },
+ {
+ "id": "game_3",
+ "kind": "call",
+ "x": 1034,
+ "y": 250,
+ "ref": "entity.byName",
+ "literals": {
+ "name": "Game"
+ }
+ }
+ ],
+ "edges": [
+ {
+ "id": "upd.then->gate.",
+ "from": "upd",
+ "fromPort": "then",
+ "to": "gate",
+ "toPort": ""
+ },
+ {
+ "id": "game.entity->alive.entity",
+ "from": "game",
+ "fromPort": "entity",
+ "to": "alive",
+ "toPort": "entity"
+ },
+ {
+ "id": "game_1.entity->score.entity",
+ "from": "game_1",
+ "fromPort": "entity",
+ "to": "score",
+ "toPort": "entity"
+ },
+ {
+ "id": "alive.value->gate.cond",
+ "from": "alive",
+ "fromPort": "value",
+ "to": "gate",
+ "toPort": "cond"
+ },
+ {
+ "id": "gate.true->live.",
+ "from": "gate",
+ "fromPort": "true",
+ "to": "live",
+ "toPort": ""
+ },
+ {
+ "id": "score.text->label.b",
+ "from": "score",
+ "fromPort": "text",
+ "to": "label",
+ "toPort": "b"
+ },
+ {
+ "id": "label.result->live.value",
+ "from": "label",
+ "fromPort": "result",
+ "to": "live",
+ "toPort": "value"
+ },
+ {
+ "id": "gate.false->over.",
+ "from": "gate",
+ "fromPort": "false",
+ "to": "over",
+ "toPort": ""
+ },
+ {
+ "id": "over.then->again.",
+ "from": "over",
+ "fromPort": "then",
+ "to": "again",
+ "toPort": ""
+ },
+ {
+ "id": "space.result->again.cond",
+ "from": "space",
+ "fromPort": "result",
+ "to": "again",
+ "toPort": "cond"
+ },
+ {
+ "id": "again.true->reset.",
+ "from": "again",
+ "fromPort": "true",
+ "to": "reset",
+ "toPort": ""
+ },
+ {
+ "id": "game_2.entity->reset.entity",
+ "from": "game_2",
+ "fromPort": "entity",
+ "to": "reset",
+ "toPort": "entity"
+ },
+ {
+ "id": "reset.then->zero.",
+ "from": "reset",
+ "fromPort": "then",
+ "to": "zero",
+ "toPort": ""
+ },
+ {
+ "id": "game_3.entity->zero.entity",
+ "from": "game_3",
+ "fromPort": "entity",
+ "to": "zero",
+ "toPort": "entity"
+ }
+ ]
+}
\ No newline at end of file
diff --git a/examples/dodge-graph/assets/logic/hud.esgraph.meta b/examples/dodge-graph/assets/logic/hud.esgraph.meta
new file mode 100644
index 000000000..5997856b3
--- /dev/null
+++ b/examples/dodge-graph/assets/logic/hud.esgraph.meta
@@ -0,0 +1,6 @@
+{
+ "uuid": "7c0d0b10-2c44-4a8e-9f21-7c6b1d3e4b03",
+ "version": "2.0",
+ "type": "scriptgraph",
+ "importer": {}
+}
\ No newline at end of file
diff --git a/examples/dodge-graph/assets/logic/player.esgraph b/examples/dodge-graph/assets/logic/player.esgraph
new file mode 100644
index 000000000..44f20a2a6
--- /dev/null
+++ b/examples/dodge-graph/assets/logic/player.esgraph
@@ -0,0 +1,260 @@
+{
+ "version": "1.0",
+ "name": "Player",
+ "nodes": [
+ {
+ "id": "upd",
+ "kind": "event.update",
+ "x": 0,
+ "y": 60
+ },
+ {
+ "id": "game",
+ "kind": "call",
+ "x": -182,
+ "y": 440,
+ "ref": "entity.byName",
+ "literals": {
+ "name": "Game"
+ }
+ },
+ {
+ "id": "alive",
+ "kind": "call",
+ "x": 34,
+ "y": 250,
+ "ref": "property.get",
+ "literals": {
+ "path": "GameState.alive"
+ }
+ },
+ {
+ "id": "gate",
+ "kind": "flow.branch",
+ "x": 250,
+ "y": 60
+ },
+ {
+ "id": "axis",
+ "kind": "call",
+ "x": -216,
+ "y": 250,
+ "ref": "input.axis",
+ "literals": {
+ "negative": "ArrowLeft",
+ "positive": "ArrowRight"
+ }
+ },
+ {
+ "id": "speed",
+ "kind": "call",
+ "x": -216,
+ "y": 440,
+ "ref": "math.multiply",
+ "literals": {
+ "b": 430
+ }
+ },
+ {
+ "id": "step",
+ "kind": "call",
+ "x": -216,
+ "y": 630,
+ "ref": "math.multiply"
+ },
+ {
+ "id": "getX",
+ "kind": "var.get",
+ "x": -216,
+ "y": 820,
+ "literals": {
+ "name": "x"
+ }
+ },
+ {
+ "id": "sum",
+ "kind": "call",
+ "x": 68,
+ "y": 440,
+ "ref": "math.add"
+ },
+ {
+ "id": "clamp",
+ "kind": "call",
+ "x": 284,
+ "y": 250,
+ "ref": "math.clamp",
+ "literals": {
+ "min": -360,
+ "max": 360
+ }
+ },
+ {
+ "id": "setX",
+ "kind": "var.set",
+ "x": 500,
+ "y": 60,
+ "literals": {
+ "name": "x"
+ }
+ },
+ {
+ "id": "write",
+ "kind": "call",
+ "x": 750,
+ "y": 60,
+ "ref": "property.set",
+ "literals": {
+ "path": "Transform.position.x"
+ }
+ },
+ {
+ "id": "start",
+ "kind": "event.start",
+ "x": 0,
+ "y": 960
+ },
+ {
+ "id": "seed",
+ "kind": "call",
+ "ref": "property.get",
+ "x": 34,
+ "y": 1150,
+ "literals": {
+ "path": "Transform.position.x"
+ }
+ },
+ {
+ "id": "seedSet",
+ "kind": "var.set",
+ "x": 250,
+ "y": 960,
+ "literals": {
+ "name": "x"
+ }
+ },
+ {
+ "id": "getX_1",
+ "kind": "var.get",
+ "x": 534,
+ "y": 250,
+ "literals": {
+ "name": "x"
+ }
+ }
+ ],
+ "edges": [
+ {
+ "id": "upd.then->gate.",
+ "from": "upd",
+ "fromPort": "then",
+ "to": "gate",
+ "toPort": ""
+ },
+ {
+ "id": "game.entity->alive.entity",
+ "from": "game",
+ "fromPort": "entity",
+ "to": "alive",
+ "toPort": "entity"
+ },
+ {
+ "id": "alive.value->gate.cond",
+ "from": "alive",
+ "fromPort": "value",
+ "to": "gate",
+ "toPort": "cond"
+ },
+ {
+ "id": "gate.true->setX.",
+ "from": "gate",
+ "fromPort": "true",
+ "to": "setX",
+ "toPort": ""
+ },
+ {
+ "id": "axis.result->speed.a",
+ "from": "axis",
+ "fromPort": "result",
+ "to": "speed",
+ "toPort": "a"
+ },
+ {
+ "id": "speed.result->step.a",
+ "from": "speed",
+ "fromPort": "result",
+ "to": "step",
+ "toPort": "a"
+ },
+ {
+ "id": "upd.dt->step.b",
+ "from": "upd",
+ "fromPort": "dt",
+ "to": "step",
+ "toPort": "b"
+ },
+ {
+ "id": "getX.value->sum.a",
+ "from": "getX",
+ "fromPort": "value",
+ "to": "sum",
+ "toPort": "a"
+ },
+ {
+ "id": "step.result->sum.b",
+ "from": "step",
+ "fromPort": "result",
+ "to": "sum",
+ "toPort": "b"
+ },
+ {
+ "id": "sum.result->clamp.value",
+ "from": "sum",
+ "fromPort": "result",
+ "to": "clamp",
+ "toPort": "value"
+ },
+ {
+ "id": "clamp.result->setX.value",
+ "from": "clamp",
+ "fromPort": "result",
+ "to": "setX",
+ "toPort": "value"
+ },
+ {
+ "id": "setX.then->write.",
+ "from": "setX",
+ "fromPort": "then",
+ "to": "write",
+ "toPort": ""
+ },
+ {
+ "id": "getX_1.value->write.value",
+ "from": "getX_1",
+ "fromPort": "value",
+ "to": "write",
+ "toPort": "value"
+ },
+ {
+ "id": "start.then->seedSet.",
+ "from": "start",
+ "fromPort": "then",
+ "to": "seedSet",
+ "toPort": ""
+ },
+ {
+ "id": "seed.value->seedSet.value",
+ "from": "seed",
+ "fromPort": "value",
+ "to": "seedSet",
+ "toPort": "value"
+ }
+ ],
+ "variables": [
+ {
+ "name": "x",
+ "type": "number",
+ "default": 0
+ }
+ ]
+}
\ No newline at end of file
diff --git a/examples/dodge-graph/assets/logic/player.esgraph.meta b/examples/dodge-graph/assets/logic/player.esgraph.meta
new file mode 100644
index 000000000..1b81e0a2a
--- /dev/null
+++ b/examples/dodge-graph/assets/logic/player.esgraph.meta
@@ -0,0 +1,6 @@
+{
+ "uuid": "7c0d0b10-2c44-4a8e-9f21-7c6b1d3e4b01",
+ "version": "2.0",
+ "type": "scriptgraph",
+ "importer": {}
+}
\ No newline at end of file
diff --git a/examples/dodge-graph/assets/scenes/main.esscene b/examples/dodge-graph/assets/scenes/main.esscene
new file mode 100644
index 000000000..87768ef4e
--- /dev/null
+++ b/examples/dodge-graph/assets/scenes/main.esscene
@@ -0,0 +1,258 @@
+{
+ "version": 4,
+ "name": "Main",
+ "entities": [
+ {
+ "id": 0,
+ "name": "Camera",
+ "parent": null,
+ "children": [],
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": 0,
+ "y": 0,
+ "z": 10
+ }
+ }
+ },
+ {
+ "type": "Camera",
+ "data": {
+ "projectionType": 1,
+ "orthoSize": 320,
+ "isActive": true,
+ "priority": 0
+ }
+ }
+ ]
+ },
+ {
+ "id": 1,
+ "name": "Game",
+ "parent": null,
+ "children": [],
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": 0,
+ "y": 0,
+ "z": 0
+ }
+ }
+ },
+ {
+ "type": "GameState",
+ "data": {
+ "alive": true,
+ "score": 0
+ }
+ }
+ ]
+ },
+ {
+ "id": 2,
+ "name": "Player",
+ "parent": null,
+ "children": [],
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": 0,
+ "y": -250,
+ "z": 0
+ }
+ }
+ },
+ {
+ "type": "Sprite",
+ "data": {
+ "size": {
+ "x": 76,
+ "y": 24
+ },
+ "color": {
+ "r": 0.42,
+ "g": 0.72,
+ "b": 0.62,
+ "a": 1
+ }
+ }
+ },
+ {
+ "type": "ScriptGraphAgent",
+ "data": {
+ "graph": "assets/logic/player.esgraph"
+ }
+ }
+ ]
+ },
+ {
+ "id": 3,
+ "name": "Block1",
+ "parent": null,
+ "children": [],
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": -220,
+ "y": 340,
+ "z": 0
+ }
+ }
+ },
+ {
+ "type": "Sprite",
+ "data": {
+ "size": {
+ "x": 56,
+ "y": 56
+ },
+ "color": {
+ "r": 0.8,
+ "g": 0.42,
+ "b": 0.36,
+ "a": 1
+ }
+ }
+ },
+ {
+ "type": "ScriptGraphAgent",
+ "data": {
+ "graph": "assets/logic/block.esgraph"
+ }
+ }
+ ]
+ },
+ {
+ "id": 4,
+ "name": "Block2",
+ "parent": null,
+ "children": [],
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": 150,
+ "y": 560,
+ "z": 0
+ }
+ }
+ },
+ {
+ "type": "Sprite",
+ "data": {
+ "size": {
+ "x": 56,
+ "y": 56
+ },
+ "color": {
+ "r": 0.8,
+ "g": 0.42,
+ "b": 0.36,
+ "a": 1
+ }
+ }
+ },
+ {
+ "type": "ScriptGraphAgent",
+ "data": {
+ "graph": "assets/logic/block.esgraph"
+ }
+ }
+ ]
+ },
+ {
+ "id": 5,
+ "name": "Block3",
+ "parent": null,
+ "children": [],
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": 260,
+ "y": 800,
+ "z": 0
+ }
+ }
+ },
+ {
+ "type": "Sprite",
+ "data": {
+ "size": {
+ "x": 56,
+ "y": 56
+ },
+ "color": {
+ "r": 0.8,
+ "g": 0.42,
+ "b": 0.36,
+ "a": 1
+ }
+ }
+ },
+ {
+ "type": "ScriptGraphAgent",
+ "data": {
+ "graph": "assets/logic/block.esgraph"
+ }
+ }
+ ]
+ },
+ {
+ "id": 6,
+ "name": "ScoreText",
+ "parent": null,
+ "children": [],
+ "visible": true,
+ "components": [
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": -360,
+ "y": 280,
+ "z": 0
+ }
+ }
+ },
+ {
+ "type": "Text",
+ "data": {
+ "content": "SCORE 0",
+ "fontSize": 30,
+ "color": {
+ "r": 0.92,
+ "g": 0.92,
+ "b": 0.88,
+ "a": 1
+ }
+ }
+ },
+ {
+ "type": "ScriptGraphAgent",
+ "data": {
+ "graph": "assets/logic/hud.esgraph"
+ }
+ }
+ ]
+ }
+ ]
+}
\ No newline at end of file
diff --git a/examples/dodge-graph/assets/scenes/main.esscene.meta b/examples/dodge-graph/assets/scenes/main.esscene.meta
new file mode 100644
index 000000000..324f79ca3
--- /dev/null
+++ b/examples/dodge-graph/assets/scenes/main.esscene.meta
@@ -0,0 +1,8 @@
+{
+ "uuid": "7c0d0b10-2c44-4a8e-9f21-7c6b1d3e4b00",
+ "version": "2.0",
+ "type": "scene",
+ "importer": {
+ "autoMigrate": true
+ }
+}
\ No newline at end of file
diff --git a/examples/dodge-graph/project.esproject b/examples/dodge-graph/project.esproject
new file mode 100644
index 000000000..d4199ad46
--- /dev/null
+++ b/examples/dodge-graph/project.esproject
@@ -0,0 +1,10 @@
+{
+ "formatVersion": "1",
+ "name": "Dodge",
+ "description": "A whole game whose logic is script graphs — move, dodge, score, lose, restart. No gameplay code.",
+ "tag": "Scripting",
+ "version": "0.1.0",
+ "defaultScene": "assets/scenes/main.esscene",
+ "designResolution": { "width": 800, "height": 600 },
+ "spineVersion": "none"
+}
diff --git a/examples/dodge-graph/src/components.ts b/examples/dodge-graph/src/components.ts
new file mode 100644
index 000000000..8d7d3e011
--- /dev/null
+++ b/examples/dodge-graph/src/components.ts
@@ -0,0 +1,23 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
+/**
+ * @file components.ts
+ * @brief What the game remembers. All of it, and the only code here.
+ *
+ * A graph's variables belong to the entity running it, which is right for a
+ * block's own height and wrong for the score: three blocks and a HUD have to
+ * agree on it. The engine already has one place a fact can live where anything
+ * can read it — a component — so the shared state is one, on one entity, and
+ * the graphs reach it with `property.get` / `property.set` naming that entity.
+ *
+ * Data is a component, logic is a graph. There is no gameplay code in this
+ * project beyond this declaration.
+ */
+import { defineComponent } from 'esengine';
+
+export const GameState = defineComponent('GameState', {
+ /** False once a block has caught the player; the graphs gate on it. */
+ alive: true,
+ /** Blocks dodged. */
+ score: 0,
+});
diff --git a/examples/dodge-graph/src/main.ts b/examples/dodge-graph/src/main.ts
new file mode 100644
index 000000000..264914d55
--- /dev/null
+++ b/examples/dodge-graph/src/main.ts
@@ -0,0 +1,7 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
+/**
+ * @file main.ts
+ * @brief Startup. There is no gameplay code in this project; see components.ts.
+ */
+import './components';
diff --git a/examples/dodge-graph/tsconfig.json b/examples/dodge-graph/tsconfig.json
new file mode 100644
index 000000000..1f11ff310
--- /dev/null
+++ b/examples/dodge-graph/tsconfig.json
@@ -0,0 +1,20 @@
+{
+ "compilerOptions": {
+ "target": "ES2020",
+ "module": "ESNext",
+ "moduleResolution": "bundler",
+ "strict": true,
+ "esModuleInterop": true,
+ "skipLibCheck": true,
+ "forceConsistentCasingInFileNames": true,
+ "declaration": false,
+ "outDir": "./dist",
+ "rootDir": ".",
+ "paths": {
+ "esengine": ["./.esengine/sdk/index.d.ts"],
+ "esengine/wasm": ["./.esengine/sdk/wasm.d.ts"]
+ }
+ },
+ "include": ["src/**/*"],
+ "exclude": ["node_modules"]
+}
diff --git a/examples/script-graph-demo/assets/logic/bounce.esgraph b/examples/script-graph-demo/assets/logic/bounce.esgraph
index d6b9c3a16..505723367 100644
--- a/examples/script-graph-demo/assets/logic/bounce.esgraph
+++ b/examples/script-graph-demo/assets/logic/bounce.esgraph
@@ -2,41 +2,267 @@
"version": "1.0",
"name": "Bounce",
"variables": [
- { "name": "x", "type": "number", "default": -220 },
- { "name": "dir", "type": "number", "default": 1 }
+ {
+ "name": "x",
+ "type": "number",
+ "default": -220
+ },
+ {
+ "name": "dir",
+ "type": "number",
+ "default": 1
+ }
],
"nodes": [
- { "id": "update", "kind": "event.update", "x": 40, "y": 60 },
- { "id": "getX", "kind": "var.get", "x": 40, "y": 200, "literals": { "name": "x" } },
- { "id": "getDir", "kind": "var.get", "x": 40, "y": 290, "literals": { "name": "dir" } },
- { "id": "speed", "kind": "call", "ref": "math.multiply", "x": 240, "y": 290, "literals": { "b": 260 } },
- { "id": "step", "kind": "call", "ref": "math.multiply", "x": 430, "y": 290 },
- { "id": "moved", "kind": "call", "ref": "math.add", "x": 620, "y": 200 },
- { "id": "setX", "kind": "var.set", "x": 300, "y": 60, "literals": { "name": "x" } },
- { "id": "write", "kind": "call", "ref": "property.set", "x": 500, "y": 60,
- "literals": { "path": "Transform.position.x" } },
- { "id": "past", "kind": "call", "ref": "compare.greater", "x": 620, "y": 420, "literals": { "b": 220 } },
- { "id": "absX", "kind": "call", "ref": "math.abs", "x": 430, "y": 420 },
- { "id": "turn", "kind": "flow.branch", "x": 760, "y": 60 },
- { "id": "flip", "kind": "call", "ref": "math.multiply", "x": 900, "y": 300, "literals": { "b": -1 } },
- { "id": "setDir", "kind": "var.set", "x": 940, "y": 60, "literals": { "name": "dir" } }
+ {
+ "id": "update",
+ "kind": "event.update",
+ "x": 0,
+ "y": 60
+ },
+ {
+ "id": "getX",
+ "kind": "var.get",
+ "x": -182,
+ "y": 440,
+ "literals": {
+ "name": "x"
+ }
+ },
+ {
+ "id": "getDir",
+ "kind": "var.get",
+ "x": -216,
+ "y": 250,
+ "literals": {
+ "name": "dir"
+ }
+ },
+ {
+ "id": "speed",
+ "kind": "call",
+ "ref": "math.multiply",
+ "x": -216,
+ "y": 440,
+ "literals": {
+ "b": 260
+ }
+ },
+ {
+ "id": "step",
+ "kind": "call",
+ "ref": "math.multiply",
+ "x": -182,
+ "y": 630
+ },
+ {
+ "id": "moved",
+ "kind": "call",
+ "ref": "math.add",
+ "x": 34,
+ "y": 250
+ },
+ {
+ "id": "setX",
+ "kind": "var.set",
+ "x": 250,
+ "y": 60,
+ "literals": {
+ "name": "x"
+ }
+ },
+ {
+ "id": "write",
+ "kind": "call",
+ "ref": "property.set",
+ "x": 500,
+ "y": 60,
+ "literals": {
+ "path": "Transform.position.x"
+ }
+ },
+ {
+ "id": "past",
+ "kind": "call",
+ "ref": "compare.greater",
+ "x": 534,
+ "y": 250,
+ "literals": {
+ "b": 220
+ }
+ },
+ {
+ "id": "absX",
+ "kind": "call",
+ "ref": "math.abs",
+ "x": 318,
+ "y": 440
+ },
+ {
+ "id": "turn",
+ "kind": "flow.branch",
+ "x": 750,
+ "y": 60
+ },
+ {
+ "id": "flip",
+ "kind": "call",
+ "ref": "math.multiply",
+ "x": 784,
+ "y": 250,
+ "literals": {
+ "b": -1
+ }
+ },
+ {
+ "id": "setDir",
+ "kind": "var.set",
+ "x": 1000,
+ "y": 60,
+ "literals": {
+ "name": "dir"
+ }
+ },
+ {
+ "id": "getDir_1",
+ "kind": "var.get",
+ "x": 568,
+ "y": 440,
+ "literals": {
+ "name": "dir"
+ }
+ },
+ {
+ "id": "getX_1",
+ "kind": "var.get",
+ "x": 284,
+ "y": 250,
+ "literals": {
+ "name": "x"
+ }
+ },
+ {
+ "id": "getX_2",
+ "kind": "var.get",
+ "x": 102,
+ "y": 630,
+ "literals": {
+ "name": "x"
+ }
+ }
],
"edges": [
- { "id": "e1", "from": "update", "fromPort": "then", "to": "setX", "toPort": "" },
- { "id": "e2", "from": "getDir", "fromPort": "value", "to": "speed", "toPort": "a" },
- { "id": "e3", "from": "speed", "fromPort": "result", "to": "step", "toPort": "a" },
- { "id": "e4", "from": "update", "fromPort": "dt", "to": "step", "toPort": "b" },
- { "id": "e5", "from": "getX", "fromPort": "value", "to": "moved", "toPort": "a" },
- { "id": "e6", "from": "step", "fromPort": "result", "to": "moved", "toPort": "b" },
- { "id": "e7", "from": "moved", "fromPort": "result", "to": "setX", "toPort": "value" },
- { "id": "e8", "from": "setX", "fromPort": "then", "to": "write", "toPort": "" },
- { "id": "e9", "from": "getX", "fromPort": "value", "to": "write", "toPort": "value" },
- { "id": "e10", "from": "write", "fromPort": "then", "to": "turn", "toPort": "" },
- { "id": "e11", "from": "getX", "fromPort": "value", "to": "absX", "toPort": "a" },
- { "id": "e12", "from": "absX", "fromPort": "result", "to": "past", "toPort": "a" },
- { "id": "e13", "from": "past", "fromPort": "result", "to": "turn", "toPort": "cond" },
- { "id": "e14", "from": "turn", "fromPort": "true", "to": "setDir", "toPort": "" },
- { "id": "e15", "from": "getDir", "fromPort": "value", "to": "flip", "toPort": "a" },
- { "id": "e16", "from": "flip", "fromPort": "result", "to": "setDir", "toPort": "value" }
+ {
+ "id": "e1",
+ "from": "update",
+ "fromPort": "then",
+ "to": "setX",
+ "toPort": ""
+ },
+ {
+ "id": "e2",
+ "from": "getDir",
+ "fromPort": "value",
+ "to": "speed",
+ "toPort": "a"
+ },
+ {
+ "id": "e3",
+ "from": "speed",
+ "fromPort": "result",
+ "to": "step",
+ "toPort": "a"
+ },
+ {
+ "id": "e4",
+ "from": "update",
+ "fromPort": "dt",
+ "to": "step",
+ "toPort": "b"
+ },
+ {
+ "id": "e5",
+ "from": "getX",
+ "fromPort": "value",
+ "to": "moved",
+ "toPort": "a"
+ },
+ {
+ "id": "e6",
+ "from": "step",
+ "fromPort": "result",
+ "to": "moved",
+ "toPort": "b"
+ },
+ {
+ "id": "e7",
+ "from": "moved",
+ "fromPort": "result",
+ "to": "setX",
+ "toPort": "value"
+ },
+ {
+ "id": "e8",
+ "from": "setX",
+ "fromPort": "then",
+ "to": "write",
+ "toPort": ""
+ },
+ {
+ "id": "getX_1.value->write.value",
+ "from": "getX_1",
+ "fromPort": "value",
+ "to": "write",
+ "toPort": "value"
+ },
+ {
+ "id": "e10",
+ "from": "write",
+ "fromPort": "then",
+ "to": "turn",
+ "toPort": ""
+ },
+ {
+ "id": "getX_2.value->absX.a",
+ "from": "getX_2",
+ "fromPort": "value",
+ "to": "absX",
+ "toPort": "a"
+ },
+ {
+ "id": "e12",
+ "from": "absX",
+ "fromPort": "result",
+ "to": "past",
+ "toPort": "a"
+ },
+ {
+ "id": "e13",
+ "from": "past",
+ "fromPort": "result",
+ "to": "turn",
+ "toPort": "cond"
+ },
+ {
+ "id": "e14",
+ "from": "turn",
+ "fromPort": "true",
+ "to": "setDir",
+ "toPort": ""
+ },
+ {
+ "id": "getDir_1.value->flip.a",
+ "from": "getDir_1",
+ "fromPort": "value",
+ "to": "flip",
+ "toPort": "a"
+ },
+ {
+ "id": "e16",
+ "from": "flip",
+ "fromPort": "result",
+ "to": "setDir",
+ "toPort": "value"
+ }
]
-}
+}
\ No newline at end of file
diff --git a/examples/script-graph-demo/assets/logic/wave.esgraph b/examples/script-graph-demo/assets/logic/wave.esgraph
index f57f2423b..ce83ca012 100644
--- a/examples/script-graph-demo/assets/logic/wave.esgraph
+++ b/examples/script-graph-demo/assets/logic/wave.esgraph
@@ -12,14 +12,14 @@
{
"id": "update",
"kind": "event.update",
- "x": 40,
+ "x": 0,
"y": 60
},
{
"id": "getT",
"kind": "var.get",
- "x": 40,
- "y": 210,
+ "x": -182,
+ "y": 440,
"literals": {
"name": "elapsed"
}
@@ -28,8 +28,8 @@
"id": "rate",
"kind": "call",
"ref": "math.multiply",
- "x": 240,
- "y": 330,
+ "x": -182,
+ "y": 630,
"literals": {
"b": 110
}
@@ -38,13 +38,13 @@
"id": "advance",
"kind": "call",
"ref": "math.add",
- "x": 240,
- "y": 210
+ "x": 34,
+ "y": 250
},
{
"id": "setT",
"kind": "var.set",
- "x": 430,
+ "x": 250,
"y": 60,
"literals": {
"name": "elapsed"
@@ -54,8 +54,8 @@
"id": "wave",
"kind": "call",
"ref": "demo.pingPong",
- "x": 430,
- "y": 260,
+ "x": 68,
+ "y": 440,
"literals": {
"range": 140
}
@@ -64,8 +64,8 @@
"id": "lift",
"kind": "call",
"ref": "math.add",
- "x": 660,
- "y": 260,
+ "x": 284,
+ "y": 250,
"literals": {
"b": -70
}
@@ -74,11 +74,20 @@
"id": "write",
"kind": "call",
"ref": "property.set",
- "x": 660,
+ "x": 500,
"y": 60,
"literals": {
"path": "Transform.position.y"
}
+ },
+ {
+ "id": "getT_1",
+ "kind": "var.get",
+ "x": -148,
+ "y": 630,
+ "literals": {
+ "name": "elapsed"
+ }
}
],
"edges": [
@@ -111,8 +120,8 @@
"toPort": "value"
},
{
- "id": "e5",
- "from": "getT",
+ "id": "getT_1.value->wave.t",
+ "from": "getT_1",
"fromPort": "value",
"to": "wave",
"toPort": "t"
diff --git a/examples/script-graph-demo/src/components.ts b/examples/script-graph-demo/src/components.ts
new file mode 100644
index 000000000..de3b01b11
--- /dev/null
+++ b/examples/script-graph-demo/src/components.ts
@@ -0,0 +1,42 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
+/**
+ * @file components.ts
+ * @brief One registered value node — the half of a script graph that is code.
+ *
+ * A graph is structure; the verbs it calls are names. The engine ships a
+ * vocabulary (maths, comparison, `property.get`/`set`, …) and a project extends
+ * it through the same door: `registerValue` for a name that answers a question,
+ * `registerAction` for one that does something. Either shows up in the editor's
+ * palette with the pins it declares, so nothing about a built-in node is
+ * privileged.
+ *
+ * It lives HERE, in the declaration module, and not in `main.ts`: the editor
+ * imports this file to learn what the project declares, and never the startup
+ * one (importing that would run the game). A name registered in `main.ts` works
+ * at runtime and is a node the editor cannot draw.
+ */
+import { registerValue } from 'esengine';
+
+/**
+ * A triangle wave: `t` folded into `0 → range → 0`. The engine has no such node
+ * and should not — this is the demo's own idea of motion, and one function is
+ * the whole of teaching the graph about it.
+ */
+registerValue('demo.pingPong', {
+ params: [
+ { name: 't', type: 'number', tooltip: 'Seconds elapsed' },
+ { name: 'range', type: 'number', tooltip: 'Peak value' },
+ ],
+ outputs: [{ name: 'value', type: 'number' }],
+ // Pure: it reads nothing and writes nothing, so the graph may pull it
+ // whenever the node reading it runs.
+ touches: {},
+ evaluate: (_ctx, _bb, params, out) => {
+ const range = Number(params.range ?? 0);
+ if (range <= 0) { out.value = 0; return; }
+ const span = range * 2;
+ const wrapped = Math.abs(Number(params.t ?? 0)) % span;
+ out.value = wrapped <= range ? wrapped : span - wrapped;
+ },
+});
diff --git a/examples/script-graph-demo/src/main.ts b/examples/script-graph-demo/src/main.ts
index 7262ed736..25606a466 100644
--- a/examples/script-graph-demo/src/main.ts
+++ b/examples/script-graph-demo/src/main.ts
@@ -2,36 +2,7 @@
// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
/**
* @file main.ts
- * @brief One registered value node — the half of a script graph that is code.
- *
- * A graph is structure; the verbs it calls are names. The engine ships a
- * vocabulary (maths, comparison, `property.get`/`set`, …) and a project extends
- * it through the same door: `registerValue` for a name that answers a question,
- * `registerAction` for one that does something. Either shows up in the editor's
- * palette with the pins it declares, so nothing about a built-in node is
- * privileged.
+ * @brief Startup. Everything this demo does is in the graphs — this only
+ * pulls in what the project declares.
*/
-import { registerValue } from 'esengine';
-
-/**
- * A triangle wave: `t` folded into `0 → range → 0`. The engine has no such node
- * and should not — this is the demo's own idea of motion, and one function is
- * the whole of teaching the graph about it.
- */
-registerValue('demo.pingPong', {
- params: [
- { name: 't', type: 'number', tooltip: 'Seconds elapsed' },
- { name: 'range', type: 'number', tooltip: 'Peak value' },
- ],
- outputs: [{ name: 'value', type: 'number' }],
- // Pure: it reads nothing and writes nothing, so the graph may pull it
- // whenever the node reading it runs.
- touches: {},
- evaluate: (_ctx, _bb, params, out) => {
- const range = Number(params.range ?? 0);
- if (range <= 0) { out.value = 0; return; }
- const span = range * 2;
- const wrapped = Math.abs(Number(params.t ?? 0)) % span;
- out.value = wrapped <= range ? wrapped : span - wrapped;
- },
-});
+import './components';
diff --git a/sdk/etc/index.api.md b/sdk/etc/index.api.md
index 3d696e9a0..ca6b9e8c4 100644
--- a/sdk/etc/index.api.md
+++ b/sdk/etc/index.api.md
@@ -210,6 +210,7 @@ dt: number
entity: number
get: <C extends AnyComponentDef>(component: C) => ComponentData<C>
has: (component: AnyComponentDef) => boolean
+input: InputState
set: <C extends AnyComponentDef>(component: C, data: ComponentData<C>) => void
world: World
```
@@ -12344,7 +12345,7 @@ EventBindingPlugin
## fireScriptGraphEvent — function @experimental
```
-<Ctx>(compiled: CompiledScriptGraph, run: ScriptRunState, tick: ScriptTickContext<Ctx>, type: string, target: number): void
+<Ctx>(compiled: CompiledScriptGraph, run: ScriptRunState, tick: ScriptTickContext<Ctx>, type: string, target: number, other?: number): void
```
## flattenPrefab — function @experimental
@@ -14299,7 +14300,7 @@ StatsPlugin
## stepScriptGraphs — function @experimental
```
-(world: ScriptGraphWorldView, commands: CommandsInstance, dt: number, states: Map<Entity, AgentState>, resolveGraph?: (ref: string) => CompiledScriptGraph | undefined, onProblem?: (entity: Entity, message: string) => void): void
+(world: ScriptGraphWorldView, commands: CommandsInstance, dt: number, states: Map<Entity, AgentState>, resolveGraph?: (ref: string) => CompiledScriptGraph | undefined, onProblem?: (entity: Entity, message: string) => void, input?: InputState): void
```
## stopNavAgent — function @experimental
diff --git a/sdk/etc/index.native.api.md b/sdk/etc/index.native.api.md
index 8c125349e..51e99b610 100644
--- a/sdk/etc/index.native.api.md
+++ b/sdk/etc/index.native.api.md
@@ -210,6 +210,7 @@ dt: number
entity: number
get: <C extends AnyComponentDef>(component: C) => ComponentData<C>
has: (component: AnyComponentDef) => boolean
+input: InputState
set: <C extends AnyComponentDef>(component: C, data: ComponentData<C>) => void
world: World
```
@@ -12577,7 +12578,7 @@ EventBindingPlugin
## fireScriptGraphEvent — function @experimental
```
-<Ctx>(compiled: CompiledScriptGraph, run: ScriptRunState, tick: ScriptTickContext<Ctx>, type: string, target: number): void
+<Ctx>(compiled: CompiledScriptGraph, run: ScriptRunState, tick: ScriptTickContext<Ctx>, type: string, target: number, other?: number): void
```
## flattenPrefab — function @experimental
@@ -14542,7 +14543,7 @@ StatsPlugin
## stepScriptGraphs — function @experimental
```
-(world: ScriptGraphWorldView, commands: CommandsInstance, dt: number, states: Map<Entity, AgentState>, resolveGraph?: (ref: string) => CompiledScriptGraph | undefined, onProblem?: (entity: Entity, message: string) => void): void
+(world: ScriptGraphWorldView, commands: CommandsInstance, dt: number, states: Map<Entity, AgentState>, resolveGraph?: (ref: string) => CompiledScriptGraph | undefined, onProblem?: (entity: Entity, message: string) => void, input?: InputState): void
```
## stopNavAgent — function @experimental
diff --git a/sdk/etc/index.node.api.md b/sdk/etc/index.node.api.md
index 2135ab6be..9dbc8d99f 100644
--- a/sdk/etc/index.node.api.md
+++ b/sdk/etc/index.node.api.md
@@ -205,6 +205,7 @@ dt: number
entity: number
get: <C extends AnyComponentDef>(component: C) => ComponentData<C>
has: (component: AnyComponentDef) => boolean
+input: InputState
set: <C extends AnyComponentDef>(component: C, data: ComponentData<C>) => void
world: World
```
@@ -12339,7 +12340,7 @@ EventBindingPlugin
## fireScriptGraphEvent — function @experimental
```
-<Ctx>(compiled: CompiledScriptGraph, run: ScriptRunState, tick: ScriptTickContext<Ctx>, type: string, target: number): void
+<Ctx>(compiled: CompiledScriptGraph, run: ScriptRunState, tick: ScriptTickContext<Ctx>, type: string, target: number, other?: number): void
```
## flattenPrefab — function @experimental
@@ -14304,7 +14305,7 @@ StatsPlugin
## stepScriptGraphs — function @experimental
```
-(world: ScriptGraphWorldView, commands: CommandsInstance, dt: number, states: Map<Entity, AgentState>, resolveGraph?: (ref: string) => CompiledScriptGraph | undefined, onProblem?: (entity: Entity, message: string) => void): void
+(world: ScriptGraphWorldView, commands: CommandsInstance, dt: number, states: Map<Entity, AgentState>, resolveGraph?: (ref: string) => CompiledScriptGraph | undefined, onProblem?: (entity: Entity, message: string) => void, input?: InputState): void
```
## stopNavAgent — function @experimental
diff --git a/sdk/etc/minigame.api.md b/sdk/etc/minigame.api.md
index f33c37a9b..189df7f84 100644
--- a/sdk/etc/minigame.api.md
+++ b/sdk/etc/minigame.api.md
@@ -205,6 +205,7 @@ dt: number
entity: number
get: <C extends AnyComponentDef>(component: C) => ComponentData<C>
has: (component: AnyComponentDef) => boolean
+input: InputState
set: <C extends AnyComponentDef>(component: C, data: ComponentData<C>) => void
world: World
```
@@ -12598,7 +12599,7 @@ EventBindingPlugin
## fireScriptGraphEvent — function @experimental
```
-<Ctx>(compiled: CompiledScriptGraph, run: ScriptRunState, tick: ScriptTickContext<Ctx>, type: string, target: number): void
+<Ctx>(compiled: CompiledScriptGraph, run: ScriptRunState, tick: ScriptTickContext<Ctx>, type: string, target: number, other?: number): void
```
## flattenPrefab — function @experimental
@@ -14563,7 +14564,7 @@ StatsPlugin
## stepScriptGraphs — function @experimental
```
-(world: ScriptGraphWorldView, commands: CommandsInstance, dt: number, states: Map<Entity, AgentState>, resolveGraph?: (ref: string) => CompiledScriptGraph | undefined, onProblem?: (entity: Entity, message: string) => void): void
+(world: ScriptGraphWorldView, commands: CommandsInstance, dt: number, states: Map<Entity, AgentState>, resolveGraph?: (ref: string) => CompiledScriptGraph | undefined, onProblem?: (entity: Entity, message: string) => void, input?: InputState): void
```
## stopNavAgent — function @experimental
diff --git a/sdk/etc/wechat.api.md b/sdk/etc/wechat.api.md
index b93239b57..4dceb70ab 100644
--- a/sdk/etc/wechat.api.md
+++ b/sdk/etc/wechat.api.md
@@ -205,6 +205,7 @@ dt: number
entity: number
get: <C extends AnyComponentDef>(component: C) => ComponentData<C>
has: (component: AnyComponentDef) => boolean
+input: InputState
set: <C extends AnyComponentDef>(component: C, data: ComponentData<C>) => void
world: World
```
@@ -12367,7 +12368,7 @@ EventBindingPlugin
## fireScriptGraphEvent — function @experimental
```
-<Ctx>(compiled: CompiledScriptGraph, run: ScriptRunState, tick: ScriptTickContext<Ctx>, type: string, target: number): void
+<Ctx>(compiled: CompiledScriptGraph, run: ScriptRunState, tick: ScriptTickContext<Ctx>, type: string, target: number, other?: number): void
```
## flattenPrefab — function @experimental
@@ -14327,7 +14328,7 @@ StatsPlugin
## stepScriptGraphs — function @experimental
```
-(world: ScriptGraphWorldView, commands: CommandsInstance, dt: number, states: Map<Entity, AgentState>, resolveGraph?: (ref: string) => CompiledScriptGraph | undefined, onProblem?: (entity: Entity, message: string) => void): void
+(world: ScriptGraphWorldView, commands: CommandsInstance, dt: number, states: Map<Entity, AgentState>, resolveGraph?: (ref: string) => CompiledScriptGraph | undefined, onProblem?: (entity: Entity, message: string) => void, input?: InputState): void
```
## stopNavAgent — function @experimental
diff --git a/sdk/src/ai/bt/BtPlugin.ts b/sdk/src/ai/bt/BtPlugin.ts
index e7260f68e..56aae9ae9 100644
--- a/sdk/src/ai/bt/BtPlugin.ts
+++ b/sdk/src/ai/bt/BtPlugin.ts
@@ -21,7 +21,8 @@ import { Commands, type CommandsInstance } from '../../ecs/commands';
import { playModeOnly } from '../../ecs/env';
import type { AnyComponentDef, ComponentData } from '../../ecs/component';
import { Blackboard } from '../fsm/Blackboard';
-import { aiRegistry, type AiContext } from '../fsm/AiContext';
+import { aiRegistry, noInput, type AiContext } from '../fsm/AiContext';
+import { Input, type InputState } from '../../input/input';
import { tickBt, createBtRunState, type BtRunState } from './BtRunner';
import { BehaviorTreeAgent, getBt, allBts } from './BehaviorTreeAgent';
import { appRegistryAsset, appRegistryAssets } from '../../asset/registryLookup';
@@ -66,6 +67,7 @@ export function stepBehaviorTrees(
// authored ref, so resolve before lookup (falls back to the raw ref for
// `registerBt` code names). Optional so tests need no realm.
resolveTree?: (ref: string) => BtDefinition | undefined,
+ input: InputState = noInput(),
): void {
if (dt <= 0) return;
@@ -73,6 +75,7 @@ export function stepBehaviorTrees(
entity: 0 as Entity,
dt,
blackboard: null as unknown as Blackboard,
+ input,
world: world as unknown as World,
commands,
get: c => world.get(ctx.entity, c),
@@ -160,9 +163,9 @@ export class BtPlugin implements Plugin {
app.addSystemToSchedule(
Schedule.Update,
defineSystem(
- [Res(Time), Commands(), GetWorld()],
- (time: TimeData, commands: CommandsInstance, world) => {
- stepBehaviorTrees(world as AiWorldView, commands, time.delta, states, resolveTree);
+ [Res(Time), Res(Input), Commands(), GetWorld()],
+ (time: TimeData, input: InputState, commands: CommandsInstance, world) => {
+ stepBehaviorTrees(world as AiWorldView, commands, time.delta, states, resolveTree, input);
},
// See fsmTouches: the reach is the union over the loaded trees,
// which is why it is asked for rather than stated.
diff --git a/sdk/src/ai/builtins.ts b/sdk/src/ai/builtins.ts
index f145ea576..422642dc4 100644
--- a/sdk/src/ai/builtins.ts
+++ b/sdk/src/ai/builtins.ts
@@ -18,6 +18,7 @@ import type { AiAction, AiActionInput, AiCondition, AiParamValue, AiTouches } fr
import { TimelinePlayer } from '../timeline/TimelinePlayerComponent';
import { SpriteAnimator } from '../animation/SpriteAnimator';
import { setEntityProperty } from '../ecs/propertyPath';
+import type { Entity } from '../types';
const TIMELINE: AiTouches = { reads: [TimelinePlayer._name], writes: [TimelinePlayer._name] };
const SPRITE_ANIM: AiTouches = { reads: [SpriteAnimator._name], writes: [SpriteAnimator._name] };
@@ -120,12 +121,17 @@ export function ensureBuiltinAiRegistrations(): void {
params: [
{ name: 'path', type: 'string', tooltip: 'Component.field, e.g. UIVisual.color.a' },
{ name: 'value', type: 'string' },
+ // Optional third, so a game can write the entity it just found.
+ // Trailing and empty by default, so a reference authored before
+ // it reads and writes back byte-identically.
+ { name: 'entity', type: 'number', tooltip: 'Leave empty for this entity' },
],
run: (ctx, _bb, _arg, params) => {
const path = typeof params?.path === 'string' ? params.path.trim() : '';
const raw = params?.value;
if (!path || raw === undefined) return;
- setEntityProperty(ctx.world, ctx.entity, path, parseValue(raw));
+ const target = Number(params?.entity ?? 0) || (ctx.entity as number);
+ setEntityProperty(ctx.world, target as Entity, path, parseValue(raw));
},
// The component it writes is the first segment of the authored path,
// so the reach is unknowable when this registers and plain in the
diff --git a/sdk/src/ai/fsm/AiContext.ts b/sdk/src/ai/fsm/AiContext.ts
index e55f2272a..33804897f 100644
--- a/sdk/src/ai/fsm/AiContext.ts
+++ b/sdk/src/ai/fsm/AiContext.ts
@@ -5,10 +5,14 @@
* @brief The concrete leaf context + the shared action/condition registry.
*
* AiContext is a BehaviorContext superset (adds the blackboard): writing an FSM
- * action or a BT leaf is the same programming model as a `defineBehavior.update`
- * — same entity/world/commands access, plus `blackboard` for AI data flow. This
- * is the engine layer, so it may reference World/Commands; the FSM/BT cores stay
- * generic and wasm-free.
+ * action, a BT leaf or a script-graph node is the same programming model as a
+ * `defineBehavior.update` — same entity/world/commands/input/time access, plus
+ * `blackboard` for AI data flow. This is the engine layer, so it may reference
+ * World/Commands; the FSM/BT cores stay generic and wasm-free.
+ *
+ * `input` arrives through {@link noInput} when a caller has none — a test, or a
+ * host with no devices. Nothing pressed is the truthful answer there, and it
+ * keeps the context's shape from being conditional.
*/
import type { Entity } from '../../types';
@@ -17,12 +21,16 @@ import type { CommandsInstance } from '../../ecs/commands';
import type { AnyComponentDef, ComponentData } from '../../ecs/component';
import type { Blackboard } from './Blackboard';
import { AiRegistry, type AiAction, type AiActionSpec, type AiCondition, type AiValueSpec } from './registry';
+import { InputState } from '../../input/input';
export interface AiContext {
/** The agent entity this action/condition runs for. */
readonly entity: Entity;
/** Frame delta in seconds. */
readonly dt: number;
+ /** Runtime input — the same state `defineBehavior` reads. A leaf that cannot
+ * see the keyboard is a leaf no game logic can be written in. */
+ readonly input: InputState;
/** This agent's blackboard — the AI data plane. */
readonly blackboard: Blackboard;
/** The world, for cross-entity access. */
@@ -44,6 +52,10 @@ export interface AiContext {
*/
export const aiRegistry = new AiRegistry<AiContext>();
+/** An input state with nothing pressed — what a caller with no devices supplies. */
+export const noInput = (): InputState => EMPTY_INPUT;
+const EMPTY_INPUT = new InputState();
+
/**
* Register a named action referenced by FSM state hooks / BT leaves / event
* rows — as a bare function, or with declared parameters, which is what turns
diff --git a/sdk/src/ai/fsm/FsmPlugin.ts b/sdk/src/ai/fsm/FsmPlugin.ts
index d640fe08d..de44c49fd 100644
--- a/sdk/src/ai/fsm/FsmPlugin.ts
+++ b/sdk/src/ai/fsm/FsmPlugin.ts
@@ -16,13 +16,15 @@ import type { Entity } from '../../types';
import type { World } from '../../ecs/world';
import { defineSystem, Schedule, GetWorld, type SystemTouches } from '../../ecs/system';
import { Res, Time, type TimeData } from '../../ecs/resource';
+import { Input } from '../../input/input';
import { defineResource } from '../../ecs/resource';
import { Commands, type CommandsInstance } from '../../ecs/commands';
import { playModeOnly } from '../../ecs/env';
import type { AnyComponentDef, ComponentData } from '../../ecs/component';
import { Blackboard } from './Blackboard';
import { createFsmRunState, stepFsm, type CompiledFsm, type FsmRunState } from './FsmRunner';
-import { aiRegistry, type AiContext } from './AiContext';
+import { aiRegistry, noInput, type AiContext } from './AiContext';
+import type { InputState } from '../../input/input';
import { StateMachineAgent, getFsm, allFsms } from './StateMachineAgent';
import { appRegistryAsset, appRegistryAssets } from '../../asset/registryLookup';
import { actionRefName, actionRefArg, actionRefParams } from './types';
@@ -69,6 +71,7 @@ export function stepStateMachines(
// ref, so resolve before lookup. Falls back to the raw ref for `registerFsm`
// code names, which are keyed verbatim. Optional so tests need no realm.
resolveFsm?: (ref: string) => CompiledFsm | undefined,
+ input: InputState = noInput(),
): void {
if (dt <= 0) return;
@@ -76,6 +79,7 @@ export function stepStateMachines(
entity: 0 as Entity,
dt,
blackboard: null as unknown as Blackboard,
+ input,
world: world as unknown as World,
commands,
get: c => world.get(ctx.entity, c),
@@ -182,9 +186,9 @@ export class FsmPlugin implements Plugin {
app.addSystemToSchedule(
Schedule.Update,
defineSystem(
- [Res(Time), Commands(), GetWorld()],
- (time: TimeData, commands: CommandsInstance, world) => {
- stepStateMachines(world as FsmWorldView, commands, time.delta, states, resolveFsm);
+ [Res(Time), Res(Input), Commands(), GetWorld()],
+ (time: TimeData, input: InputState, commands: CommandsInstance, world) => {
+ stepStateMachines(world as FsmWorldView, commands, time.delta, states, resolveFsm, input);
},
// Asked per analysis, not once: a graph loaded later changes the
// answer, and no graph at all means this system touches only the
diff --git a/sdk/src/eventBinding.ts b/sdk/src/eventBinding.ts
index 0970b1080..7a653584e 100644
--- a/sdk/src/eventBinding.ts
+++ b/sdk/src/eventBinding.ts
@@ -36,7 +36,8 @@ import { playModeOnly } from './ecs/env';
import { log } from './util/logger';
import { ensureEntityEvents, EntityEventQueue, type EntityEvent, type Unsubscribe } from './ecs/entityEvents';
import { Blackboard } from './ai/fsm/Blackboard';
-import { aiRegistry, type AiContext } from './ai/fsm/AiContext';
+import { Input, type InputState } from './input/input';
+import { aiRegistry, noInput, type AiContext } from './ai/fsm/AiContext';
import { invokeAction, type AiParamValue } from './ai/fsm/registry';
import { AiFsm } from './ai/fsm/FsmPlugin';
import { ensureBuiltinAiRegistrations } from './ai/builtins';
@@ -139,6 +140,9 @@ export interface EventBindingHost {
commands(): CommandsInstance | null;
/** Current frame delta, for actions that care. */
dt(): number;
+ /** Runtime input, for the leaves that read it. A host with no devices — a
+ * test, a server — omits it and the leaves read nothing pressed. */
+ input?(): InputState;
}
export interface EventBindingRuntime {
@@ -176,6 +180,7 @@ export function createEventBindingRuntime(host: EventBindingHost): EventBindingR
entity: target,
dt: host.dt(),
blackboard: bb,
+ input: host.input?.() ?? noInput(),
world,
commands: host.commands() as CommandsInstance,
get: (c) => world.get(target, c),
@@ -266,6 +271,7 @@ export class EventBindingPlugin implements Plugin {
let commands_: CommandsInstance | null = null;
let dt_ = 0;
+ let input_: InputState = noInput();
const runtime = createEventBindingRuntime({
world,
@@ -279,15 +285,17 @@ export class EventBindingPlugin implements Plugin {
},
commands: () => commands_,
dt: () => dt_,
+ input: () => input_,
});
this.runtime_ = runtime;
app.addSystemToSchedule(
Schedule.PreUpdate,
defineSystem(
- [Res(Time), Commands()],
- (time: TimeData, commands: CommandsInstance) => {
+ [Res(Time), Res(Input), Commands()],
+ (time: TimeData, input: InputState, commands: CommandsInstance) => {
dt_ = time.delta;
+ input_ = input;
commands_ = commands;
runtime.sync();
},
diff --git a/sdk/src/logic/ScriptGraphPlugin.ts b/sdk/src/logic/ScriptGraphPlugin.ts
index 6dcaf69ea..ef3a2fc5b 100644
--- a/sdk/src/logic/ScriptGraphPlugin.ts
+++ b/sdk/src/logic/ScriptGraphPlugin.ts
@@ -21,7 +21,8 @@ import { playModeOnly } from '../ecs/env';
import type { AnyComponentDef, ComponentData } from '../ecs/component';
import { log } from '../util/logger';
import { Blackboard } from '../ai/fsm/Blackboard';
-import { aiRegistry, type AiContext } from '../ai/fsm/AiContext';
+import { aiRegistry, noInput, type AiContext } from '../ai/fsm/AiContext';
+import { Input, type InputState } from '../input/input';
import { ensureBuiltinAiRegistrations } from '../ai/builtins';
import { ensureBuiltinScriptNodes } from './builtinNodes';
import { TouchesBuilder, touchesOfLeaves, type LeafRef } from '../ai/worldView';
@@ -64,11 +65,12 @@ export function agentGraphBlackboard(states: Map<Entity, AgentState>, entity: En
return st.bb;
}
-function contextFor(world: ScriptGraphWorldView, commands: CommandsInstance): MutableAiContext {
+function contextFor(world: ScriptGraphWorldView, commands: CommandsInstance, input: InputState): MutableAiContext {
const ctx: MutableAiContext = {
entity: 0 as Entity,
dt: 0,
blackboard: null as unknown as Blackboard,
+ input,
world: world as unknown as World,
commands,
get: c => world.get(ctx.entity, c),
@@ -91,8 +93,9 @@ export function stepScriptGraphs(
states: Map<Entity, AgentState>,
resolveGraph?: (ref: string) => CompiledScriptGraph | undefined,
onProblem?: (entity: Entity, message: string) => void,
+ input: InputState = noInput(),
): void {
- const ctx = contextFor(world, commands);
+ const ctx = contextFor(world, commands, input);
const tick = (entity: Entity, st: AgentState, delta: number): ScriptTickContext<AiContext> => {
ctx.entity = entity;
ctx.dt = delta;
@@ -183,7 +186,10 @@ function createEventBridge(
const self = event.currentTarget;
const st = states.get(self);
if (!st?.compiled || !st.run) return;
- fireScriptGraphEvent(st.compiled, st.run, tickOf(self, st), event.type, event.target as number);
+ // The payload's `other` when it has one — physics names the contact's
+ // partner there, and that is the entity a game reacts to.
+ const other = Number((event.data as { other?: number } | undefined)?.other ?? 0);
+ fireScriptGraphEvent(st.compiled, st.run, tickOf(self, st), event.type, event.target as number, other);
};
return {
@@ -247,6 +253,7 @@ export class ScriptGraphPlugin implements Plugin {
const events = ensureEntityEvents(app);
let commands_: CommandsInstance | null = null;
let dt_ = 0;
+ let input_: InputState = noInput();
const onProblem = (entity: Entity, message: string): void => {
log.warn('logic', `ScriptGraphAgent on entity ${entity as number}: ${message}`);
@@ -254,12 +261,13 @@ export class ScriptGraphPlugin implements Plugin {
// An event arrives outside the system body, so the context it runs on is
// built from the frame's last known commands and delta.
- const ctx = contextFor(world, null as unknown as CommandsInstance);
+ const ctx = contextFor(world, null as unknown as CommandsInstance, noInput());
const bridge = createEventBridge(world, events, states, (entity, st) => {
ctx.entity = entity;
ctx.dt = dt_;
ctx.blackboard = st.bb;
ctx.commands = commands_ as CommandsInstance;
+ ctx.input = input_;
return {
ctx: ctx as AiContext, bb: st.bb, registry: aiRegistry, dt: dt_,
onProblem: message => onProblem(entity, message),
@@ -272,11 +280,12 @@ export class ScriptGraphPlugin implements Plugin {
app.addSystemToSchedule(
Schedule.Update,
defineSystem(
- [Res(Time), Commands(), GetWorld()],
- (time: TimeData, commands: CommandsInstance, w) => {
+ [Res(Time), Res(Input), Commands(), GetWorld()],
+ (time: TimeData, input: InputState, commands: CommandsInstance, w) => {
commands_ = commands;
dt_ = time.delta;
- stepScriptGraphs(w as ScriptGraphWorldView, commands, time.delta, states, resolveGraph, onProblem);
+ input_ = input;
+ stepScriptGraphs(w as ScriptGraphWorldView, commands, time.delta, states, resolveGraph, onProblem, input);
bridge.sync();
},
// Asked per analysis, not once: a graph loaded later changes the
diff --git a/sdk/src/logic/ScriptGraphRunner.ts b/sdk/src/logic/ScriptGraphRunner.ts
index ce35d0278..066b0eb68 100644
Binary files a/sdk/src/logic/ScriptGraphRunner.ts and b/sdk/src/logic/ScriptGraphRunner.ts differ
diff --git a/sdk/src/logic/builtinNodes.ts b/sdk/src/logic/builtinNodes.ts
index 6e32ae489..3c26523d6 100644
--- a/sdk/src/logic/builtinNodes.ts
+++ b/sdk/src/logic/builtinNodes.ts
@@ -18,7 +18,9 @@
import { aiRegistry, type AiContext } from '../ai/fsm/AiContext';
import type { AiOutputDef, AiOutputs, AiParamDef, AiParams, AiTouches } from '../ai/fsm/registry';
-import { getEntityProperty } from '../ecs/propertyPath';
+import { getEntityProperty, setEntityProperty } from '../ecs/propertyPath';
+import { Name } from '../ecs/component';
+import type { Entity } from '../types';
import { log } from '../util/logger';
const num = (v: unknown): number => (typeof v === 'number' ? v : Number(v) || 0);
@@ -88,15 +90,19 @@ export function ensureBuiltinScriptNodes(): void {
[{ name: 'a', type: 'string' }, { name: 'b', type: 'string' }], OUT_S,
(_c, p, out) => { out.result = str(p.a) + str(p.b); });
- // The read half of `property.set`, and the same addressing: "Component" plus
- // a dot path. Numbers come back as numbers, everything else as its string, so
- // one node serves every field without a type per node.
+ // The read half of `property.set`, and the same addressing. Numbers come
+ // back as numbers and everything else as its string, so one node serves
+ // every field; `entity` 0 is the one this graph rides on.
value('property.get',
- [{ name: 'path', type: 'string', tooltip: 'Component.field, e.g. Transform.position.x' }],
+ [
+ { name: 'path', type: 'string', tooltip: 'Component.field, e.g. Transform.position.x' },
+ { name: 'entity', type: 'number', tooltip: 'Leave 0 for this entity' },
+ ],
[{ name: 'value', type: 'number' }, { name: 'text', type: 'string' }],
(ctx, p, out) => {
const path = str(p.path).trim();
- const raw = path ? getEntityProperty(ctx.world, ctx.entity, path) : undefined;
+ const target = (Number(p.entity ?? 0) || (ctx.entity as number)) as Entity;
+ const raw = path ? getEntityProperty(ctx.world, target, path) : undefined;
out.value = typeof raw === 'number' ? raw : Number(raw) || 0;
out.text = str(raw);
},
@@ -108,6 +114,43 @@ export function ensureBuiltinScriptNodes(): void {
value('entity.self', [], [{ name: 'entity', type: 'entity' }],
(ctx, _p, out) => { out.entity = ctx.entity as number; });
+ // — Input. A leaf that cannot see the keyboard is a leaf no game logic can
+ // be written in; every authored surface reads the same InputState a
+ // `defineBehavior` does. —
+ value('input.down', [{ name: 'key', type: 'string', tooltip: 'KeyboardEvent.code, e.g. ArrowLeft' }], OUT_B,
+ (ctx, p, out) => { out.result = ctx.input.isKeyDown(str(p.key)); }, {});
+ value('input.pressed', [{ name: 'key', type: 'string' }], OUT_B,
+ (ctx, p, out) => { out.result = ctx.input.isKeyPressed(str(p.key)); }, {});
+ // The 1-D axis a character moves along: two keys, one number, so a graph
+ // spends one node on "left or right" instead of a branch per direction.
+ value('input.axis',
+ [{ name: 'negative', type: 'string' }, { name: 'positive', type: 'string' }], OUT_N,
+ (ctx, p, out) => {
+ out.result = (ctx.input.isKeyDown(str(p.positive)) ? 1 : 0)
+ - (ctx.input.isKeyDown(str(p.negative)) ? 1 : 0);
+ }, {});
+ value('input.pointer', [], [{ name: 'x', type: 'number' }, { name: 'y', type: 'number' }],
+ (ctx, _p, out) => {
+ const m = ctx.input.getMousePosition();
+ out.x = m.x; out.y = m.y;
+ }, {});
+
+ // — Other entities. A game is entities acting on each other, and a graph
+ // that can only reach the one it rides on cannot express that. —
+ value('entity.byName', [{ name: 'name', type: 'string' }], [{ name: 'entity', type: 'entity' }],
+ (ctx, p, out) => { out.entity = findByName(ctx, str(p.name)) as number; }, { reads: [Name._name] });
+
+ if (!aiRegistry.hasAction('entity.despawn')) {
+ aiRegistry.registerAction('entity.despawn', {
+ params: [{ name: 'entity', type: 'number', tooltip: 'Leave 0 for this entity' }],
+ touches: { opaque: true },
+ run: (ctx, _bb, _arg, params) => {
+ const target = Number(params?.entity ?? 0) || (ctx.entity as number);
+ ctx.commands.despawn(target as Entity);
+ },
+ });
+ }
+
// An effectful name, so it carries an exec pin and sequences like any other
// statement — a log line that ran "somewhere in the data" would be useless.
if (!aiRegistry.hasAction('debug.log')) {
@@ -118,3 +161,10 @@ export function ensureBuiltinScriptNodes(): void {
});
}
}
+
+/** The nearest entity carrying `name`, or 0. The world's own index — the same
+ * answer `findEntityByName` gives every other caller. */
+function findByName(ctx: AiContext, name: string): Entity {
+ if (!name) return 0 as Entity;
+ return (ctx.world.findEntityByName(name) ?? 0) as Entity;
+}
diff --git a/sdk/src/logic/nodes.ts b/sdk/src/logic/nodes.ts
index 3ecbd1eba..58791b427 100644
--- a/sdk/src/logic/nodes.ts
+++ b/sdk/src/logic/nodes.ts
@@ -103,7 +103,13 @@ export function describeNode(
case 'event.destroy':
return entry(NO_PORTS, 'destroy');
case 'event.on':
- return entry([{ name: 'target', type: 'entity' }], 'event');
+ // `other` is who ELSE the event named — the contact's partner on a
+ // physics event, 0 where the event names nobody. It is the only
+ // payload field a wire can carry, and the one a game acts on.
+ return entry([
+ { name: 'target', type: 'entity' },
+ { name: 'other', type: 'entity' },
+ ], 'event');
case 'flow.branch':
return {
diff --git a/sdk/tests/fsm-sprite-anim.test.ts b/sdk/tests/fsm-sprite-anim.test.ts
index 76bdd74a9..f2dc93c76 100644
--- a/sdk/tests/fsm-sprite-anim.test.ts
+++ b/sdk/tests/fsm-sprite-anim.test.ts
@@ -12,6 +12,7 @@ import { aiRegistry, type AiContext } from '../src/ai/fsm/AiContext';
import { ensureBuiltinAiRegistrations } from '../src/ai/builtins';
import { compileFsm, createFsmRunState, stepFsm } from '../src/ai/fsm/FsmRunner';
import { Blackboard } from '../src/ai/fsm/Blackboard';
+import { noInput } from '../src/ai/fsm/AiContext';
import { createBtRunState, tickBt } from '../src/ai/bt/BtRunner';
import type { BtDefinition } from '../src/ai/bt/types';
import type { FsmDefinition } from '../src/ai/fsm/types';
@@ -26,6 +27,7 @@ function makeCtx(components: Map<string, unknown>): AiContext {
entity: 1 as never,
dt: 1 / 60,
blackboard: new Blackboard(),
+ input: noInput(),
world: {} as never,
commands: {} as never,
get: (c: { _name: string }) => structuredClone(components.get(c._name)) as never,
diff --git a/sdk/tests/script-graph-corpus.test.ts b/sdk/tests/script-graph-corpus.test.ts
new file mode 100644
index 000000000..e362a7a38
--- /dev/null
+++ b/sdk/tests/script-graph-corpus.test.ts
@@ -0,0 +1,70 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
+/**
+ * @file script-graph-corpus.test.ts
+ * @brief Every `.esgraph` the repo ships compiles clean against the real
+ * vocabulary.
+ *
+ * A graph naming something nothing registered, or wiring a port that does not
+ * exist, still loads: the interpreter reports it and runs what is left. That is
+ * right at runtime and wrong for a file we ship — it means the demo an author
+ * opens quietly does less than it looks like it does. Compiling is cheap and the
+ * answer is exact, so the corpus is held to zero problems.
+ */
+import { describe, it, expect } from 'vitest';
+import { readdirSync, readFileSync, statSync } from 'node:fs';
+import path from 'node:path';
+import { fileURLToPath } from 'node:url';
+import { aiRegistry } from '../src/ai/fsm/AiContext';
+import { ensureBuiltinAiRegistrations } from '../src/ai/builtins';
+import { ensureBuiltinScriptNodes } from '../src/logic/builtinNodes';
+import { compileScriptGraph } from '../src/logic/ScriptGraphRunner';
+import type { ScriptGraph } from '../src/logic/types';
+
+const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..', '..');
+
+function graphsUnder(dir: string, out: string[] = []): string[] {
+ for (const entry of readdirSync(dir)) {
+ if (entry === 'node_modules' || entry === '.esengine' || entry.startsWith('.')) continue;
+ const full = path.join(dir, entry);
+ if (statSync(full).isDirectory()) graphsUnder(full, out);
+ else if (entry.endsWith('.esgraph')) out.push(full);
+ }
+ return out;
+}
+
+/**
+ * The names a PROJECT registers are not in this process — the extractor is what
+ * carries those to a reader that does not run game code. A `demo.`-prefixed ref
+ * is that case and is not a corpus fault.
+ */
+const PROJECT_PREFIX = /^demo\./;
+
+describe('the shipped script graphs', () => {
+ ensureBuiltinAiRegistrations();
+ ensureBuiltinScriptNodes();
+
+ const files = [
+ ...graphsUnder(path.join(ROOT, 'examples')),
+ ...graphsUnder(path.join(ROOT, 'templates')),
+ ];
+
+ it('exist at all — a corpus with none proves nothing', () => {
+ expect(files.length).toBeGreaterThan(0);
+ });
+
+ for (const file of files) {
+ const rel = path.relative(ROOT, file);
+ it(`${rel} compiles with nothing missing`, () => {
+ const graph = JSON.parse(readFileSync(file, 'utf8')) as ScriptGraph;
+ const compiled = compileScriptGraph(graph, aiRegistry);
+ const problems = compiled.problems.filter((p) => !PROJECT_PREFIX.test(p.split('"')[1] ?? ''));
+ expect(problems).toEqual([]);
+ // A graph nothing can start is a picture. Every one of these ships as
+ // behaviour, so every one owes an entry.
+ const entries = compiled.starts.length + compiled.updates.length
+ + compiled.destroys.length + compiled.events.size;
+ expect(entries, 'no entry node — nothing would ever run it').toBeGreaterThan(0);
+ });
+ }
+});
diff --git a/sdk/tests/script-graph-runner.test.ts b/sdk/tests/script-graph-runner.test.ts
index 71bd7a588..cb1ca93c1 100644
--- a/sdk/tests/script-graph-runner.test.ts
+++ b/sdk/tests/script-graph-runner.test.ts
@@ -66,7 +66,7 @@ interface Run {
problems: string[];
tick(dt?: number): void;
destroy(): void;
- fire(type: string, target?: number): void;
+ fire(type: string, target?: number, other?: number): void;
halted(): boolean;
}
@@ -85,7 +85,7 @@ function run(graph: ScriptGraph, reg: AiRegistry<TestCtx>, budget?: number): Run
problems,
tick: (dt = 1 / 60) => stepScriptGraph(compiled, state, tickCtx(dt)),
destroy: () => destroyScriptGraph(compiled, state, tickCtx(0)),
- fire: (type, target = 7) => fireScriptGraphEvent(compiled, state, tickCtx(0), type, target),
+ fire: (type, target = 7, other = 0) => fireScriptGraphEvent(compiled, state, tickCtx(0), type, target, other),
halted: () => state.halted,
};
}
@@ -233,6 +233,17 @@ describe('data is pulled', () => {
expect(runOnce(g, reg)).toEqual(['spawn', '41']);
});
+ it('publishes who else the event named, so a graph can act on them', () => {
+ const { reg } = registry();
+ const g = graphOf([
+ node('on', 'event.on', { literals: { type: 'trigger_enter' } }),
+ node('say', 'call', { ref: 'test.say' }),
+ ], [edge('on', 'then', 'say', ''), edge('on', 'other', 'say', 'text')]);
+ const r = run(g, reg);
+ r.fire('trigger_enter', 3, 99);
+ expect(r.log).toEqual(['99']);
+ });
+
it('publishes the event target on the entry that heard it', () => {
const { reg } = registry();
const g = graphOf([
diff --git a/tools/goldenProjects.mjs b/tools/goldenProjects.mjs
index 7948e007b..005ee47a6 100644
--- a/tools/goldenProjects.mjs
+++ b/tools/goldenProjects.mjs
@@ -677,7 +677,17 @@ export const GOLDEN = [
certifies: ['script-graph'],
targets: ['web', 'desktop'],
tier: 'nightly',
- interactGap: 'both squares are graph-driven on a clock; no input path yet',
+ interactGap: 'both squares are graph-driven on a clock; the INPUT path is dodge-graph\'s',
+ },
+ {
+ id: 'dodge-graph',
+ // A game, not a demonstration: it reads the keyboard, scores, ends and
+ // restarts, and every one of those is a graph. The capability is the same
+ // one script-graph-demo carries; what this adds is that it is playable.
+ certifies: ['script-graph'],
+ targets: ['web', 'desktop'],
+ tier: 'nightly',
+ interact: { keys: ['ArrowRight'], frames: 40 },
},
{
id: 'save-load',
diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjs
index 5865217f5..4bda90e39 100644
--- a/tools/releaseNotes.mjs
+++ b/tools/releaseNotes.mjs
@@ -28,6 +28,12 @@ export const NOTES_FLOOR = 'v0.65.0';
* the reader recognises anyway.
*/
export const NOTED = {
+ 'feat(logic): a graph can reach a keyboard, another entity, and who an event named':
+ { note: 'Gameplay you can draw.' },
+
+ 'feat(logic): wires that clear the nodes, nodes that say which one they are':
+ { note: 'Gameplay you can draw.' },
+
'feat(logic): gameplay you can draw, and a corpus that draws some':
{ note: 'Gameplay you can draw.' },