Object Pooling in Unreal

  1. Hi, I would like to know if there is a built in Object Pooling System in UE4 to use for spawning and maintaining Actors on the Scene.
    I searched about it, but didn’t find any mentions. If there isn’t such system, then the other part of my question is about a custom implementation.
  2. I want to make a singleton class for managing my actor pool with a static field holding instance to it. As far as I know, I can’t make static fields an UPROPERY() so it cant be garbage collected automatically. Is there a better way to implement a singleton for this kind of Task?
  3. And lastly I would like to know if there is a function best suitable for initializing my Object pool. A function in which I could spawn my initial objects. It would be best is this could be called on the beginning of a level, before any actors can do anything. Is there such a function to override in GameMode, GameInstance or maybe GameState?

I’ve tried to make a component for pooling - it looks like this:

UCLASS(ClassGroup = (Transient), meta = (BlueprintSpawnableComponent))
class MY_API UTransientObject : public UActorComponent
{
	GENERATED_UCLASS_BODY()

public:
	UPROPERTY(EditAnywhere, Category = Transient)
	TSubclassOf<UObject> ClassToCreate;

	UPROPERTY(EditAnywhere, Instanced)
	UObject* Template;

	// Limit for spawnable objects
	UPROPERTY(EditAnywhere, Category = Transient)
	int32 MaxPoolSize;

private:
	UPROPERTY(Transient)
	TArray<UObject*> TransientObjectPool;
};

To spawn new object inside pool I use NewObject(GetTransientPackage(), ClassToCreate, NAME_None, RF_Transient, Template) which creates a new object of given class and based on a given template (template is visible in properties and can be modified in editor as you want). It works fine but there is still troubles with public\private tags for template object instance. If you don’t need in adjustable template it’s easy enougth to create this pool - just fill array with objects created based on your choosen class and then manage them. Storing pool objects inside UPROPERTY tagged array prevents from garbage collecting them.