Pool<T extends GameObject> class
final
Objects to use again rather than make and destroy each time, such as
shots, enemies or sparks (Unity's ObjectPool<T>).
get hands out a hidden one, or makes a new one under parent;
release (or the object's own GameObject.release) hides it again
for the next get. A released object starts clean: the routines and
motions it and everything below it ran are stopped, and it's back
under parent. What else it keeps, its fields and the signals it
listens to, is the game's to reset: in get's reset, or in
onEnable, which runs on each get.
Something that holds on to a pooled object, such as a shot chasing an enemy, keeps an ObjectHandle to it instead: the handle goes null once the object is released, even if get hands it out again.
late final shots = Pool(this, Shot.new);
void fire() => shots.get((shot) => shot.position = muzzle.position);
// in Shot, when it hits or flies out:
release();
Constructors
- Pool(GameObject parent, T make(), {int? maxSize})
-
A pool that adds the objects
makemakes toparent. WithmaxSize, a release beyond that many hidden objects destroys the object instead.
Properties
-
active
→ List<
T> -
The objects handed out, as a new list: one that stays as it is
whatever is released while it's walked.
no setter
- countActive → int
-
The objects handed out and not released.
no setter
- countAll → int
-
Every object the pool has that isn't destroyed.
no setter
- countInactive → int
-
The hidden objects waiting for get.
no setter
- hashCode → int
-
The hash code for this object.
no setterinherited
- make → T Function()
-
Makes a new object, as
Shot.newdoes.final - maxSize → int?
-
The most hidden objects the pool keeps, or null for no limit.
final
- parent → GameObject
-
Where new objects are added, and where released ones go back to.
final
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
activeAt(
int index) → T -
The handed-out object at
index, 0 to one less than countActive, to walk them with nothing made:for (var i = shots.countActive - 1; i >= 0; i--) shots.activeAt(i).fly(). Walk from the end to release as you go: a release moves the last one into its place. -
clear(
) → void - Destroys the hidden objects; those handed out stay.
-
get(
[void reset(T object)?]) → T -
A hidden object, or a new one:
resetgets it first, then it's shown, so itsonEnablesees whatresetdid. A new one appears once it's loaded (see GameObject.onLoad). -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
prewarm(
int count) → void -
Makes objects until
countare hidden and waiting, so the first gets needn't make any (and load them) in the middle of play. -
release(
T object) → void -
Hides
objectfor the next get: it and everything below it stop their routines and motions and cancel what they listened to withonwhile shown (what they listened to in their load stays), it goes back under parent, and each ObjectHandle to it goes null. Beyond maxSize it's destroyed instead. One destroyed already is left alone. Throws if it isn't this pool's or was released already. -
toString(
) → String -
A string representation of this object.
override
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited