Node pools
Create and destroy nodes freely: for most games that is fast enough, and simpler. Reach for a node pool when a
game spawns many nodes a second (falling coins, bullets, floating “+10” labels) and you see it in
engine.stats.cpuMs or in garbage-collection pauses on a TV.
A pool keeps released nodes and hands them out again, reset to how they were made. nodePool is an import of its
own (script tags: FabiEngine.nodePool), so a game without pools bundles none of it:
import { nodePool } from '@fabiplay/engine'
const coins = nodePool(() => engine.shape({ type: 'circle', r: 16, fill: '#FBD46A' }), { max: 40 })
function spawn(x: number) { const coin = layer.add(coins.get({ x, y: -20 })) engine.tween(coin, { y: 740 }, 2, 'linear').then(() => coins.release(coin))}nodePool(factory, options?)
Section titled “nodePool(factory, options?)”factory: makes one node (any kind: a sprite, a shape, a text, an animation, a group with its children). Called byget()when no released node is free.options.max: the most free nodes kept. A node released whenmaxare already free is destroyed instead. Default: no limit.options.reset(node): called on each released node, after the engine reset it. Reset what the engine does not:data, a group’s children, your own fields.
It returns a NodePool:
get(props?) |
a free node, reset, or a new one from the factory; then props are applied (any node field, and a text’s text). It is not added anywhere: add it to a group. |
release(node) |
takes the node back: out of its group, hidden, its tweens stopped (their promises resolve), its animation stopped. Not destroyed. |
stats |
{ live, free, created }: given out, waiting, made in all. |
clear() |
destroys the free nodes (the ones in use stay). |
destroy() |
destroys every node the pool made, in use or free. |
What is reset
Section titled “What is reset”When a node is released it goes back to the state the factory left it in:
- every node:
x,y,rotation,scaleX,scaleY,alpha,visible,blend,tint,z,anchor,hitRadius,clip,mask,filters(the same objects the factory set: a clip or filter you changed in place stays changed); - sprites: the frame, the size (
width,height) andslice; - shapes: the spec (baked again only if you changed it);
- texts: the string and the style (baked again at the next draw if they changed);
- animations:
fps,loop,speed,onComplete, and playing as made, rewound to the first frame (the last one whenspeedis negative).
Not reset: data, a group’s children, and fields of your own node classes. Use options.reset:
const enemies = nodePool(makeEnemy, { reset: (e) => { e.data.hp = 3; e.data.hit = false } })The rules
Section titled “The rules”- Don’t keep a released node. After
release(node)the nextget()gives it to someone else: a reference kept in your game would now move another enemy. Drop your references when you release (set them tonull, take them out of your arrays). In development builds the engine warns when a released node is used (see below). - A node belongs to the pool that made it. Releasing it to another pool, or twice, does nothing (in development builds the console says so once).
- Destroying a pooled node is allowed: it leaves its pool. A destroyed node is a stale handle (see
Nodes are handles): it can never move another node. A released node is
not stale (it keeps its slot, ready for the next
get()), which is why a reference kept afterrelease()is the one mistake that can still move someone else’s node. - Groups keep their children: the factory builds the whole group once; release and get move it as a whole.
- Timelines that move a released node skip it while it is free; stop them yourself when you release it.
get() and release() allocate nothing while free nodes are there (apart from what props you pass cost), so a game
spawning dozens of nodes a second makes no garbage for them.
Development warnings
Section titled “Development warnings”In development builds (the npm package’s development condition, the readable .umd.js script tags), using a node
after destroy() or after release() logs one warning per node: what it is, its frame or text, and what was done.
A pool destroying its own free nodes (clear(), past max) is not a misuse and says nothing.
[engine] Sprite 'coin' used after release() to its node pool: setting xProduction builds (.min.js, the npm default) carry none of these checks.