2022-02-21 02:04:58 -05:00
// Copyright Epic Games, Inc. All Rights Reserved.
2021-11-24 04:26:12 -05:00
2022-09-01 09:06:53 -04:00
# include "Blueprint/StateTreeNodeBlueprintBase.h"
2021-11-24 04:26:12 -05:00
# include "AIController.h"
# include "StateTreeExecutionContext.h"
2022-09-01 09:06:53 -04:00
# include "VisualLogger/VisualLogger.h"
2021-11-24 04:26:12 -05:00
2022-09-28 01:06:15 -04:00
# include UE_INLINE_GENERATED_CPP_BY_NAME(StateTreeNodeBlueprintBase)
2022-09-01 09:06:53 -04:00
UWorld * UStateTreeNodeBlueprintBase : : GetWorld ( ) const
2021-11-24 04:26:12 -05:00
{
2022-12-02 05:18:07 -05:00
// The items are duplicated as the State Tree execution context as outer, so this should be essentially the same as GetWorld() on StateTree context.
2021-11-24 04:26:12 -05:00
// The CDO is used by the BP editor to check for certain functionality, make it return nullptr so that the GetWorld() passes as overridden.
if ( ! HasAnyFlags ( RF_ClassDefaultObject ) )
{
2022-12-05 09:16:37 -05:00
if ( CachedOwner ! = nullptr )
2022-12-02 05:18:07 -05:00
{
2022-12-05 09:16:37 -05:00
return CachedOwner - > GetWorld ( ) ;
2022-12-02 05:18:07 -05:00
}
2021-11-24 04:26:12 -05:00
if ( UObject * Outer = GetOuter ( ) )
{
return Outer - > GetWorld ( ) ;
}
}
return nullptr ;
}
2022-09-01 09:06:53 -04:00
AActor * UStateTreeNodeBlueprintBase : : GetOwnerActor ( const FStateTreeExecutionContext & Context ) const
2021-11-24 04:26:12 -05:00
{
if ( const AAIController * Controller = Cast < AAIController > ( Context . GetOwner ( ) ) )
{
return Controller - > GetPawn ( ) ;
}
return Cast < AActor > ( Context . GetOwner ( ) ) ;
}
2023-01-23 12:48:04 -05:00
void UStateTreeNodeBlueprintBase : : SetCachedInstanceDataFromContext ( const FStateTreeExecutionContext & Context ) const
2022-09-01 09:06:53 -04:00
{
2023-01-23 12:48:04 -05:00
if ( FStateTreeInstanceData * InstanceData = Context . GetMutableInstanceData ( ) )
{
InstanceStorage = & InstanceData - > GetMutableStorage ( ) ;
}
CachedState = Context . GetCurrentlyProcessedState ( ) ;
2022-12-05 09:16:37 -05:00
CachedOwner = Context . GetOwner ( ) ;
}
2023-01-23 12:48:04 -05:00
void UStateTreeNodeBlueprintBase : : ClearCachedInstanceData ( ) const
2022-12-05 09:16:37 -05:00
{
2023-01-23 12:48:04 -05:00
InstanceStorage = nullptr ;
CachedState = FStateTreeStateHandle : : Invalid ;
2022-12-05 09:16:37 -05:00
CachedOwner = nullptr ;
}
void UStateTreeNodeBlueprintBase : : SendEvent ( const FStateTreeEvent & Event )
{
2023-01-23 12:48:04 -05:00
if ( InstanceStorage = = nullptr | | CachedOwner = = nullptr )
2022-12-05 09:16:37 -05:00
{
UE_VLOG_UELOG ( this , LogStateTree , Error , TEXT ( " Trying to call SendEvent() while node is not active. Use SendEvent() on UStateTreeComponent instead for sending signals externally. " ) ) ;
return ;
}
2023-01-23 12:48:04 -05:00
InstanceStorage - > GetMutableEventQueue ( ) . SendEvent ( CachedOwner , Event . Tag , Event . Payload , Event . Origin ) ;
2022-09-01 09:06:53 -04:00
}
2022-09-28 01:06:15 -04:00
2023-01-23 12:48:04 -05:00
void UStateTreeNodeBlueprintBase : : RequestTransition ( const FStateTreeStateLink & TargetState , const EStateTreeTransitionPriority Priority )
{
if ( InstanceStorage = = nullptr | | CachedOwner = = nullptr )
{
UE_VLOG_UELOG ( this , LogStateTree , Error , TEXT ( " Trying to call SendEvent() while node is not active. Use SendEvent() on UStateTreeComponent instead for sending signals externally. " ) ) ;
return ;
}
FStateTreeTransitionRequest Request ;
Request . SourceState = CachedState ;
Request . TargetState = TargetState . StateHandle ;
Request . Priority = Priority ;
InstanceStorage - > AddTransitionRequest ( CachedOwner , Request ) ;
}