2019-12-26 14:45:42 -05:00
// Copyright Epic Games, Inc. All Rights Reserved.
2014-03-14 14:13:41 -04:00
2018-03-27 14:27:07 -04:00
# include "NavRelevantComponent.h"
# include "NavigationSystem.h"
2014-03-14 14:13:41 -04:00
2022-09-24 13:57:58 -04:00
# include UE_INLINE_GENERATED_CPP_BY_NAME(NavRelevantComponent)
2014-10-14 10:29:11 -04:00
UNavRelevantComponent : : UNavRelevantComponent ( const FObjectInitializer & ObjectInitializer ) : Super ( ObjectInitializer )
2020-03-23 11:01:34 -04:00
, bAttachToOwnersRoot ( true )
, bBoundsInitialized ( false )
, bNavParentCacheInitialized ( false )
2014-03-14 14:13:41 -04:00
{
2016-03-11 09:55:03 -05:00
bCanEverAffectNavigation = true ;
2014-03-14 14:13:41 -04:00
bNavigationRelevant = true ;
}
2014-06-10 16:45:28 -04:00
void UNavRelevantComponent : : OnRegister ( )
{
Super : : OnRegister ( ) ;
2015-06-25 09:42:42 -04:00
if ( bAttachToOwnersRoot )
{
bool bUpdateCachedParent = true ;
# if WITH_EDITOR
2018-03-27 14:27:07 -04:00
UNavigationSystemV1 * NavSys = FNavigationSystem : : GetCurrent < UNavigationSystemV1 > ( GetWorld ( ) ) ;
2015-06-25 09:42:42 -04:00
if ( NavSys & & NavSys - > IsNavigationRegisterLocked ( ) )
{
bUpdateCachedParent = false ;
}
# endif
AActor * OwnerActor = GetOwner ( ) ;
if ( OwnerActor & & bUpdateCachedParent )
{
// attach to root component if it's relevant for navigation
UActorComponent * ActorComp = OwnerActor - > GetRootComponent ( ) ;
INavRelevantInterface * NavInterface = ActorComp ? Cast < INavRelevantInterface > ( ActorComp ) : nullptr ;
if ( NavInterface & & NavInterface - > IsNavigationRelevant ( ) & &
OwnerActor - > IsComponentRelevantForNavigation ( ActorComp ) )
{
CachedNavParent = ActorComp ;
}
// otherwise try actor itself under the same condition
if ( CachedNavParent = = nullptr )
{
NavInterface = Cast < INavRelevantInterface > ( OwnerActor ) ;
if ( NavInterface & & NavInterface - > IsNavigationRelevant ( ) )
{
CachedNavParent = OwnerActor ;
}
}
}
}
2014-08-22 06:53:27 -04:00
2020-03-23 11:01:34 -04:00
// Mark cache as initialized (even if null) from this point so calls to GetNavigationParent can be validated.
bNavParentCacheInitialized = true ;
2018-03-27 14:27:07 -04:00
FNavigationSystem : : OnComponentRegistered ( * this ) ;
2014-06-10 16:45:28 -04:00
}
void UNavRelevantComponent : : OnUnregister ( )
{
Super : : OnUnregister ( ) ;
2018-03-27 14:27:07 -04:00
FNavigationSystem : : OnComponentUnregistered ( * this ) ;
2014-06-10 16:45:28 -04:00
}
2014-08-22 06:53:27 -04:00
FBox UNavRelevantComponent : : GetNavigationBounds ( ) const
2014-03-14 14:13:41 -04:00
{
2016-03-29 16:33:59 -04:00
if ( ! bBoundsInitialized )
{
CalcAndCacheBounds ( ) ;
bBoundsInitialized = true ;
}
2014-08-22 06:53:27 -04:00
return Bounds ;
2014-03-14 14:13:41 -04:00
}
2014-08-22 06:53:27 -04:00
bool UNavRelevantComponent : : IsNavigationRelevant ( ) const
2014-03-14 14:13:41 -04:00
{
2014-08-22 06:53:27 -04:00
return bNavigationRelevant ;
2014-03-14 14:13:41 -04:00
}
2014-10-27 07:56:32 -04:00
void UNavRelevantComponent : : UpdateNavigationBounds ( )
{
2015-04-12 08:11:48 -04:00
CalcAndCacheBounds ( ) ;
2016-03-29 16:33:59 -04:00
bBoundsInitialized = true ;
2015-04-12 08:11:48 -04:00
}
UObject * UNavRelevantComponent : : GetNavigationParent ( ) const
{
2020-03-23 11:01:34 -04:00
UE_CLOG ( ! bNavParentCacheInitialized , LogNavigation , Error ,
TEXT ( " %s called before initialization of the navigation parent cache for [%s]. This might cause improper registration in the NavOctree and must be fixed. " ) ,
ANSI_TO_TCHAR ( __FUNCTION__ ) ,
* GetFullName ( ) ) ;
2015-06-25 09:42:42 -04:00
return CachedNavParent ;
2014-10-27 07:56:32 -04:00
}
2015-04-12 08:11:48 -04:00
void UNavRelevantComponent : : CalcAndCacheBounds ( ) const
{
const AActor * OwnerActor = GetOwner ( ) ;
2014-08-22 06:53:27 -04:00
const FVector MyLocation = OwnerActor ? OwnerActor - > GetActorLocation ( ) : FVector : : ZeroVector ;
2014-03-14 14:13:41 -04:00
2014-08-22 06:53:27 -04:00
Bounds = FBox : : BuildAABB ( MyLocation , FVector ( 100.0f , 100.0f , 100.0f ) ) ;
2014-03-14 14:13:41 -04:00
}
2015-04-17 03:23:20 -04:00
void UNavRelevantComponent : : ForceNavigationRelevancy ( bool bForce )
{
bAttachToOwnersRoot = ! bForce ;
if ( bForce )
{
bNavigationRelevant = true ;
}
RefreshNavigationModifiers ( ) ;
}
2015-04-12 08:11:48 -04:00
2014-03-14 14:13:41 -04:00
void UNavRelevantComponent : : SetNavigationRelevancy ( bool bRelevant )
{
if ( bNavigationRelevant ! = bRelevant )
{
bNavigationRelevant = bRelevant ;
RefreshNavigationModifiers ( ) ;
}
}
void UNavRelevantComponent : : RefreshNavigationModifiers ( )
{
2020-03-23 11:01:34 -04:00
// Only update after component registration since some required informations are initialized at that time (i.e. Cached Navigation Parent)
if ( bRegistered )
{
FNavigationSystem : : UpdateComponentData ( * this ) ;
}
2014-03-14 14:13:41 -04:00
}
2022-09-24 13:57:58 -04:00