Back to Aframe

pool

docs/components/pool.md

1.7.11.9 KB
Original Source

The pool component allows for object pooling. This gives us a reusable pool of entities to avoid creating and destroying the same kind of entities in dynamic scenes. Object pooling helps reduce garbage collection pauses.

Note that entities requested from the pool are paused by default and you need to call .play() in order to activate their components' tick functions.

For performance reasons, unused entities in the pool are detached from the THREE.js scene graph, which means that they are not rendered, their matrices are not updated, and they are excluded from raycasting.

Example

For example, we may have a game with enemy entities that we want to reuse.

html
<a-scene pool__enemy="mixin: enemy; size: 10"></a-scene>
js
var el = sceneEl.components.pool__enemy.requestEntity();
el.play();
sceneEl.components.pool__enemy.returnEntity(el);

Properties

PropertyDescriptionDefault Value
containerSelector to store pooled entities. Defaults to the scene.''
dynamicGrow the pool automatically if more entities are requested after reaching the size.false
mixinMixin required to initialize the entities of the pool.''
sizeNumber of preallocated entities in the pool.0

Methods

.requestEntity ()

Request one of the available entities in the pool. Will return undefined and log a warning if dynamic is set to false and you have exhausted the pool.

.returnEntity (entityEl)

Relinquish an entity back to the pool. Will log a warning if you attempt to return an entity that did not belong to this pool.