2021-09-28 13:33:00 -04:00
|
|
|
// Copyright Epic Games, Inc. All Rights Reserved.
|
|
|
|
|
|
|
|
|
|
#include "SmartObjectComponent.h"
|
2022-03-16 03:47:02 -04:00
|
|
|
|
2021-09-28 13:33:00 -04:00
|
|
|
#include "SmartObjectSubsystem.h"
|
2023-02-02 18:43:13 -05:00
|
|
|
#include "VisualLogger/VisualLogger.h"
|
2021-09-28 13:33:00 -04:00
|
|
|
|
2022-09-28 01:06:15 -04:00
|
|
|
#include UE_INLINE_GENERATED_CPP_BY_NAME(SmartObjectComponent)
|
|
|
|
|
|
2021-09-28 13:33:00 -04:00
|
|
|
#if WITH_EDITOR
|
|
|
|
|
#include "Engine/World.h"
|
|
|
|
|
#endif
|
|
|
|
|
|
2022-11-24 14:53:52 -05:00
|
|
|
#if WITH_EDITORONLY_DATA
|
|
|
|
|
USmartObjectComponent::FOnSmartObjectChanged USmartObjectComponent::OnSmartObjectChanged;
|
|
|
|
|
#endif // WITH_EDITORONLY_DATA
|
|
|
|
|
|
2021-09-28 13:33:00 -04:00
|
|
|
USmartObjectComponent::USmartObjectComponent(const FObjectInitializer& ObjectInitializer)
|
|
|
|
|
: Super(ObjectInitializer)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-24 14:53:52 -05:00
|
|
|
void USmartObjectComponent::PostInitProperties()
|
|
|
|
|
{
|
|
|
|
|
Super::PostInitProperties();
|
|
|
|
|
#if WITH_EDITORONLY_DATA
|
|
|
|
|
if (HasAnyFlags(RF_ClassDefaultObject) == false)
|
|
|
|
|
{
|
|
|
|
|
if (AActor* Actor = GetOwner())
|
|
|
|
|
{
|
|
|
|
|
// tagging owner actors since the tags get included in FWorldPartitionActorDesc
|
|
|
|
|
// and that's the only way we can tell a given actor has a SmartObjectComponent
|
|
|
|
|
// until it's fully loaded
|
|
|
|
|
if (Actor->Tags.Contains(UE::SmartObjects::WithSmartObjectTag) == false)
|
|
|
|
|
{
|
|
|
|
|
Actor->Tags.AddUnique(UE::SmartObjects::WithSmartObjectTag);
|
|
|
|
|
Actor->MarkPackageDirty();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif // WITH_EDITORONLY_DATA
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-28 13:33:00 -04:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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))
|
|
|
|
|
{
|
2022-11-04 03:35:03 -04:00
|
|
|
Subsystem->RegisterSmartObject(*this);
|
2021-09-28 13:33:00 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-24 14:53:52 -05:00
|
|
|
if (GetRegisteredHandle().IsValid())
|
2023-02-02 18:43:13 -05:00
|
|
|
{
|
2022-11-24 14:53:52 -05:00
|
|
|
if (USmartObjectSubsystem* Subsystem = USmartObjectSubsystem::GetCurrent(World))
|
|
|
|
|
{
|
2022-12-07 13:05:23 -05:00
|
|
|
if (!IsBeingDestroyed())
|
|
|
|
|
{
|
|
|
|
|
Subsystem->UnregisterSmartObject(*this);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// note that this case is really only expected in the editor when the component is being unregistered
|
|
|
|
|
// as part of DestroyComponent. In default game flow EndPlay will get called first and once we make
|
|
|
|
|
// it here the RegisteredHandle should already be Invalid
|
|
|
|
|
Subsystem->RemoveSmartObject(*this);
|
|
|
|
|
}
|
2022-11-24 14:53:52 -05:00
|
|
|
}
|
2021-09-28 13:33:00 -04:00
|
|
|
}
|
2022-09-23 20:48:44 -04:00
|
|
|
}
|
|
|
|
|
|
2022-12-05 08:19:29 -05:00
|
|
|
void USmartObjectComponent::EndPlay(const EEndPlayReason::Type EndPlayReason)
|
|
|
|
|
{
|
|
|
|
|
if (EndPlayReason == EEndPlayReason::Destroyed && GetRegisteredHandle().IsValid())
|
|
|
|
|
{
|
|
|
|
|
if (USmartObjectSubsystem* Subsystem = USmartObjectSubsystem::GetCurrent(GetWorld()))
|
|
|
|
|
{
|
2022-12-07 13:05:23 -05:00
|
|
|
Subsystem->RemoveSmartObject(*this);
|
2022-12-05 08:19:29 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Super::EndPlay(EndPlayReason);
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-28 13:33:00 -04:00
|
|
|
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
|
|
|
|
2022-12-05 08:19:29 -05:00
|
|
|
void USmartObjectComponent::SetRegisteredHandle(const FSmartObjectHandle Value, const ESmartObjectRegistrationType InRegistrationType)
|
|
|
|
|
{
|
|
|
|
|
ensure(Value.IsValid());
|
|
|
|
|
ensure(RegisteredHandle.IsValid() == false || RegisteredHandle == Value);
|
|
|
|
|
RegisteredHandle = Value;
|
|
|
|
|
ensure(RegistrationType == ESmartObjectRegistrationType::None && InRegistrationType != ESmartObjectRegistrationType::None);
|
|
|
|
|
RegistrationType = InRegistrationType;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void USmartObjectComponent::InvalidateRegisteredHandle()
|
|
|
|
|
{
|
|
|
|
|
RegisteredHandle = FSmartObjectHandle::Invalid;
|
|
|
|
|
RegistrationType = ESmartObjectRegistrationType::None;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-02 18:43:13 -05:00
|
|
|
void USmartObjectComponent::OnRuntimeInstanceBound(FSmartObjectRuntime& RuntimeInstance)
|
|
|
|
|
{
|
|
|
|
|
EventDelegateHandle = RuntimeInstance.GetMutableEventDelegate().AddUObject(this, &USmartObjectComponent::OnRuntimeEventReceived);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void USmartObjectComponent::OnRuntimeInstanceUnbound(FSmartObjectRuntime& RuntimeInstance)
|
|
|
|
|
{
|
|
|
|
|
if (EventDelegateHandle.IsValid())
|
|
|
|
|
{
|
|
|
|
|
RuntimeInstance.GetMutableEventDelegate().Remove(EventDelegateHandle);
|
|
|
|
|
EventDelegateHandle.Reset();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-03 13:28:03 -05:00
|
|
|
TStructOnScope<FActorComponentInstanceData> USmartObjectComponent::GetComponentInstanceData() const
|
|
|
|
|
{
|
|
|
|
|
return MakeStructOnScope<FActorComponentInstanceData, FSmartObjectComponentInstanceData>(this, DefinitionAsset);
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-24 14:53:52 -05:00
|
|
|
#if WITH_EDITOR
|
|
|
|
|
void USmartObjectComponent::PostEditUndo()
|
|
|
|
|
{
|
|
|
|
|
Super::PostEditUndo();
|
|
|
|
|
|
|
|
|
|
OnSmartObjectChanged.Broadcast(*this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void USmartObjectComponent::PostEditChangeProperty(FPropertyChangedEvent& PropertyChangedEvent)
|
|
|
|
|
{
|
|
|
|
|
Super::PostEditChangeProperty(PropertyChangedEvent);
|
|
|
|
|
|
|
|
|
|
OnSmartObjectChanged.Broadcast(*this);
|
|
|
|
|
}
|
|
|
|
|
#endif // WITH_EDITOR
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
// FSmartObjectComponentInstanceData
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2022-02-03 13:28:03 -05:00
|
|
|
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);
|
2022-03-16 03:47:02 -04:00
|
|
|
// We only need to force a register if DefinitionAsset is currently null and a valid one was backed up.
|
|
|
|
|
// Reason is that our registration to the Subsystem depends on a valid definition so it can be skipped.
|
|
|
|
|
if (SmartObjectComponent->DefinitionAsset != DefinitionAsset && SmartObjectComponent->DefinitionAsset == nullptr)
|
2022-02-03 13:28:03 -05:00
|
|
|
{
|
|
|
|
|
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);
|
2022-03-16 03:47:02 -04:00
|
|
|
}
|
2022-09-28 01:06:15 -04:00
|
|
|
|
2023-02-02 18:43:13 -05:00
|
|
|
void USmartObjectComponent::OnRuntimeEventReceived(const FSmartObjectEventData& Event)
|
|
|
|
|
{
|
|
|
|
|
const AActor* Interactor = nullptr;
|
2023-02-13 20:06:02 -05:00
|
|
|
if (const FSmartObjectActorUserData* ActorUser = Event.EventPayload.GetPtr<const FSmartObjectActorUserData>())
|
2023-02-02 18:43:13 -05:00
|
|
|
{
|
|
|
|
|
Interactor = ActorUser->UserActor.Get();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
UE_CVLOG_LOCATION(Interactor != nullptr, USmartObjectSubsystem::GetCurrent(GetWorld()), LogSmartObject, Display,
|
|
|
|
|
Interactor->GetActorLocation(), /*Radius*/25.f, FColor::Green, TEXT("%s: %s. Interactor: %s"),
|
|
|
|
|
*GetNameSafe(GetOwner()), *UEnum::GetValueAsString(Event.Reason), *GetNameSafe(Interactor));
|
|
|
|
|
|
|
|
|
|
ReceiveOnEvent(Event, Interactor);
|
|
|
|
|
OnSmartObjectEvent.Broadcast(Event, Interactor);
|
|
|
|
|
}
|