Recently I thought about how to create an object pool with C++ in UE4. In the following I’m gonna explain you my thought process and my implementation. So first of all, there are three components for a pooling system to work and make sense: a lot of pools, a pool manager and some objects which would need to be instantiated and destroyed during runtime. Power ups are a common example for pooling. Especially when the are dropped by chance.
First off, I created a PoolItem class. Let’s have a look at what I did (look at the image section to the right or bottom). As you can see, first I have added two dynamic multicast delegates with read access only. This is a way for other other classes like the pool to react to certain actions happening. Worth pointing out is that the ondespawed a reference to the actor despawn passes. That enables the pool to despawn it without keeping direct reference after spawning. It just hooks up to that event after spawning and waits for the despawn call. That enables that the pool content could grow without a resize action of the reference list which is positive. Usually you preheat pools in a way where they never need to grow. But it happens.
It is not recommended for games which require to despawn all instances of a poolitem after the game ended. In that case you would rather want to always keep reference. Otherwise, all poolitems would need to be informed that the game ended and need to call despawn on themselves.
The class itself is abstract to avoid confusion with blueprints and prevent hollow instances of this class. The spawn and despawn functions are virtual in order to initialize and reset your actor in a custom way whenever such action happens. In this basic implementation here I just tried to remove the majority of overhead that these pooled objects could be creating. So I disabled and enabled ticks, rendering and collision.
The poolmanager has three major responsibilities: it should allow adding pools through the user interface, it should create objects and provide them to other classes which request them and in the end take them back so other objects can use them as well.
The preheat method generates all the pools with the amount of instances which are set per object. When creating them, it already hooks up the PutBack()-function to the despawn delegate so it will be put into the pool whenever required. This method itself just relocates the object which gets despawned and adds it to the pool object list again.
The Get() function last but not least returns an object in the desired location and rotation which gets removed from the pool list. Important here to notice is that the used Pop() function has false as parameter which means it won’t resize even if there are less elements.
Especially the pool class has still plenty of potential to be improved. Things like dynamic growing or maybe even dynamic shrinking because of live data analysis would be something nice. I will extend the classes in the future and put them right here. Thanks for making it so far through my tutorial.