SceneSpawner

View source

Tracks pending scene instances by scene asset ID.

Each entry associates a scene AssetId with an entity carrying a SceneInstance component. The resource records which entities are waiting for their scene assets and lets callers inspect or update those associations. It does not instantiate scenes, create entities, or alter entities by itself. ScenePlugin provides this resource for a world.

Examples

import { SceneSpawner } from '@wimaengine/scene'

// Assume `world` has the scene resource available.
const spawner = world.getResource(SceneSpawner)
const instanceEntity = world.spawn([])
const sceneId = sceneHandle.id()

spawner.add(sceneId, instanceEntity)
spawner.add(sceneId, instanceEntity) // the entity is recorded once
console.log(spawner.get(sceneId)) // [instanceEntity.id()]

for (const pendingSceneId of spawner.assets()) {
  console.log(spawner.get(pendingSceneId))
}

spawner.remove(sceneId, instanceEntity)
spawner.flush(sceneId) // clears the key and returns sceneId

Constructor


constructor()

Methods


add(id: AssetId, entity: EntityHandle): void

View source

Records an entity as waiting for a scene asset.

The entity id is appended only when that id is not already recorded for the asset. Calling this method repeatedly for the same asset and entity is therefore idempotent. This method stores the id immediately; it does not check whether the asset is loaded or whether the entity still exists.

Parameters

NameDescriptionDefault
id——
entity——

remove(id: AssetId, entity: EntityHandle): void

View source

Stops tracking an entity for a scene asset.

If the asset has no list, or the list does not contain this entity id, the method does nothing. Removing the last entity leaves an empty list under the asset id; use clear or flush to remove that key. This method only changes the pending map and does not despawn the entity.

Parameters

NameDescriptionDefault
id——
entity——

clear(id: AssetId): void

View source

Removes all pending entities for a scene asset.

Missing asset ids are ignored. Clearing a key does not affect the scene asset or any entity that was previously listed under it.

Parameters

NameDescriptionDefault
id——

get(id: AssetId): readonly number[]

View source

Returns the pending entity ids for a scene asset.

The returned list is the resource’s stored list rather than a copy. It is typed as readonly for callers; use add, remove, and clear to maintain the map. An unknown asset id returns undefined. A key whose entities were all removed returns an empty list until the key is cleared.

Parameters

NameDescriptionDefault
id——

flush(id: AssetId): AssetId

View source

Removes all pending entities for an asset and returns its id.

This is a convenience for consuming a pending key. It does not look up the asset, spawn a scene, or change any entity. The same id is returned even when no key was present.

Parameters

NameDescriptionDefault
id——

assets(): IterableIterator<AssetId, any, any>

View source

Iterates over scene asset ids currently tracked by this resource.

Keys remain present after their last entity is removed, so the iterator can include an asset whose get result is an empty list. Use get to inspect the entity ids associated with each asset.

Returns

Iterator over the current asset ids.