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): live play-realm introspection/mutation bridge (PD1)

esengine77967105a933
diff --git a/desktop/src/engine/PlayRealm.ts b/desktop/src/engine/PlayRealm.ts
index 2a8acc6fd..9887514b9 100644
--- a/desktop/src/engine/PlayRealm.ts
+++ b/desktop/src/engine/PlayRealm.ts
@@ -88,13 +88,36 @@ class PlayRealmImpl {
this.post({ type: 'estella:play:setPaused', paused });
}
+ // — Live introspection bridge (UE5-PIE Details): query/mutate the running World —
+ private reqSeq = 0;
+ private readonly pending = new Map<number, (data: unknown) => void>();
+
+ /** A live SceneData snapshot of the running game's World (null if not ready). */
+ snapshot(): Promise<SceneData | null> {
+ if (!this.iframe?.contentWindow || !this.store.getState().ready) return Promise.resolve(null);
+ const reqId = ++this.reqSeq;
+ return new Promise((resolve) => {
+ const done = (data: unknown) => resolve((data as SceneData) ?? null);
+ this.pending.set(reqId, done);
+ this.post({ type: 'estella:play:query', kind: 'snapshot', reqId });
+ setTimeout(() => {
+ if (this.pending.delete(reqId)) resolve(null);
+ }, 2000);
+ });
+ }
+
+ /** Live-edit a field of a running entity (debug; reverts on Stop). */
+ setField(entityId: number, comp: string, key: string, value: unknown): void {
+ this.post({ type: 'estella:play:setField', entityId, comp, key, value });
+ }
+
private post(message: Record<string, unknown>): void {
this.iframe?.contentWindow?.postMessage(message, '*');
}
private onMessage = (e: MessageEvent): void => {
if (!this.iframe || e.source !== this.iframe.contentWindow) return;
- const data = e.data as { type?: string; message?: string } | null;
+ const data = e.data as { type?: string; message?: string; reqId?: number; data?: unknown } | null;
if (!data?.type) return;
switch (data.type) {
case 'estella:play:hello':
@@ -107,6 +130,14 @@ class PlayRealmImpl {
case 'estella:play:error':
this.set({ error: data.message ?? 'play realm error' });
break;
+ case 'estella:play:reply': {
+ const resolve = data.reqId != null ? this.pending.get(data.reqId) : undefined;
+ if (resolve && data.reqId != null) {
+ this.pending.delete(data.reqId);
+ resolve(data.data);
+ }
+ break;
+ }
}
};
}
diff --git a/desktop/src/playHost.ts b/desktop/src/playHost.ts
index dcef1a5ef..1fa946f24 100644
--- a/desktop/src/playHost.ts
+++ b/desktop/src/playHost.ts
@@ -12,7 +12,7 @@
* Everything is same-origin estella:// (host, sdk, bundle, wasm, assets),
* sidestepping the custom-scheme cross-fetch ban.
*/
-import { createWebApp, setEditorMode, setPlayMode, initPlayRealmRuntime } from 'esengine';
+import { createWebApp, setEditorMode, setPlayMode, initPlayRealmRuntime, serializeScene, getComponent } from 'esengine';
import type { App, ESEngineModule, SceneData } from 'esengine';
interface InitMessage {
@@ -97,11 +97,50 @@ async function boot(msg: InitMessage): Promise<void> {
}
}
+/** Apply a (possibly dotted, e.g. "position.x") key to a cloned component data. */
+function applyKey(target: Record<string, unknown>, key: string, value: unknown): void {
+ const parts = key.split('.');
+ let obj = target;
+ for (let i = 0; i < parts.length - 1; i++) {
+ const k = parts[i];
+ obj[k] = { ...(obj[k] as Record<string, unknown>) };
+ obj = obj[k] as Record<string, unknown>;
+ }
+ obj[parts[parts.length - 1]] = value;
+}
+
+/** Live-edit one field of one entity's component in the running World (debug). */
+function setField(entityId: number, comp: string, key: string, value: unknown): void {
+ const world = app?.world;
+ if (!world) return;
+ const def = getComponent(comp);
+ if (!def || !world.has(entityId, def)) return;
+ const data = { ...(world.get(entityId, def) as Record<string, unknown>) };
+ applyKey(data, key, value);
+ world.set(entityId, def, data as never);
+}
+
window.addEventListener('message', (e: MessageEvent) => {
- const data = e.data as { type?: string; paused?: boolean } | null;
+ const data = e.data as { type?: string; paused?: boolean; reqId?: number; kind?: string; entityId?: number; comp?: string; key?: string; value?: unknown } | null;
if (!data || typeof data !== 'object') return;
- if (data.type === 'estella:play:init') void boot(e.data as InitMessage);
- else if (data.type === 'estella:play:setPaused') app?.setPaused(!!data.paused);
+ switch (data.type) {
+ case 'estella:play:init':
+ void boot(e.data as InitMessage);
+ break;
+ case 'estella:play:setPaused':
+ app?.setPaused(!!data.paused);
+ break;
+ case 'estella:play:query':
+ // Live introspection for the editor's "Game" inspect mode (UE5 PIE Details).
+ if (data.kind === 'snapshot') {
+ const snapshot = app ? serializeScene(app.world) : null;
+ post({ type: 'estella:play:reply', reqId: data.reqId, data: snapshot });
+ }
+ break;
+ case 'estella:play:setField':
+ if (data.entityId != null && data.comp && data.key) setField(data.entityId, data.comp, data.key, data.value);
+ break;
+ }
});
post({ type: 'estella:play:hello' });