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(play): capture the running game's console into the editor Output Log

esengine9049d6ecd425
diff --git a/desktop/src/engine/PlayRealm.ts b/desktop/src/engine/PlayRealm.ts
index d4c829209..e3c726077 100644
--- a/desktop/src/engine/PlayRealm.ts
+++ b/desktop/src/engine/PlayRealm.ts
@@ -13,6 +13,7 @@
*/
import { createStore } from 'zustand/vanilla';
import type { SceneData, PhysicsPluginConfig, SubsystemStatus } from 'esengine';
+import { LogStore, type LogLevel } from '@/store/LogStore';
export interface PlayPayload {
sceneData: SceneData;
@@ -154,13 +155,21 @@ class PlayRealmImpl {
private onMessage = (e: MessageEvent): void => {
if (!this.iframe || e.source !== this.iframe.contentWindow) return;
- const data = e.data as { type?: string; message?: string; reqId?: number; data?: unknown } | null;
+ const data = e.data as
+ | { type?: string; message?: string; reqId?: number; data?: unknown; level?: LogLevel; line?: string }
+ | null;
if (!data?.type) return;
switch (data.type) {
case 'estella:play:hello':
// Realm mounted + listening — hand it the scene snapshot.
if (this.payload) this.post({ type: 'estella:play:init', ...this.payload });
break;
+ case 'estella:play:log':
+ // The running game's console/wasm output, forwarded from the realm iframe so
+ // it lands in the editor's Output Log (it runs in a separate JS realm, so the
+ // parent's console patch never sees it).
+ LogStore.push(data.level ?? 'info', 'Play', data.line ?? '');
+ break;
case 'estella:play:ready':
this.set({ ready: true });
break;
diff --git a/desktop/src/playHost.ts b/desktop/src/playHost.ts
index 95e390525..fcad8c376 100644
--- a/desktop/src/playHost.ts
+++ b/desktop/src/playHost.ts
@@ -106,6 +106,35 @@ let booted = false;
let reloadSeq = 0;
const post = (m: Record<string, unknown>) => parent.postMessage(m, '*');
+// Forward the running game's console output to the editor's Output Log. The realm is
+// a separate JS realm, so the editor's console patch never sees these — mirror each
+// call to the parent (keeping the original for the realm's own devtools). wasm
+// print/printErr and the SDK logger both route through console, so this captures the
+// whole game stream.
+const fmtArg = (a: unknown): string => {
+ if (typeof a === 'string') return a;
+ if (a instanceof Error) return a.stack ?? a.message;
+ try {
+ return typeof a === 'object' ? JSON.stringify(a) : String(a);
+ } catch {
+ return String(a);
+ }
+};
+(() => {
+ const levels = { log: 'info', info: 'info', debug: 'info', warn: 'warn', error: 'error' } as const;
+ for (const m of ['log', 'info', 'debug', 'warn', 'error'] as const) {
+ const orig = console[m].bind(console);
+ console[m] = (...args: unknown[]) => {
+ orig(...args);
+ try {
+ post({ type: 'estella:play:log', level: levels[m], line: args.map(fmtArg).join(' ') });
+ } catch {
+ // Parent unavailable (realm tearing down) — the original console still ran.
+ }
+ };
+ }
+})();
+
/** Create the wasm module + GL context ONCE; both persist across hot-reloads —
* re-instantiating wasm and re-creating GL is the expensive part a reload skips. */
async function ensureEngine(): Promise<void> {