Skip to content

Commit

Permalink
Added more ensures for hidden errors detection and temporary removed …
Browse files Browse the repository at this point in the history
…unfinished feature of taking returned object back from the pool instead of creating new one
  • Loading branch information
JanSeliv committed Nov 6, 2023
1 parent dfdcb29 commit 8b65c03
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 19 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.
3 changes: 2 additions & 1 deletion Binaries/Win64/UnrealEditor.modules
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"BuildId": "27405482",
"Modules":
{
"PoolManager": "UnrealEditor-PoolManager.dll"
"PoolManager": "UnrealEditor-PoolManager.dll",
"PoolManagerEditor": "UnrealEditor-PoolManagerEditor.dll"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ bool UPoolFactory_UObject::DequeueSpawnRequestByHandle(const FPoolObjectHandle&
return Request.Handle == Handle;
});

if (!SpawnQueueInternal.IsValidIndex(Idx))
if (!ensureMsgf(SpawnQueueInternal.IsValidIndex(Idx), TEXT("ASSERT: [%i] %s:\nHandle is not found within Spawn Requests, can't dequeue it: %s"), __LINE__, *FString(__FUNCTION__), *Handle.GetHash().ToString()))
{
return false;
}
Expand Down
38 changes: 21 additions & 17 deletions Source/PoolManager/Private/PoolManagerSubsystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,8 @@ const FPoolObjectData* UPoolManagerSubsystem::TakeFromPoolOrNull(const UClass* O
}

FPoolContainer* Pool = FindPool(ObjectClass);
if (!Pool)
if (!ensureMsgf(Pool, TEXT("ASSERT: [%i] %s:\n'Pool' is not registered for next class: %s"), __LINE__, *FString(__FUNCTION__), *ObjectClass->GetName()))
{
// Pool is not registered
return nullptr;
}

Expand Down Expand Up @@ -152,37 +151,34 @@ bool UPoolManagerSubsystem::ReturnToPool_Implementation(UObject* Object)

SetObjectStateInPool(EPoolObjectState::Inactive, *Object, *Pool);

if (!Factory.IsSpawnQueueEmpty())
{
// Spawn queue is not empty
// Take this object back from the pool instead of creating new one
FSpawnRequest OutRequest;
Factory.DequeueSpawnRequest(OutRequest);
TakeFromPool(OutRequest.Class, OutRequest.Transform, OutRequest.Callbacks.OnPostSpawned);
}

return true;
}

// Alternative to ReturnToPool() to return object to the pool by its handle
bool UPoolManagerSubsystem::ReturnToPool(const FPoolObjectHandle& Handle)
{
if (!ensureMsgf(Handle.IsValid(), TEXT("ASSERT: [%i] %s:\n'Handle' is not valid!"), __LINE__, *FString(__FUNCTION__)))
{
return false;
}

const FPoolContainer* Pool = FindPool(Handle.GetObjectClass());
if (!Pool)
if (!ensureMsgf(Pool, TEXT("ASSERT: [%i] %s:\n'Pool' is not registered for next class: %s"), __LINE__, *FString(__FUNCTION__), *Handle.GetObjectClass()->GetName()))
{
// Pool is not registered
return false;
}

if (const FPoolObjectData* ObjectData = Pool->FindInPool(Handle))
{
return ReturnToPool(ObjectData->PoolObject);
const bool bSucceed = ReturnToPool(ObjectData->PoolObject);
return ensureMsgf(bSucceed, TEXT("ASSERT: [%i] %s:\nFailed to return object to the Pool by given object!"), __LINE__, *FString(__FUNCTION__));
}

// It's exclusive feature of Handles:
// cancel spawn request if object returns to pool faster than it is spawned
FSpawnRequest OutRequest;
return Pool->GetFactoryChecked().DequeueSpawnRequestByHandle(Handle, OutRequest);
const bool bSucceed = Pool->GetFactoryChecked().DequeueSpawnRequestByHandle(Handle, OutRequest);
return ensureMsgf(bSucceed, TEXT("ASSERT: [%i] %s:\nGiven Handle is not known by Pool Manager and is not even in spawning queue!"), __LINE__, *FString(__FUNCTION__));
}

/*********************************************************************************************
Expand Down Expand Up @@ -529,8 +525,16 @@ int32 UPoolManagerSubsystem::GetRegisteredObjectsNum_Implementation(const UClass
UObject* UPoolManagerSubsystem::FindPoolObjectByHandle(const FPoolObjectHandle& Handle) const
{
const FPoolContainer* Pool = FindPool(Handle.GetObjectClass());
const FPoolObjectData* PoolObject = Pool ? Pool->FindInPool(Handle) : nullptr;
return PoolObject ? PoolObject->PoolObject : nullptr;
const FPoolObjectData* ObjectData = Pool ? Pool->FindInPool(Handle) : nullptr;
return ObjectData ? ObjectData->PoolObject : nullptr;
}

// Returns handle associated with given object
const FPoolObjectHandle& UPoolManagerSubsystem::FindPoolHandleByObject(const UObject* Object) const
{
const FPoolContainer* Pool = Object ? FindPool(Object->GetClass()) : nullptr;
const FPoolObjectData* ObjectData = Pool ? Pool->FindInPool(*Object) : nullptr;
return ObjectData ? ObjectData->Handle : FPoolObjectHandle::EmptyHandle;
}

/*********************************************************************************************
Expand Down
5 changes: 5 additions & 0 deletions Source/PoolManager/Public/PoolManagerSubsystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,11 @@ class POOLMANAGER_API UPoolManagerSubsystem : public UWorldSubsystem
UFUNCTION(BlueprintPure, Category = "Pool Manager")
UObject* FindPoolObjectByHandle(const FPoolObjectHandle& Handle) const;

/** Returns handle associated with given object.
* Can be invalid (FPoolObjectHandle::EmptyHandle) if not found. */
UFUNCTION(BlueprintPure, Category = "Pool Manager", meta = (DefaultToSelf = "Object"))
const FPoolObjectHandle& FindPoolHandleByObject(const UObject* Object) const;

/*********************************************************************************************
* Protected properties
********************************************************************************************* */
Expand Down

0 comments on commit 8b65c03

Please sign in to comment.