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(prefab): ?expand-virtual leaves out an instance marked as another layout

yhh291b5c24a9f1
diff --git a/sdk/src/prefab/deferred.ts b/sdk/src/prefab/deferred.ts
index 5c7f61a38..967a9625b 100644
--- a/sdk/src/prefab/deferred.ts
+++ b/sdk/src/prefab/deferred.ts
@@ -17,6 +17,9 @@ import type { PrefabData, PrefabOverride, ProcessedEntity } from './types';
/** The metadata key that makes an instance virtual. */
export const DEFERRED_KEY = 'deferred';
+/** The metadata key a scene instance carries when it is one layout of a page that has several (a phone's, say): `?expand-virtual` leaves such an instance out, as the layouts would stand side by side. */
+export const VARIANT_KEY = 'variant';
+
/** What stands in for a virtual instance until it is opened. `id` is the entity the root was made as. */
export interface DeferredRecord {
id: number;
diff --git a/sdk/src/scene/scene.ts b/sdk/src/scene/scene.ts
index 7f3424906..7b0b6e77a 100644
--- a/sdk/src/scene/scene.ts
+++ b/sdk/src/scene/scene.ts
@@ -9,7 +9,7 @@ import { World } from '../ecs/world';
import { Entity, INVALID_ENTITY } from '../types';
import { upgradeEntityComponents } from './legacyComponents';
import { isPrefabEntry, type SceneEntry } from './sceneEntry';
-import { canDefer, cutToRoot, isDeferredInstance, rootOnly, rootOverrides, type DeferredRecord } from '../prefab/deferred';
+import { canDefer, cutToRoot, isDeferredInstance, rootOnly, rootOverrides, type DeferredRecord, VARIANT_KEY } from '../prefab/deferred';
import { expandVirtualRequested, registerDeferredInstances } from './deferredInstances';
import { validateScene } from './validateScene';
import { getComponent, Name, Camera, RuntimeOnly } from '../ecs/component';
@@ -329,6 +329,7 @@ export async function expandScenePrefabs(
out.push(entry);
continue;
}
+ if (expandVirtual && entry.overrides.some((o) => o.type === 'metadata_set' && o.metadataKey === VARIANT_KEY)) continue;
const raw = await loadPrefab(entry.prefab);
if (!raw) {
log.warn('scene', `Prefab instance "${entry.prefab}" could not be resolved; instance skipped`);
diff --git a/sdk/tests/deferred-instances.test.ts b/sdk/tests/deferred-instances.test.ts
index abd85443e..8da318642 100644
--- a/sdk/tests/deferred-instances.test.ts
+++ b/sdk/tests/deferred-instances.test.ts
@@ -102,6 +102,13 @@ describe('virtual prefab instances: the page can ask for them whole', () => {
expect((await expandScenePrefabs(sceneOf(entry(3, 'shop', [mark()])), loader(prefabs))).deferredInstances).toHaveLength(1);
});
});
+ it('?expand-virtual leaves out an instance marked as another layout, and keeps it virtual otherwise', async () => {
+ const variant: PrefabOverride = { prefabEntityId: 'root', type: 'metadata_set', metadataKey: 'variant', value: 'phone' };
+ const scene = sceneOf(entry(3, 'shop', [mark(), variant]));
+ const whole = await expandScenePrefabs(scene, loader(prefabs), { expandVirtual: true });
+ expect(whole.entities.some((e) => e.name === 'shop')).toBe(false);
+ expect((await expandScenePrefabs(scene, loader(prefabs), { expandVirtual: false })).deferredInstances).toHaveLength(1);
+ });
it('the option wins over the address', async () => {
await withAddress('?expand-virtual', async () => {
expect((await expandScenePrefabs(sceneOf(entry(3, 'shop', [mark()])), loader(prefabs), { expandVirtual: false })).deferredInstances).toHaveLength(1);