Create new objects in pool if no free objects exist
This commit is contained in:
parent
4fe6cc1281
commit
25efc64ebd
@ -12,6 +12,8 @@ void AObjectPoolManager::BeginPlay()
|
||||
|
||||
void AObjectPoolManager::InitializeObjectPool(TSubclassOf<AActor> Object, const int InitialObjectPoolSize)
|
||||
{
|
||||
ObjectTemplate = Object;
|
||||
|
||||
for (int i = 0; i < InitialObjectPoolSize; i++)
|
||||
{
|
||||
if (AActor* object = GetWorld()->SpawnActor<AActor>(Object, FVector(100000.0f, 100000.0f, 0), FRotator(0, 0, 0)))
|
||||
@ -34,24 +36,27 @@ void AObjectPoolManager::InitializeObjectPool(UClass* Object, int InitialObjectP
|
||||
}
|
||||
}
|
||||
|
||||
AActor* AObjectPoolManager::GetObject()
|
||||
AActor* AObjectPoolManager::GetObject(int startingOffset)
|
||||
{
|
||||
for (AActor* object : ObjectPool)
|
||||
int ObjectPoolSize = ObjectPool.Num();
|
||||
for (int i = startingOffset; i < ObjectPoolSize; i++)
|
||||
{
|
||||
if (object->IsHidden())
|
||||
if (ObjectPool[i]->IsHidden())
|
||||
{
|
||||
SetObjectStatus(true, object);
|
||||
SetObjectStatus(true, ObjectPool[i]);
|
||||
|
||||
if (UObjectPoolComponent* objectPoolComponent = object->GetComponentByClass<UObjectPoolComponent>())
|
||||
if (UObjectPoolComponent* objectPoolComponent = ObjectPool[i]->GetComponentByClass<UObjectPoolComponent>())
|
||||
{
|
||||
objectPoolComponent->OnRetrieve.ExecuteIfBound();
|
||||
}
|
||||
|
||||
return object;
|
||||
return ObjectPool[i];
|
||||
}
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
InitializeObjectPool(ObjectTemplate);
|
||||
|
||||
return GetObject(ObjectPoolSize);
|
||||
}
|
||||
|
||||
void AObjectPoolManager::ReturnObject(AActor* object)
|
||||
|
@ -12,12 +12,13 @@ class VAMPIRES_API AObjectPoolManager : public AActor
|
||||
GENERATED_BODY()
|
||||
|
||||
TArray<TObjectPtr<AActor>> ObjectPool = TArray<TObjectPtr<AActor>>();
|
||||
|
||||
TSubclassOf<AActor> ObjectTemplate;
|
||||
|
||||
public:
|
||||
void InitializeObjectPool(TSubclassOf<AActor> Object, int InitialObjectPoolSize = 400);
|
||||
void InitializeObjectPool(UClass* Object, int InitialObjectPoolSize = 400);
|
||||
|
||||
AActor* GetObject();
|
||||
AActor* GetObject(int startingOffset = 0);
|
||||
|
||||
void ReturnObject(AActor* object);
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user