2023-01-31 14:46:14 -05:00
|
|
|
// Copyright Epic Games, Inc. All Rights Reserved.
|
|
|
|
|
|
|
|
|
|
#include "GeometryCollection/GeometryCollectionISMPoolSubSystem.h"
|
|
|
|
|
#include "GeometryCollection/GeometryCollectionISMPoolActor.h"
|
|
|
|
|
#include "Engine/World.h"
|
|
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------//
|
|
|
|
|
// UGeometryCollectionISMPoolSubSystem
|
|
|
|
|
//----------------------------------------------------------------------//
|
|
|
|
|
UGeometryCollectionISMPoolSubSystem::UGeometryCollectionISMPoolSubSystem()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void UGeometryCollectionISMPoolSubSystem::Initialize(FSubsystemCollectionBase& Collection)
|
|
|
|
|
{
|
|
|
|
|
Super::Initialize(Collection);
|
|
|
|
|
Collection.InitializeDependency<UGeometryCollectionISMPoolSubSystem>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void UGeometryCollectionISMPoolSubSystem::Deinitialize()
|
|
|
|
|
{
|
2023-08-25 13:36:52 -04:00
|
|
|
PerLevelISMPoolActors.Reset();
|
2023-01-31 14:46:14 -05:00
|
|
|
Super::Deinitialize();
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-25 13:36:52 -04:00
|
|
|
AGeometryCollectionISMPoolActor* UGeometryCollectionISMPoolSubSystem::FindISMPoolActor(ULevel* Level)
|
2023-01-31 14:46:14 -05:00
|
|
|
{
|
2023-08-25 13:36:52 -04:00
|
|
|
AGeometryCollectionISMPoolActor* ISMPoolActor = nullptr;
|
|
|
|
|
|
|
|
|
|
// on demand creation of the actor based on level
|
|
|
|
|
TObjectPtr<AGeometryCollectionISMPoolActor>* ISMPoolActorInLevel = PerLevelISMPoolActors.Find(Level);
|
|
|
|
|
if (ISMPoolActorInLevel)
|
|
|
|
|
{
|
|
|
|
|
ISMPoolActor = ISMPoolActorInLevel->Get();
|
|
|
|
|
}
|
|
|
|
|
else
|
2023-01-31 14:46:14 -05:00
|
|
|
{
|
|
|
|
|
// we keep it as transient to avoid accumulation of those actors in saved levels
|
|
|
|
|
FActorSpawnParameters Params;
|
|
|
|
|
Params.ObjectFlags = EObjectFlags::RF_DuplicateTransient | EObjectFlags::RF_Transient;
|
2023-08-25 13:36:52 -04:00
|
|
|
Params.OverrideLevel = Level;
|
2023-01-31 14:46:14 -05:00
|
|
|
ISMPoolActor = GetWorld()->SpawnActor<AGeometryCollectionISMPoolActor>(Params);
|
2023-08-25 13:36:52 -04:00
|
|
|
// spawn can still fail if we are in the middle or tearing down the world
|
|
|
|
|
if (ISMPoolActor)
|
|
|
|
|
{
|
|
|
|
|
PerLevelISMPoolActors.Add(Level, ISMPoolActor);
|
|
|
|
|
// make sure we capture when the actor get removed so we can update our internal structure accordingly
|
|
|
|
|
ISMPoolActor->OnEndPlay.AddDynamic(this, &UGeometryCollectionISMPoolSubSystem::OnActorEndPlay);
|
|
|
|
|
}
|
2023-01-31 14:46:14 -05:00
|
|
|
}
|
|
|
|
|
return ISMPoolActor;
|
|
|
|
|
}
|
2023-06-08 19:39:06 -04:00
|
|
|
|
2023-08-25 13:36:52 -04:00
|
|
|
void UGeometryCollectionISMPoolSubSystem::OnActorEndPlay(AActor* InSource, EEndPlayReason::Type Reason)
|
2023-06-08 19:39:06 -04:00
|
|
|
{
|
2023-08-25 13:36:52 -04:00
|
|
|
if (ULevel* ActorLevel = InSource->GetLevel())
|
2023-06-08 19:39:06 -04:00
|
|
|
{
|
2023-08-25 13:36:52 -04:00
|
|
|
PerLevelISMPoolActors.Remove(ActorLevel);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void UGeometryCollectionISMPoolSubSystem::GetISMPoolActors(TArray<AGeometryCollectionISMPoolActor*>& OutActors) const
|
|
|
|
|
{
|
|
|
|
|
for (const auto& MapEntry : PerLevelISMPoolActors)
|
|
|
|
|
{
|
|
|
|
|
if (MapEntry.Value != nullptr)
|
|
|
|
|
{
|
|
|
|
|
OutActors.Add(MapEntry.Value);
|
|
|
|
|
}
|
2023-06-08 19:39:06 -04:00
|
|
|
}
|
|
|
|
|
}
|