// Copyright Epic Games, Inc. All Rights Reserved. #include "InteractionStatics.h" #include "Components/ActorComponent.h" #include "Components/PrimitiveComponent.h" #include "Engine/HitResult.h" #include "Engine/OverlapResult.h" #include "GameFramework/Actor.h" #include "IInteractableTarget.h" #include "Misc/AssertionMacros.h" #include "Templates/Casts.h" #include "UObject/Object.h" UInteractionStatics::UInteractionStatics() : Super(FObjectInitializer::Get()) { } AActor* UInteractionStatics::GetActorFromInteractableTarget(TScriptInterface InteractableTarget) { if (UObject* Object = InteractableTarget.GetObject()) { if (AActor* Actor = Cast(Object)) { return Actor; } else if (UActorComponent* ActorComponent = Cast(Object)) { return ActorComponent->GetOwner(); } else { unimplemented(); } } return nullptr; } void UInteractionStatics::GetInteractableTargetsFromActor(AActor* Actor, TArray>& OutInteractableTargets) { // If the actor is directly interactable, return that. TScriptInterface InteractableActor(Actor); if (InteractableActor) { OutInteractableTargets.Add(InteractableActor); } // If the actor isn't interactable, it might have a component that has a interactable interface. TArray InteractableComponents = Actor ? Actor->GetComponentsByInterface(UInteractableTarget::StaticClass()) : TArray(); for (UActorComponent* InteractableComponent : InteractableComponents) { OutInteractableTargets.Add(TScriptInterface(InteractableComponent)); } } void UInteractionStatics::AppendInteractableTargetsFromOverlapResults(const TArray& OverlapResults, TArray>& OutInteractableTargets) { for (const FOverlapResult& Overlap : OverlapResults) { TScriptInterface InteractableActor(Overlap.GetActor()); if (InteractableActor) { OutInteractableTargets.AddUnique(InteractableActor); } TScriptInterface InteractableComponent(Overlap.GetComponent()); if (InteractableComponent) { OutInteractableTargets.AddUnique(InteractableComponent); } } } void UInteractionStatics::AppendInteractableTargetsFromHitResult(const FHitResult& HitResult, TArray>& OutInteractableTargets) { TScriptInterface InteractableActor(HitResult.GetActor()); if (InteractableActor) { OutInteractableTargets.AddUnique(InteractableActor); } TScriptInterface InteractableComponent(HitResult.GetComponent()); if (InteractableComponent) { OutInteractableTargets.AddUnique(InteractableComponent); } }