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(editor): the play probe can read a resource, not just components

esenginea140c8463d51
diff --git a/desktop/shared/toolCatalog.mjs b/desktop/shared/toolCatalog.mjs
index 5ed8c2945..06ab6801f 100644
--- a/desktop/shared/toolCatalog.mjs
+++ b/desktop/shared/toolCatalog.mjs
@@ -514,7 +514,7 @@ export const TOOLS = [
return { total: t.length, from, events: t.slice(from) };
})` },
{ name: 'play_probe', effect: 'irreversible',
- description: "Evaluate JS inside the RUNNING play realm and return the result — the gameplay probe. One expression gives its value; several statements need an explicit `return`. window.__estellaPlay = { app, getComponent, find, componentNames, input }: **`find(NAME)` is the one to reach for** — it returns every entity carrying that component with its live data, which is how you ask what the game currently thinks is going on; `componentNames()` lists what you may ask for. `getComponent(NAME)` gives the DEFINITION and `app.world.tryGet(entity, def)` one entity's data (`get` returns a ZEROED object for an entity that lacks the component, so it cannot be used to test for one); a write only lands through `app.world.insert(entity, def, data)` — assigning to a copy changes nothing. Use play_input, not synthetic DOM events, to drive input. frame picks the realm in multiplayer previews (0 = host).",
+ description: "Evaluate JS inside the RUNNING play realm and return the result — the gameplay probe. One expression gives its value; several statements need an explicit `return`. window.__estellaPlay = { app, getComponent, find, componentNames, resource, input }: **`find(NAME)` and `resource(NAME)` are the ones to reach for** — `find` returns every entity carrying that component with its live data, `resource` the live value of a resource (a score, a phase — state that belongs to no entity); together they are how you ask what the game currently thinks is going on. `componentNames()` lists what `find` accepts, and `resource` on an unknown name answers with the available ones. `getComponent(NAME)` gives the DEFINITION and `app.world.tryGet(entity, def)` one entity's data (`get` returns a ZEROED object for an entity that lacks the component, so it cannot be used to test for one); a write only lands through `app.world.insert(entity, def, data)` — assigning to a copy changes nothing. Use play_input, not synthetic DOM events, to drive input. frame picks the realm in multiplayer previews (0 = host).",
schema: obj({ code: { type: 'string' }, frame: { type: 'number' } }, ['code']),
op: 'play_probe' },
{ name: 'play_input', effect: 'irreversible',
diff --git a/desktop/src/playHost.ts b/desktop/src/playHost.ts
index a0da447db..27e2aac16 100644
--- a/desktop/src/playHost.ts
+++ b/desktop/src/playHost.ts
@@ -322,6 +322,29 @@ async function buildAppAndRun(msg: InitMessage): Promise<void> {
.flatMap((e) => app!.world.getComponentTypes(e as never))
.filter((t, i, a) => a.indexOf(t) === i)
.sort(),
+ /**
+ * A resource's live value by name — the other half of "what does the game
+ * think is going on", for state that belongs to no entity (a score, a life
+ * count, a phase).
+ *
+ * `app.getResource` takes the DEF, which a probe has no way to name, so the
+ * next move was reading `app.resources_.resources_` — a private map, reached
+ * by two different agents on two different days, which is how a probe surface
+ * turns into a dependency on internals.
+ */
+ resource: (name: string) => {
+ // Keyed by the def's symbol, whose description carries the name as
+ // `Resource_<n>_<Name>` — the only place the name survives at runtime.
+ const entries = [...(app as unknown as {
+ resources_: { resources_: Map<symbol, unknown> };
+ }).resources_.resources_];
+ const nameOf = (k: symbol): string => (/^Resource_\d+_(.+)$/.exec(k.description ?? '')?.[1] ?? k.description ?? '');
+ const hit = entries.find(([k]) => nameOf(k) === name);
+ if (!hit) {
+ return { error: `no resource named "${name}"`, available: entries.map(([k]) => nameOf(k)).filter(Boolean).sort() };
+ }
+ return hit[1];
+ },
input: {
move: (x: number, y: number) => injected.onPointerMove?.(x, y),
down: (x: number, y: number, button = 0) => injected.onPointerDown?.(button, x, y),