|
| 1 | +// Fill out your copyright notice in the Description page of Project Settings. |
| 2 | + |
| 3 | +#pragma once |
| 4 | + |
| 5 | +#include "CoreMinimal.h" |
| 6 | +#include "Components/ActorComponent.h" |
| 7 | +#include "ObjectPoolComponent.generated.h" |
| 8 | + |
| 9 | +UENUM(BlueprintType) |
| 10 | +enum EOutputStates |
| 11 | +{ |
| 12 | + Success UMETA(DisplayName = "Success"), |
| 13 | + Failed UMETA(DisplayName = "Failed"), |
| 14 | +}; |
| 15 | + |
| 16 | +DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FActorSpawnedFromPool, AActor*, SpawnedActor); |
| 17 | +DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FActorReturnedToPool, AActor*, ReturnedActor); |
| 18 | + |
| 19 | +UCLASS(Blueprintable, ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) ) |
| 20 | +class OBJECTPOOL_API UObjectPoolComponent : public UActorComponent |
| 21 | +{ |
| 22 | + GENERATED_BODY() |
| 23 | + |
| 24 | +public: |
| 25 | + UObjectPoolComponent(); |
| 26 | + |
| 27 | + //Should pool actors be spawned on this component event begin play. |
| 28 | + UPROPERTY(EditAnywhere, BlueprintReadWrite, meta = (ExposeOnSpawn="true")) |
| 29 | + bool bSpawnPoolObjectsOnBeginPlay = true; |
| 30 | + |
| 31 | + //Subclass of AActor class to use in the pool. |
| 32 | + UPROPERTY(EditAnywhere, BlueprintReadOnly, meta = (ExposeOnSpawn="true")) |
| 33 | + TSubclassOf<AActor> PoolActor; |
| 34 | + |
| 35 | + //Define how big should be pool by default. |
| 36 | + UPROPERTY(EditAnywhere, BlueprintReadWrite, meta = (ExposeOnSpawn="true")) |
| 37 | + int32 InitialPoolSize; |
| 38 | + |
| 39 | + //Dispatcher. Each time an actor from the pool is spawned. |
| 40 | + UPROPERTY(BlueprintAssignable, BlueprintReadWrite) |
| 41 | + FActorSpawnedFromPool OnActorBeginSpawnedFromPool; |
| 42 | + |
| 43 | + //Dispatcher. Each time an actor is returned to the pool. |
| 44 | + UPROPERTY(BlueprintAssignable, BlueprintReadWrite) |
| 45 | + FActorReturnedToPool OnActorReturnedToPool; |
| 46 | + |
| 47 | + //Adds the specified number of actors to the pool. |
| 48 | + UFUNCTION(BlueprintCallable, Meta = (Keywords = "object pool, pool, object")) |
| 49 | + void AddActorsToPool(int const ActorsNumber); |
| 50 | + |
| 51 | + /*** |
| 52 | + * Returns an actor from the pool if any is available. |
| 53 | + * @param SpawnActorIfPoolIsEmpty If there is no available actor in the pool spawn another actor and add it to pool. |
| 54 | + */ |
| 55 | + UFUNCTION(BlueprintCallable, Meta = (ExpandEnumAsExecs = "Branch", Keywords = "object pool, pool, object")) |
| 56 | + void SpawnActorFromPool(FTransform SpawnTransform, AActor* Owner, APawn* Instigator, bool SpawnActorIfPoolIsEmpty, |
| 57 | + TEnumAsByte<EOutputStates>& Branch, AActor*& SpawnedActor); |
| 58 | + |
| 59 | + //Returns whether the pool has at least one free actor. |
| 60 | + UFUNCTION(BlueprintCallable, BlueprintPure, Meta = (Keywords = "object pool, pool, object")) |
| 61 | + bool HasPoolFreeActor() const; |
| 62 | + |
| 63 | + //Returns given actor back to the pool. |
| 64 | + UFUNCTION(BlueprintCallable, Meta = (Keywords = "object pool, pool, object")) |
| 65 | + void ReturnActorToPool(AActor* Actor); |
| 66 | + |
| 67 | + //Returns array of active actors in the pool. |
| 68 | + UFUNCTION(BlueprintCallable, Meta = (Keywords = "object pool, pool, object")) |
| 69 | + TArray<AActor*> GetActiveActorsFromPool() const; |
| 70 | + |
| 71 | + //Return array of inactive actors in the pool. |
| 72 | + UFUNCTION(BlueprintCallable, Meta = (Keywords = "object pool, pool, object")) |
| 73 | + TArray<AActor*> GetInactiveActorsFromPool() const; |
| 74 | + |
| 75 | + //Return total number of actors in the pool. |
| 76 | + UFUNCTION(BlueprintCallable, Meta = (Keywords = "object pool, pool, object")) |
| 77 | + int32 GetPoolSize() const; |
| 78 | + |
| 79 | + /*** |
| 80 | + * Destroys all the actors in the pool and releases the memory. |
| 81 | + * @warning Calling this function will cause all actors in the pool to be removed. |
| 82 | + * @warning Can cause hitch. |
| 83 | + */ |
| 84 | + UFUNCTION(BlueprintCallable, Meta = (Keywords = "object pool, pool, object")) |
| 85 | + void EmptyPool(); |
| 86 | + |
| 87 | + // Is given actor used by this pool right now. |
| 88 | + UFUNCTION(BlueprintCallable, BlueprintPure, Meta = (Keywords = "object pool, pool, object")) |
| 89 | + bool IsActorInUse(AActor* Actor) const; |
| 90 | + |
| 91 | +protected: |
| 92 | + virtual void BeginPlay() override; |
| 93 | + |
| 94 | + UPROPERTY() |
| 95 | + TArray<AActor*> ActiveActors; |
| 96 | + |
| 97 | + UPROPERTY() |
| 98 | + TArray<AActor*> InactiveActors; |
| 99 | + |
| 100 | +public: |
| 101 | + virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override; |
| 102 | + |
| 103 | + |
| 104 | +}; |
0 commit comments