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 row picked in the running game is found in the tree

yhhaf365a7176be
diff --git a/desktop/src/outliner/liveTree.ts b/desktop/src/outliner/liveTree.ts
index 26d3f7002..8c1360240 100644
--- a/desktop/src/outliner/liveTree.ts
+++ b/desktop/src/outliner/liveTree.ts
@@ -6,9 +6,12 @@
import type { SceneData } from 'esengine';
import type { EntityId } from '@/types';
import type { LiveOrigin } from '@/engine/playProtocol';
-import { authoredRef, refOfLive, spawnedRef, type EntityRef } from '@/engine/entityRef';
+import { authoredRef, refKey, refOfLive, spawnedRef, type EntityRef } from '@/engine/entityRef';
+import { folderKey } from './OutlinerModel';
+import { folderPrefixes, normalizeFolder } from './folders';
-type LiveEntity = SceneData['entities'][number] & LiveOrigin;
+type Row = SceneData['entities'][number];
+type LiveEntity = Row & LiveOrigin;
export interface LiveTreeView {
/** The tree to build rows from; null until the realm has reported one. */
@@ -50,7 +53,6 @@ export function mergeLiveTree(live: SceneData | null, doc: SceneData | null): Li
const parentOf = (parent: EntityId | null | undefined): EntityId | null =>
parent == null ? null : srcToLive.get(parent) ?? (deadSrcs.has(parent) ? tombstoneId(parent) : null);
- type Row = SceneData['entities'][number];
// Placed where the document has it, behind the last authored row still running:
// the row you were reading must not jump when the game destroys it.
const lead: Row[] = [];
@@ -75,3 +77,29 @@ export function mergeLiveTree(live: SceneData | null, doc: SceneData | null): Li
gone: new Set([...deadSrcs].map(tombstoneId)),
};
}
+
+/**
+ * The keys to expand for `ref`'s row to be on screen: every ancestor it nests
+ * under, then its root's folder. The running world has its own parents and its
+ * own keys, so a reveal has to be asked of the tree that is showing.
+ */
+export function revealKeys(view: LiveTreeView, ref: EntityRef, folderOf: (id: EntityId) => string): string[] {
+ const rows = view.data?.entities;
+ if (!rows) return [];
+ const wanted = refKey(ref);
+ const found = rows.find((e) => refKey(view.refOf(e.id)) === wanted);
+ if (!found) return [];
+ let node: Row = found;
+ const byId = new Map(rows.map((e) => [e.id, e]));
+ const out: string[] = [];
+ const seen = new Set<EntityId>([node.id]);
+ for (;;) {
+ const parent = node.parent == null ? undefined : byId.get(node.parent);
+ if (!parent || seen.has(parent.id)) break;
+ seen.add(parent.id);
+ out.push(refKey(view.refOf(parent.id)));
+ node = parent;
+ }
+ for (const pre of folderPrefixes(normalizeFolder(folderOf(node.id)))) out.push(folderKey(pre));
+ return out;
+}
diff --git a/desktop/src/panels/Outliner.tsx b/desktop/src/panels/Outliner.tsx
index 53773322c..3542c0c4e 100644
--- a/desktop/src/panels/Outliner.tsx
+++ b/desktop/src/panels/Outliner.tsx
@@ -19,7 +19,7 @@ import { contributedContextRows } from '@/plugins/contextMenus';
import { VirtualTree } from '@/components/VirtualTree';
import { t } from '@/i18n';
import { buildOutlinerItems, collectExpandableKeys, entityKey, folderKey, parseQuery, type OutlinerItem, type SortMode } from '@/outliner/OutlinerModel';
-import { mergeLiveTree } from '@/outliner/liveTree';
+import { mergeLiveTree, revealKeys } from '@/outliner/liveTree';
import { useOutliner } from '@/outliner/OutlinerController';
import { OutlinerRow } from '@/outliner/OutlinerRow';
import { useAgent, touchedEntities } from '@/store/AgentStore';
@@ -249,9 +249,13 @@ export function Outliner() {
scrollToIndex(idx);
} else if (expandedSel.current !== selKey) {
expandedSel.current = selKey; // attempt expansion once (avoids a loop when filtered out)
- if (selectedId != null) useOutliner.getState().revealEntity(selectedId);
+ // Whichever tree is showing answers where the row hangs: a spawned one has
+ // no document row to climb, and its ancestors are keyed by realm handle.
+ if (live && selectedRef) useOutliner.getState().expand(revealKeys(liveView, selectedRef, liveFolderOf));
+ else if (selectedId != null) useOutliner.getState().revealEntity(selectedId);
}
- }, [selKey, selectedId, items]);
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ }, [selKey, selectedId, items, live, liveView]);
// — Keyboard navigation (↑↓ move · ←→ collapse/expand/jump · Enter toggle · F2/Del) —
const cursorItem = (): OutlinerItem | null => items.find((i) => i.key === cursor) ?? null;
diff --git a/desktop/tests/live-tree-identity.test.ts b/desktop/tests/live-tree-identity.test.ts
index 1896543ba..a6547f48b 100644
--- a/desktop/tests/live-tree-identity.test.ts
+++ b/desktop/tests/live-tree-identity.test.ts
@@ -12,8 +12,8 @@
import { describe, it, expect, beforeEach } from 'vitest';
import type { SceneData } from 'esengine';
import { authoredRef, spawnedRef, refKey, refOfLive, sameRef, srcIdOf } from '@/engine/entityRef';
-import { buildOutlinerItems, entityKey } from '@/outliner/OutlinerModel';
-import { mergeLiveTree } from '@/outliner/liveTree';
+import { buildOutlinerItems, entityKey, folderKey } from '@/outliner/OutlinerModel';
+import { mergeLiveTree, revealKeys } from '@/outliner/liveTree';
import { useSelection } from '@/store/selectionStore';
/** A live tree as the realm reports it: realm handles, `src` only where the
@@ -169,6 +169,35 @@ describe('rows the running world no longer has', () => {
});
});
+describe('finding a row again after a pick', () => {
+ const RUNTIME = 'Runtime';
+
+ it('climbs the running world to a row the document never had', () => {
+ // A bullet picked in the running game: its ancestors are realm handles up to
+ // the authored row it was spawned under, whose key is the document's.
+ const live = liveTree([{ id: 900, src: 3 }, { id: 901, parent: 900 }, { id: 902, parent: 901 }]);
+ const view = mergeLiveTree(live, sceneDoc([{ id: 3 }]));
+ expect(revealKeys(view, spawnedRef(902), () => '')).toEqual([refKey(spawnedRef(901)), entityKey(3)]);
+ });
+
+ it('opens the folder a spawned root is collected in', () => {
+ const live = liveTree([{ id: 900, src: 3 }, { id: 901 }, { id: 902, parent: 901 }]);
+ const view = mergeLiveTree(live, sceneDoc([{ id: 3 }]));
+ expect(revealKeys(view, spawnedRef(902), () => RUNTIME)).toEqual([refKey(spawnedRef(901)), folderKey(RUNTIME)]);
+ });
+
+ it('climbs an authored row by the keys the document tree uses', () => {
+ const live = liveTree([{ id: 900, src: 3 }, { id: 901, src: 4, parent: 900 }]);
+ const view = mergeLiveTree(live, sceneDoc([{ id: 3 }, { id: 4, parent: 3 }]));
+ expect(revealKeys(view, authoredRef(4), () => 'Enemies')).toEqual([entityKey(3), folderKey('Enemies')]);
+ });
+
+ it('has nothing to open for a row this tree does not have', () => {
+ const view = mergeLiveTree(liveTree([{ id: 900, src: 3 }]), sceneDoc([{ id: 3 }]));
+ expect(revealKeys(view, spawnedRef(901), () => '')).toEqual([]);
+ });
+});
+
describe('one selection', () => {
beforeEach(() => useSelection.getState().select(null));