ComponentPool<T extends Component> class

A pool for managing reusable components.

This class allows you to efficiently reuse components, reducing the overhead of creating and destroying them frequently. The pool automatically manages component lifecycle by listening to the component's Component.removed future, returning components to the pool when they are removed from their parent.

Example usage:

final bulletPool = ComponentPool<MyBullet>(
  factory: () => MyBullet(),
  maxSize: 50,
  initialSize: 10,
);

// Acquire a bullet from the pool
final bullet = bulletPool.acquire();
// Configure the bullet as needed
bullet.position = Vector2(10, 20);
bullet.velocity = Vector2(100, 0);
// Add it to the game
game.add(bullet);

// Later, when the bullet should be destroyed:
bullet.removeFromParent(); // Automatically returns to pool

The pool automatically returns components when they are removed from their parent, so you don't need to manually release them. Simply call Component.removeFromParent when the component should be destroyed, and it will be automatically returned to the pool for reuse.

Constructors

ComponentPool({required T factory(), int maxSize = 100, int initialSize = 0})
Creates a new component pool with the specified factory, maximum size, and initial size. The factory is a function that creates new instances of the component type. The maxSize parameter limits the number of components that can be stored in the pool, while the initialSize parameter determines how many components are created initially.

Properties

availableCount int
The number of components currently available in the pool for acquisition.
no setter
hashCode int
The hash code for this object.
no setterinherited
maxSize int
The maximum number of components that can be stored in the pool. If the pool reaches this limit, additional components released back to the pool will be discarded.
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

acquire() → T
Acquires a component from the pool. If the pool is empty, a new component is created using the factory function. Otherwise, the last available component is removed from the pool and returned.
clear() → void
Clears all available components from the pool. This can be useful if you want to free up memory or reset the pool state.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited