CombatActor Pooling
Combat Pawns can use the Actor Pool by declaring a
NinjaCombatActorPoolComponent
.Combat Pawns with a valid Actor Pool must implement
CombatPoolProviderInterface
.The Actor Pool supports any Actor implementing
CombatPoolableActorInterface
.The
NinjaCombatPoolableActor
is a default implementation that supports replication.The pool is replicated, and pooled actors behave as if they were placed in the map.
Actors from the pool must implement On Activation and On Deactivation analogous to Begin Play and End Play.
It is common for combat systems to require frequent spawning and destroying of actors, such as projectiles or cast actors, in short intervals.
To mitigate the performance cost of constant actor spawning and destruction during gameplay, an Actor Pool was introduced to the Combat System. This pool prepares a predefined number of instances of frequently used actors for each combatant.
The Actor Pool Component
Each combatant has its own Actor Pool, represented by the NinjaCombatActorPoolComponent
. You can configure all the actors that will be pooled and specify how many instances will be available.
Combatants using an Actor Pool must implement the CombatPoolProviderInterface
to provide the component, allowing external functionalities, such as the Projectile Request, to quickly retrieve and use the pool.
Add the Actor Pool component to your character.
Add the Provider Interface and return the component, from the appropriate function.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "Interfaces/CombatPoolProviderInterface.h"
#include "PluginLabsCharacter.generated.h"
class UNinjaCombatActorPoolComponent;
UCLASS()
class PLUGINLABS_API APluginLabsCharacter : public ACharacter, public ICombatPoolProviderInterface
{
GENERATED_BODY()
public:
APluginLabsCharacter(const FObjectInitializer& ObjectInitializer = FObjectInitializer::Get());
// -- Begin Pool Provider interface.
virtual UNinjaCombatActorPoolComponent* GetActorPool_Implementation() const override;
// -- End Pool Provider interface.
private:
/** Pool of actors that are frequently used. */
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Components", meta = (AllowPrivateAccess = true))
TObjectPtr<UNinjaCombatActorPoolComponent> ActorPool;
};
#include "GameFramework/PluginLabsCharacter.h"
#include "Components/NinjaCombatActorPoolComponent.h"
APluginLabsCharacter::APluginLabsCharacter(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
{
static const FName ActorPoolName = TEXT("ActorPool");
ActorPool = CreateDefaultSubobject<UNinjaCombatActorPoolComponent>(ActorPoolName);
}
UNinjaCombatActorPoolComponent* APluginLabsCharacter::GetActorPool_Implementation() const
{
return ActorPool;
}
Poolable Actors
Poolable Actors are primarily defined by the CombatPoolableActorInterface
. Any actor implementing this interface can be added to the Actor Pool Component.
It's generally recommended to extend from NinjaCombatPoolableActor
, the default base class, rather than directly implementing the interface. This base actor not only implements the Poolable Actor interface but is also configured to support networking and replication, as if the actor was originally placed in the map.
The Actor Pool Component and the base Poolable Actor work together to reduce the initial network overhead that would otherwise occur when replicating many actors being spawned simultaneously to prepare the pool.
The Actor Pool can be configured in the Actor Pool Component by setting the actor class and the number of instances to initialize.
Actor Lifecycle
When you use Poolable Actors, it is important to keep in mind how the Actor lifecycle changes.
Functions like Begin Play and End Play will be called when the Poolable Actor is created and destroyed, making them inadequate places to handle events that should happen whenever the actor is retrieved from the pool.
For those situations, you should use On Activation and On Deactivation instead. Here are some examples of functionalities that should be handled by these events:
Playing a Particle Effect from the start.
Playing a Sound Effect from the start.
Adding velocity to a projectile.
note
Call Parent/Super
When extending
OnActivation
orOnDeactivation
, make sure to call their parent/super implementations!
Supported Actors
Currently, Projectiles and Cast actors are supported by default. You can find more details about their implementations in their specific Ability pages.