Skip to content

Commit

Permalink
Added Pool Factories to Settings
Browse files Browse the repository at this point in the history
  • Loading branch information
JanSeliv committed Nov 20, 2023
1 parent c0768b3 commit c7e620a
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 21 deletions.
Binary file modified Binaries/Win64/UnrealEditor-PoolManager.dll
Binary file not shown.
Binary file modified Binaries/Win64/UnrealEditor-PoolManager.pdb
Binary file not shown.
4 changes: 3 additions & 1 deletion Config/BasePoolManager.ini
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
[/Script/PoolManager.PoolManagerSettings]
SpawnObjectsPerFrame=5
SpawnObjectsPerFrame=5
+PoolFactories=/Script/PoolManager.PoolFactory_UObject
+PoolFactories=/Script/PoolManager.PoolFactory_Actor
22 changes: 22 additions & 0 deletions Source/PoolManager/Private/Data/PoolManagerSettings.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (c) Yevhenii Selivanov

#include "Data/PoolManagerSettings.h"
//---
#include UE_INLINE_GENERATED_CPP_BY_NAME(PoolManagerSettings)

// Returns all Pool Factories that will be used by the Pool Manager
void UPoolManagerSettings::GetPoolFactories(TArray<UClass*>& OutBlueprintPoolFactories) const
{
if (!OutBlueprintPoolFactories.IsEmpty())
{
OutBlueprintPoolFactories.Empty();
}

for (const TSoftClassPtr<UPoolFactory_UObject>& It : PoolFactories)
{
if (UClass* PoolFactoryClass = It.LoadSynchronous())
{
OutBlueprintPoolFactories.Emplace(PoolFactoryClass);
}
}
}
24 changes: 8 additions & 16 deletions Source/PoolManager/Private/PoolManagerSubsystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "PoolManagerSubsystem.h"
//---
#include "Factories/PoolFactory_UObject.h"
#include "Data/PoolManagerSettings.h"
//---
#include "Engine/World.h"
//---
Expand Down Expand Up @@ -146,7 +147,7 @@ bool UPoolManagerSubsystem::ReturnToPool_Implementation(UObject* Object)
{
return false;
}

FPoolContainer& Pool = FindPoolOrAdd(Object->GetClass());
Pool.GetFactoryChecked().OnReturnToPool(Object);

Expand Down Expand Up @@ -251,7 +252,8 @@ FPoolObjectHandle UPoolManagerSubsystem::CreateNewObjectInPool_Implementation(co
void UPoolManagerSubsystem::AddFactory(TSubclassOf<UPoolFactory_UObject> FactoryClass)
{
const UClass* ObjectClass = GetObjectClassByFactory(FactoryClass);
if (!ensureMsgf(ObjectClass, TEXT("ASSERT: [%i] %s:\n'ObjectClass' is not set for next factory: %s"), __LINE__, *FString(__FUNCTION__), *FactoryClass->GetName()))
if (!ensureMsgf(ObjectClass, TEXT("ASSERT: [%i] %s:\n'ObjectClass' is not set for next factory: %s"), __LINE__, *FString(__FUNCTION__), *FactoryClass->GetName())
|| AllFactoriesInternal.Contains(ObjectClass))
{
return;
}
Expand Down Expand Up @@ -322,21 +324,11 @@ const UClass* UPoolManagerSubsystem::GetObjectClassByFactory(const TSubclassOf<U
// Creates all possible Pool Factories to be used by the Pool Manager when dealing with objects
void UPoolManagerSubsystem::InitializeAllFactories()
{
if (!AllFactoriesInternal.IsEmpty())
{
// Already initialized
return;
}

for (TObjectIterator<UClass> It; It; ++It)
TArray<UClass*> AllPoolFactories;
UPoolManagerSettings::Get().GetPoolFactories(/*out*/AllPoolFactories);
for (UClass* FactoryClass : AllPoolFactories)
{
if (!It->IsChildOf(UPoolFactory_UObject::StaticClass())
|| It->HasAnyClassFlags(CLASS_Abstract))
{
continue;
}

AddFactory(*It);
AddFactory(FactoryClass);
}
}

Expand Down
12 changes: 11 additions & 1 deletion Source/PoolManager/Public/Data/PoolManagerSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
//---
#include "PoolManagerSettings.generated.h"

class UPoolFactory_UObject;

/**
* Contains common settings data of the Pool Manager plugin.
* Is set up in 'Project Settings' -> "Plugins" -> "Pool Manager".
Expand All @@ -28,13 +30,21 @@ class POOLMANAGER_API UPoolManagerSettings : public UDeveloperSettings

/** Gets the category for the settings, some high level grouping like, Editor, Engine, Game...etc. */
virtual FName GetCategoryName() const override { return TEXT("Plugins"); }

/** Returns a limit of how many actors to spawn per frame. */
UFUNCTION(BlueprintPure, Category = "Pool Manager")
int32 GetSpawnObjectsPerFrame() const { return SpawnObjectsPerFrame; }

/** Returns all Pool Factories that will be used by the Pool Manager. */
UFUNCTION(BlueprintPure, Category = "Pool Manager")
void GetPoolFactories(TArray<UClass*>& OutBlueprintPoolFactories) const;

protected:
/** Set a limit of how many actors to spawn per frame. */
UPROPERTY(Config, EditDefaultsOnly, BlueprintReadOnly, Category = "Pool Manager", meta = (BlueprintProtected = "true"))
int32 SpawnObjectsPerFrame;

/** All Pool Factories that will be used by the Pool Manager. */
UPROPERTY(Config, EditDefaultsOnly, BlueprintReadOnly, Category = "Pool Manager", meta = (BlueprintProtected = "true"))
TArray<TSoftClassPtr<UPoolFactory_UObject>> PoolFactories;
};
8 changes: 5 additions & 3 deletions Source/PoolManager/Public/Factories/PoolFactory_UObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
* Destruction: UObjects call ConditionalBeginDestroy; Actors call DestroyActor, Components call DestroyComponent, Widgets call RemoveFromParent etc.
* Pool: Actors and Scene Components are changing visibility, collision, ticking, etc. UObjects and Widgets are not.
*
* To create new factory just inherit from this/child class and override GetObjectClass() method.
* Pool Manager will automatically create and use your factory for objects of your class and its children.
*/
* To create new factory:
* 1. Inherit from this/child class
* 2. Add it to the 'Project Settings' -> "Plugins" -> "Pool Manager" -> "Pool Factories"
* 3. Override GetObjectClass() method.
*/
UCLASS(Blueprintable, BlueprintType)
class POOLMANAGER_API UPoolFactory_UObject : public UObject
{
Expand Down

0 comments on commit c7e620a

Please sign in to comment.