Cleaning the Object Pool

While working on my current project I came across an issue I wanted to solve involving Object Pooling. Now, I’m not sure if it would’ve affected my game much in regards to performance, but it was something I wanted to solve anyway. So here is my solution…

For a little backstory:

I think I had about 60 bullets at one point, with only the most recently instantiated being used…

My Solution:

Next, I created a Method called ClearChildObjects. Within that method is an if statement that determines if the object has children. If true, a for loop iterates through each one and destroys it.

Over in the Pool Manager class, I created an Action called clearChildren. This Action is called in the method that gets the current weapon from the Player to determine which bullet to instantiate.

ClearChildOjects over in ClearChildren is subscribed to that Action.

The final result is all the child objects being destroyed every time the Player changes weapons. Exactly what I wanted. The script can be attached to any object that needs its children cleared. You could also pass in the amount of children to clear, or use more specific parameters.

Only the current bullet is pooled.

Future Game Developer, Current Dasher and overall enjoying life.