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.

fix(editor): a new component reaches the running game, and a new system knows where input comes from

yhhdac345b0a25f
diff --git a/desktop/electron/scriptScaffold.ts b/desktop/electron/scriptScaffold.ts
index 604c8c4cb..75100e6e8 100644
--- a/desktop/electron/scriptScaffold.ts
+++ b/desktop/electron/scriptScaffold.ts
@@ -49,6 +49,59 @@ export interface ScaffoldScriptResult {
wiredInto?: string;
/** The line appended there — surfaced so the editor can say what it did. */
wiredLine?: string;
+ /**
+ * The SECOND wiring, when a component's declaration entry was not reachable
+ * from the startup entry and had to be pulled in there too. Absent in the
+ * ordinary case, where the startup entry already imports the declarations.
+ */
+ alsoWiredInto?: string;
+ alsoWiredLine?: string;
+}
+
+/** Relative import/export specifiers a module names, in source order. */
+function relativeSpecifiers(source: string): string[] {
+ const out: string[] = [];
+ // `import x from './a'`, `import './a'`, `export * from './a'`, `export { x } from './a'`.
+ const re = /(?:^|[\s;}])(?:import|export)\s*(?:[^'"]*?\sfrom\s*)?['"](\.[^'"]*)['"]/g;
+ for (let m = re.exec(source); m; m = re.exec(source)) out.push(m[1]);
+ return out;
+}
+
+/** The project-relative module a specifier resolves to, or null. */
+function resolveRel(fromRel: string, spec: string): string | null {
+ const base = path.posix.join(path.posix.dirname(fromRel.replace(/\\/g, '/')), spec);
+ return base.replace(/^\.\//, '');
+}
+
+/**
+ * Whether the module graph rooted at `fromRel` reaches `targetRel` — the same
+ * question the bundler answers, asked with a walk over relative specifiers.
+ *
+ * Shallow on purpose: it follows only the project's own relative imports (a
+ * component's declarations are never reached through a package), and a file it
+ * cannot read is a leaf rather than a failure.
+ */
+async function entryReaches(root: string, fromRel: string, targetRel: string): Promise<boolean> {
+ const want = targetRel.replace(/\\/g, '/').replace(/\.tsx?$/, '');
+ const seen = new Set<string>();
+ const queue = [fromRel.replace(/\\/g, '/')];
+ while (queue.length) {
+ const rel = queue.shift()!;
+ const stem = rel.replace(/\.tsx?$/, '');
+ if (seen.has(stem)) continue;
+ seen.add(stem);
+ if (stem === want) return true;
+ const candidates = [rel, `${stem}.ts`, `${stem}.tsx`, `${stem}/index.ts`, `${stem}/index.tsx`];
+ const abs = candidates.map((c) => path.join(root, c)).find((p) => existsSync(p) && !p.endsWith(path.sep));
+ if (!abs) continue;
+ let source: string;
+ try { source = await readFile(abs, 'utf8'); } catch { continue; }
+ for (const spec of relativeSpecifiers(source)) {
+ const next = resolveRel(rel, spec);
+ if (next) queue.push(next);
+ }
+ }
+ return false;
}
// =============================================================================
@@ -99,6 +152,25 @@ export const ${fn} = defineSystem(
{ name: '${name}System' },
);
+// Reading input: ask for it as a resource, alongside Time above.
+//
+// import { CameraView, Input, MouseButton } from 'esengine';
+// [Query(Mut(Transform)), Res(Input), Res(CameraView)]
+// (query, input, camera) => {
+// if (input.isMouseButtonPressed(MouseButton.Left)) {
+// // input.mouseX/mouseY are SCREEN pixels (canvas-relative, y down).
+// // Anything placed in the world needs them in world units:
+// const at = camera.getWorldMousePosition(); // or camera.screenToWorld(x, y)
+// }
+// }
+//
+// This is the door for world-space content — a board, a token, a character —
+// whether or not it is under a Canvas; UINode/Interactable only answers for UI.
+// Do NOT reach for document.querySelector('canvas') and addEventListener: that
+// works in a browser and nowhere else this project can ship to (a mini-game
+// runtime and a native build have no DOM at all), and it makes the game do its
+// own screen→world arithmetic that the camera already knows.
+
addSystemToSchedule(Schedule.Update, ${fn});
`;
}
@@ -147,6 +219,23 @@ export async function scaffoldScript(
await mkdir(path.dirname(abs), { recursive: true });
await writeFile(abs, source, 'utf8');
await appendWiring(root, entry, line);
+ // Reaching the DECLARATION entry is only half of a component being real. The
+ // editor reads that entry directly — so the component shows up in Add
+ // Component and the scene saves it — but the running game only has the
+ // modules the STARTUP entry pulls in. A project whose startup entry no longer
+ // imports its declarations (the template ships the line; an entry rewritten
+ // since may not) therefore authors a component the game has never heard of,
+ // and says so only as "Unknown component type" in the play log, once per
+ // entity, after the scene is already built on it. Close it here, where the
+ // component is being made: this tool's promise is a script that is live.
+ if (opts.kind === 'component' && !await entryReaches(root, opts.entries.main, opts.entries.register)) {
+ const bridge = scriptWiring('system', opts.entries, opts.entries.register);
+ await appendWiring(root, bridge.entry, bridge.line);
+ return {
+ ok: true, path: moduleRel, wiredInto: entry, wiredLine: line,
+ alsoWiredInto: bridge.entry, alsoWiredLine: bridge.line,
+ };
+ }
return { ok: true, path: moduleRel, wiredInto: entry, wiredLine: line };
} catch (err) {
return { ok: false, error: err instanceof Error ? err.message : String(err) };
diff --git a/desktop/tests/script-scaffold.test.ts b/desktop/tests/script-scaffold.test.ts
index f52359931..2a1e749e0 100644
--- a/desktop/tests/script-scaffold.test.ts
+++ b/desktop/tests/script-scaffold.test.ts
@@ -88,6 +88,36 @@ describe('scaffoldScript — component', () => {
expect(read('src/main.ts')).toBe("import './components';\n");
});
+ it('also pulls the declarations into the STARTUP entry when nothing else does', async () => {
+ // The editor reads the declaration entry directly, so the component appears
+ // in Add Component and the scene saves it — but the running game only has
+ // what the startup entry imports. An entry rewritten since the template
+ // shipped therefore authors a component the game has never heard of, and
+ // says so only as "Unknown component type" in the play log, once per entity.
+ writeFileSync(path.join(root, 'src/main.ts'), "console.log('my own entry');\n");
+ const res = await scaffoldScript(root, { kind: 'component', name: 'Patrol', dir: 'src', entries: ENTRIES });
+ expect(res.ok).toBe(true);
+ expect(res.wiredInto).toBe('src/components.ts');
+ expect(res.alsoWiredInto).toBe('src/main.ts');
+ expect(read('src/main.ts')).toContain("import './components';");
+ expect(read('src/components.ts')).toContain("export * from './Patrol';");
+ });
+
+ it('leaves the startup entry alone when it already reaches the declarations', async () => {
+ const res = await scaffoldScript(root, { kind: 'component', name: 'Patrol', dir: 'src', entries: ENTRIES });
+ expect(res.alsoWiredInto).toBeUndefined();
+ expect(read('src/main.ts')).toBe("import './components';\n");
+ });
+
+ it('follows the import graph, not just the entry\'s own first line', async () => {
+ // main → game → components is reached; the bridge must not be added again.
+ writeFileSync(path.join(root, 'src/main.ts'), "import './game';\n");
+ writeFileSync(path.join(root, 'src/game.ts'), "export * from './components';\n");
+ const res = await scaffoldScript(root, { kind: 'component', name: 'Patrol', dir: 'src', entries: ENTRIES });
+ expect(res.alsoWiredInto).toBeUndefined();
+ expect(read('src/main.ts')).toBe("import './game';\n");
+ });
+
it('redirects a create from an assets folder into the source root', async () => {
const res = await scaffoldScript(root, { kind: 'component', name: 'Health', dir: 'assets/art', entries: ENTRIES });
expect(res.path).toBe('src/Health.ts');
@@ -114,6 +144,20 @@ describe('scaffoldScript — system', () => {
// The declaration entry is not touched — behaviour is not a declaration.
expect(read('src/components.ts')).toBe('export {};\n');
});
+
+ it('names the door for reading input, so the DOM is not the obvious one', async () => {
+ // Left to the template as it was, a system that needs a click reaches for
+ // document.querySelector('canvas') — which works in a browser and nowhere
+ // else this project can ship to, and does its own screen→world arithmetic
+ // that the camera already knows.
+ await scaffoldScript(root, { kind: 'system', name: 'Pick', dir: 'src', entries: ENTRIES });
+ const src = read('src/Pick.ts');
+ expect(src).toContain('Res(Input)');
+ expect(src).toContain('CameraView');
+ expect(src).toMatch(/getWorldMousePosition|screenToWorld/);
+ expect(src).toMatch(/document\.querySelector/); // named, as the thing NOT to do
+ expect(src).toMatch(/no DOM|have no DOM/i);
+ });
});
describe('scaffoldScript — safety', () => {