To access the API functionality part of the PoolSubsystem, you need to access the wrapper library SmartPoolLibrary.
#include "Libraries/SmartPoolLibrary.h"
USmartPoolLibrary::ReleasePool(this, EPoolType::Global, EPoolReleaseMode::Deactivate); // Example Function
To access the API functionality part of an ActorPoolComponent, you need to access it via the component itself.
#include "Components/ActorPoolComponent.h"
.h
UPROPERTY(...);
TObjectPtr<UActorPoolComponent> ActorPoolComponent;
.cpp
ActorPoolComponent = CreateDefaultSubobject<UActorPoolComponent>(TEXT("ActorPoolComponent"));
if (IsValid(ActorPoolComponent))
{
ActorPoolComponent->ReleasePool(EPoolReleaseMode::Deactivate); // Example Function
}
To bind to delegates part of the PoolSubsystem, you must access the subsystem directly.
#include "Subsystems/PoolSubsystem.h"
const UWorld* World = Retrieve & Verify World;
if (UPoolSubsystem* PoolSubsystem = World->GetSubsystem<UPoolSubsystem>())
{
PoolSubsystem->OnWorldPoolExpanded.AddDynamic(this, &AMyActor::ExampleFunction); // Example Delegate
}
To bind to delegates part of an ActorPoolComponent, you access the reference (pointer) to the component.
#include "Components/ActorPoolComponent.h"
.h
UPROPERTY(...);
TObjectPtr<UActorPoolComponent> ActorPoolComponent;
.cpp
ActorPoolComponent = CreateDefaultSubobject<UActorPoolComponent>(TEXT("ActorPoolComponent"));
if (IsValid(ActorPoolComponent))
{
ActorPoolComponent->OnActorPoolExpanded.AddDynamic(this, &AMyActor::ExampleFunction); // Example Delegate
}
The following will describe how to spawn pooled actors using the PoolSubsystem and an ActorPoolComponent.
.h
UPROPERTY(...)
TSubclassOf<AActor> ActorClass;
.cpp
// PoolSubsystem
AActor* SpawnedActor = USmartPoolLibrary::SpawnPooledActor<AActor>(this, ActorClass, FTransform::Identity);
// ActorPoolComponent
if (IsValid(ActorPoolComponent))
{
AActor* SpawnedActor = ActorPoolComponent->SpawnPooledActor<AActor>(ActorClass, FTransform::Identity);
}
The following will describe how to deactivate a pooled actor using the PoolSubsystem and an ActorPoolComponent.
// PoolSubsystem
AActor* PooledActor;
USmartPoolLibrary::DeactivatePooledActor(PooledActor);
// ActorPoolComponent (Owning Actor)
AActor* PooledActor;
if (IsValid(ActorPoolComponent))
{
ActorPoolComponent->DeactivatePooledActor(PooledActor);
}
// ActorPoolComponent (Pooled Actor that inherits IPoolable)
.h
UPROPERTY(...);
TObjectPtr<UActorPoolComponent> ActorPoolComponent;
UActorPoolComponent* GetOwningPoolComponent() const;
virtual void ReceiveSetOwningPoolComponent_Implementation(UActorPoolComponent* InActorPoolComponent) override;
.cpp
UActorPoolComponent* AMyActor::GetOwningPoolComponent() const
{
return ActorPoolComponent;
}
void AMyActor::ReceiveSetOwningPoolComponent_Implementation(UActorPoolComponent* InActorPoolComponent)
{
ActorPoolComponent = InActorPoolComponent;
}
// Example Call
if (UActorPoolComponent* ActorPoolComponent = GetOwningPoolComponent(); IsValid(ActorPoolComponent))
{
ActorPoolComponent->DeactivatePooledActor(this);
}
To create your own custom pool profiles you must create a new PoolProfile data asset instance.
Right Click inside the Content Browser, then go to Miscellaneous > Data Asset.
Search Pool Profile in the Data Asset picker.
To use your chosen Pool Profile as the global pool, you will have to set it via the plugins Developer Settings.
Go to Edit > Project Settings.
Under the Game Category locate Smart Pool Settings.
Assign your chosen Pool Profile to the Global Pool Profile property.
To use your chosen Pool Profile as the local pool, you will have to set it via the World Settings of the level where you want the pool profile to be used.
Open the level you want to assign the Pool Profile to.
In the top right, click Settings > World Settings.
Locate the Smart Pool category and assign your chosen Pool Profile to the Local Pool Profile property.
To assign a Pool Profile to an ActorPoolComponent, you will have to set it via the component's Details Panel.
Open the blueprint that contains the ActorPoolComponent you want to assign a Pool Profile to.
Locate the ActorPoolComponent within the Components Panel.
Navigate towards the component's Details Panel and locate the Smart Pool category.
Assign your chosen Pool Profile to the Actor Pool Profile property.
To inherit the Poolable interface and be able to override the default OnActivate/OnDeactivate logic, you will need to do the following:
Make sure the inheriting class is at least an AActor derived class.
#include "Interfaces/Poolable.h"
.h
UCLASS(...)
class AMyActor : public AActor, public IPoolable
// Override if you want custom Activate/Deactivate Logic
virtual void OnActivate_Implementation() override;
virtual void OnDeactivate_Implementation() override;
.cpp
void AMyActor::OnActivate_Implementation()
{
// Default Logic
IPoolable::OnActivate_Implementation();
// Custom Logic
UE_LOG(LogTemp, Log, TEXT("Activated"));
}
void AMyActor::OnDeactivate_Implementation()
{
// Default Logic
IPoolable::OnDeactivate_Implementation();
// Custom Logic
UE_LOG(LogTemp, Log, TEXT("Deactivated"));
}
For blueprint only actors that don't already inherit the Poolable interface via C++, the following must be done:
Access the blueprint you want to implement the Poolable interface on.
Go to Class Settings > Implemented Interfaces.
Click Add and search Poolable.
In My Blueprint Panel open the Smart Pool category and implement the OnActivate & OnDeactivate events as a minimum.
To make your pooled actor fully compatible with an ActorPoolComponent you need to create a variable that points to the owning ActorPoolComponent and implement the rest of the interface functions, which are used internally to set the value of your variable.
Make sure the inheriting class is at least an AActor derived class.
#include "Interfaces/Poolable.h"
.h
UCLASS(...)
class AMyActor : public AActor, public IPoolable
// Implement for full compatibility with ActorPoolComponent
UPROPERTY(...);
TObjectPtr<UActorPoolComponent> ActorPoolComponent;
UActorPoolComponent* GetOwningPoolComponent() const; // Optional
virtual void ReceiveSetOwningPoolComponent_Implementation(UActorPoolComponent* InActorPoolComponent) override;
.cpp
UActorPoolComponent* AMyActor::GetOwningPoolComponent() const
{
return ActorPoolComponent;
}
void AMyActor::ReceiveSetOwningPoolComponent_Implementation(UActorPoolComponent* InActorPoolComponent)
{
ActorPoolComponent = InActorPoolComponent;
}
To inherit from PooledActor, which holds the necessary boilerplate for working with ActorPoolComponents and multiplayer actor pooling scenarios, you will have to do the following:
#include "Actors/PooledActor.h"
UCLASS(...)
class AMyActor : public APooledActor
Right Click inside the Content Browser, then go to Blueprint Class > All Classes.
Search for PooledActor and create the Blueprint using PooledActor as the parent class.