Thursday, 31 December 2015

Stop recycling game objects

Many consider object pools a staple of game development. Here's a few examples:

Although Wikipedia does hint at potential pitfalls, there is a much greater problem than having (or forgetting) to reset pooled objects: how do you ensure that references to your objects are cleared before pooling them?

Your object may have registered to receive events.
Or it may be added to a list somewhere.
Or it may be assigned to a field in about a couple dozen places
Yes, there are possibly hundreds of objects in your runtime which may hold references to an object you'd like to recycle.

In languages like C++, it is your responsibility to clear these references. Not clearing references before destroying an object leads to wild pointers. Wild pointers lead to intermittent crashes and bugs.

In C#, Unwanted references to pooled objects fail subtly: your application won't crash but a (usually unknown) number of objects will continue to talk to your object without knowing that it's been recycled.

Pooling is an optimisation technique. If you have to use it, know that just resetting these objects is the easy part.

No comments:

Post a Comment