2021-09-28 13:33:00 -04:00
|
|
|
// Copyright Epic Games, Inc. All Rights Reserved.
|
|
|
|
|
|
|
|
|
|
#include "SmartObjectComponent.h"
|
|
|
|
|
#include "GameFramework/Actor.h"
|
|
|
|
|
#include "SmartObjectSubsystem.h"
|
|
|
|
|
#include "VisualLogger/VisualLogger.h"
|
|
|
|
|
|
|
|
|
|
#if WITH_EDITOR
|
|
|
|
|
#include "Engine/World.h"
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
USmartObjectComponent::USmartObjectComponent(const FObjectInitializer& ObjectInitializer)
|
|
|
|
|
: Super(ObjectInitializer)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void USmartObjectComponent::OnRegister()
|
|
|
|
|
{
|
|
|
|
|
Super::OnRegister();
|
|
|
|
|
|
2022-02-03 13:28:03 -05:00
|
|
|
RegisterToSubsystem();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void USmartObjectComponent::RegisterToSubsystem()
|
|
|
|
|
{
|
2021-11-02 11:12:43 -04:00
|
|
|
const UWorld* World = GetWorld();
|
2021-09-28 13:33:00 -04:00
|
|
|
if (World == nullptr)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if WITH_EDITOR
|
|
|
|
|
// Do not process any component registered to preview world
|
|
|
|
|
if (World->WorldType == EWorldType::EditorPreview)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
if (HasAnyFlags(RF_ClassDefaultObject))
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-26 15:48:13 -05:00
|
|
|
/** Do not register components that don't have a valid definition */
|
|
|
|
|
if (!IsValid(DefinitionAsset))
|
2021-11-02 11:12:43 -04:00
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-28 13:33:00 -04:00
|
|
|
// Note: we don't report error or ensure on missing subsystem since it might happen
|
|
|
|
|
// in various scenarios (e.g. inactive world)
|
|
|
|
|
if (USmartObjectSubsystem* Subsystem = USmartObjectSubsystem::GetCurrent(World))
|
|
|
|
|
{
|
|
|
|
|
Subsystem->RegisterSmartObject(*this);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void USmartObjectComponent::OnUnregister()
|
|
|
|
|
{
|
|
|
|
|
Super::OnUnregister();
|
|
|
|
|
|
2021-11-02 11:12:43 -04:00
|
|
|
const UWorld* World = GetWorld();
|
2021-09-28 13:33:00 -04:00
|
|
|
if (World == nullptr)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if WITH_EDITOR
|
|
|
|
|
// Do not process any component registered to preview world
|
|
|
|
|
if (World->WorldType == EWorldType::EditorPreview)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
if (HasAnyFlags(RF_ClassDefaultObject))
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-26 15:48:13 -05:00
|
|
|
/** Do not register components that don't have a valid definition */
|
|
|
|
|
if (!IsValid(DefinitionAsset))
|
2021-11-02 11:12:43 -04:00
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-28 13:33:00 -04:00
|
|
|
// Note: we don't report error or ensure on missing subsystem since it might happen
|
|
|
|
|
// in various scenarios (e.g. inactive world, AI system is cleaned up before the components gets unregistered, etc.)
|
|
|
|
|
if (USmartObjectSubsystem* Subsystem = USmartObjectSubsystem::GetCurrent(World))
|
|
|
|
|
{
|
|
|
|
|
Subsystem->UnregisterSmartObject(*this);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FBox USmartObjectComponent::GetSmartObjectBounds() const
|
|
|
|
|
{
|
|
|
|
|
FBox BoundingBox(ForceInitToZero);
|
2022-01-07 14:28:06 -05:00
|
|
|
|
2021-09-28 13:33:00 -04:00
|
|
|
const AActor* Owner = GetOwner();
|
2022-01-07 14:28:06 -05:00
|
|
|
if (Owner != nullptr && DefinitionAsset != nullptr)
|
2021-09-28 13:33:00 -04:00
|
|
|
{
|
2022-01-07 14:28:06 -05:00
|
|
|
BoundingBox = DefinitionAsset->GetBounds().TransformBy(Owner->GetTransform());
|
2021-09-28 13:33:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return BoundingBox;
|
|
|
|
|
}
|
2022-02-03 13:28:03 -05:00
|
|
|
|
|
|
|
|
TStructOnScope<FActorComponentInstanceData> USmartObjectComponent::GetComponentInstanceData() const
|
|
|
|
|
{
|
|
|
|
|
return MakeStructOnScope<FActorComponentInstanceData, FSmartObjectComponentInstanceData>(this, DefinitionAsset);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool FSmartObjectComponentInstanceData::ContainsData() const
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FSmartObjectComponentInstanceData::ApplyToComponent(UActorComponent* Component, const ECacheApplyPhase CacheApplyPhase)
|
|
|
|
|
{
|
|
|
|
|
// Apply data first since we might need to register to the subsystem
|
|
|
|
|
// before the component gets re-registered by the base class.
|
|
|
|
|
if (CacheApplyPhase == ECacheApplyPhase::PostUserConstructionScript)
|
|
|
|
|
{
|
|
|
|
|
USmartObjectComponent* SmartObjectComponent = CastChecked<USmartObjectComponent>(Component);
|
|
|
|
|
if (SmartObjectComponent->DefinitionAsset != DefinitionAsset)
|
|
|
|
|
{
|
|
|
|
|
SmartObjectComponent->DefinitionAsset = DefinitionAsset;
|
|
|
|
|
// Registering to the subsystem should only be attempted on registered component
|
|
|
|
|
// otherwise the OnRegister callback will take care of it.
|
|
|
|
|
if (SmartObjectComponent->IsRegistered())
|
|
|
|
|
{
|
|
|
|
|
SmartObjectComponent->RegisterToSubsystem();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Super::ApplyToComponent(Component, CacheApplyPhase);
|
|
|
|
|
}
|