2021-09-28 13:33:17 -04:00
|
|
|
// Copyright Epic Games, Inc. All Rights Reserved.
|
|
|
|
|
|
|
|
|
|
#include "StateTreeState.h"
|
|
|
|
|
#include "StateTree.h"
|
2022-04-28 03:54:07 -04:00
|
|
|
#include "StateTreeEditorData.h"
|
2021-09-28 13:33:17 -04:00
|
|
|
#include "StateTreeConditionBase.h"
|
2022-02-03 09:13:49 -05:00
|
|
|
#include "StateTreeEvaluatorBase.h"
|
|
|
|
|
#include "StateTreeTaskBase.h"
|
2021-09-28 13:33:17 -04:00
|
|
|
#include "StateTreeDelegates.h"
|
|
|
|
|
#include "CoreMinimal.h"
|
|
|
|
|
#include "UObject/Field.h"
|
|
|
|
|
|
2022-09-28 01:06:15 -04:00
|
|
|
#include UE_INLINE_GENERATED_CPP_BY_NAME(StateTreeState)
|
|
|
|
|
|
2021-10-21 04:08:20 -04:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// FStateTreeStateLink
|
|
|
|
|
|
2021-09-28 13:33:17 -04:00
|
|
|
void FStateTreeStateLink::Set(const EStateTreeTransitionType InType, const UStateTreeState* InState)
|
|
|
|
|
{
|
|
|
|
|
Type = InType;
|
|
|
|
|
if (Type == EStateTreeTransitionType::GotoState)
|
|
|
|
|
{
|
|
|
|
|
check(InState);
|
|
|
|
|
Name = InState->Name;
|
|
|
|
|
ID = InState->ID;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-10-21 04:08:20 -04:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// FStateTreeTransition
|
|
|
|
|
|
2022-09-01 09:06:53 -04:00
|
|
|
FStateTreeTransition::FStateTreeTransition(const EStateTreeTransitionTrigger InTrigger, const EStateTreeTransitionType InType, const UStateTreeState* InState)
|
|
|
|
|
: Trigger(InTrigger)
|
|
|
|
|
{
|
|
|
|
|
State.Set(InType, InState);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FStateTreeTransition::FStateTreeTransition(const EStateTreeTransitionTrigger InTrigger, const FGameplayTag InEventTag, const EStateTreeTransitionType InType, const UStateTreeState* InState)
|
|
|
|
|
: Trigger(InTrigger)
|
|
|
|
|
, EventTag(InEventTag)
|
2021-09-28 13:33:17 -04:00
|
|
|
{
|
|
|
|
|
State.Set(InType, InState);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-10-21 04:08:20 -04:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// UStateTreeState
|
|
|
|
|
|
2021-09-28 13:33:17 -04:00
|
|
|
UStateTreeState::UStateTreeState(const FObjectInitializer& ObjectInitializer)
|
|
|
|
|
: Super(ObjectInitializer)
|
|
|
|
|
, ID(FGuid::NewGuid())
|
|
|
|
|
{
|
2022-04-28 09:47:16 -04:00
|
|
|
Parameters.ID = FGuid::NewGuid();
|
2021-09-28 13:33:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if WITH_EDITOR
|
|
|
|
|
void UStateTreeState::PostEditChangeChainProperty(FPropertyChangedChainEvent& PropertyChangedEvent)
|
|
|
|
|
{
|
|
|
|
|
Super::PostEditChangeChainProperty(PropertyChangedEvent);
|
|
|
|
|
|
|
|
|
|
FProperty* Property = PropertyChangedEvent.Property;
|
|
|
|
|
FProperty* MemberProperty = nullptr;
|
|
|
|
|
if (PropertyChangedEvent.PropertyChain.GetActiveMemberNode())
|
|
|
|
|
{
|
|
|
|
|
MemberProperty = PropertyChangedEvent.PropertyChain.GetActiveMemberNode()->GetValue();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Property)
|
|
|
|
|
{
|
2022-04-05 03:20:57 -04:00
|
|
|
if (Property->GetOwnerClass() == UStateTreeState::StaticClass()
|
|
|
|
|
&& Property->GetFName() == GET_MEMBER_NAME_CHECKED(UStateTreeState, Name))
|
2021-09-28 13:33:17 -04:00
|
|
|
{
|
|
|
|
|
UStateTree* StateTree = GetTypedOuter<UStateTree>();
|
|
|
|
|
if (ensure(StateTree))
|
|
|
|
|
{
|
|
|
|
|
UE::StateTree::Delegates::OnIdentifierChanged.Broadcast(*StateTree);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-05 03:20:57 -04:00
|
|
|
if (Property->GetOwnerClass() == UStateTreeState::StaticClass()
|
|
|
|
|
&& Property->GetFName() == GET_MEMBER_NAME_CHECKED(UStateTreeState, Type))
|
|
|
|
|
{
|
2022-04-28 03:54:07 -04:00
|
|
|
// Remove any tasks and evaluators when they are not used.
|
2022-04-05 03:20:57 -04:00
|
|
|
if (Type == EStateTreeStateType::Group || Type == EStateTreeStateType::Linked)
|
|
|
|
|
{
|
|
|
|
|
Tasks.Reset();
|
|
|
|
|
}
|
2022-04-28 03:54:07 -04:00
|
|
|
|
|
|
|
|
// If transitioning from linked state, reset the linked state.
|
2022-04-05 03:20:57 -04:00
|
|
|
if (Type != EStateTreeStateType::Linked)
|
|
|
|
|
{
|
|
|
|
|
LinkedState = FStateTreeStateLink();
|
|
|
|
|
}
|
2022-04-28 03:54:07 -04:00
|
|
|
|
|
|
|
|
if (Type == EStateTreeStateType::Linked)
|
|
|
|
|
{
|
|
|
|
|
// Linked parameter layout is fixed, and copied from the linked target state.
|
|
|
|
|
Parameters.bFixedLayout = true;
|
|
|
|
|
UpdateParametersFromLinkedState();
|
|
|
|
|
}
|
|
|
|
|
else if (Type == EStateTreeStateType::Subtree)
|
|
|
|
|
{
|
|
|
|
|
// Subtree parameter layout can be edited
|
|
|
|
|
Parameters.bFixedLayout = false;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Parameters.Reset();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Property->GetOwnerClass() == UStateTreeState::StaticClass()
|
|
|
|
|
&& Property->GetFName() == GET_MEMBER_NAME_CHECKED(UStateTreeState, LinkedState))
|
|
|
|
|
{
|
|
|
|
|
// When switching to new state, update the parameters.
|
|
|
|
|
if (Type == EStateTreeStateType::Linked)
|
|
|
|
|
{
|
|
|
|
|
UpdateParametersFromLinkedState();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Property->GetOwnerClass() == UStateTreeState::StaticClass()
|
|
|
|
|
&& Property->GetFName() == GET_MEMBER_NAME_CHECKED(UStateTreeState, Parameters))
|
|
|
|
|
{
|
|
|
|
|
if (Type == EStateTreeStateType::Subtree)
|
|
|
|
|
{
|
|
|
|
|
// Broadcast subtree parameter edits so that the linked states can adapt.
|
|
|
|
|
const UStateTree* StateTree = GetTypedOuter<UStateTree>();
|
|
|
|
|
if (ensure(StateTree))
|
|
|
|
|
{
|
|
|
|
|
UE::StateTree::Delegates::OnStateParametersChanged.Broadcast(*StateTree, ID);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-04-05 03:20:57 -04:00
|
|
|
}
|
|
|
|
|
|
2021-10-21 04:08:20 -04:00
|
|
|
if (MemberProperty)
|
2021-09-28 13:33:17 -04:00
|
|
|
{
|
|
|
|
|
// Ensure unique ID on duplicated items.
|
2021-10-21 04:08:20 -04:00
|
|
|
if (PropertyChangedEvent.ChangeType == EPropertyChangeType::Duplicate)
|
2021-09-28 13:33:17 -04:00
|
|
|
{
|
2021-10-21 04:08:20 -04:00
|
|
|
if (MemberProperty->GetFName() == GET_MEMBER_NAME_CHECKED(UStateTreeState, Tasks))
|
|
|
|
|
{
|
|
|
|
|
const int32 ArrayIndex = PropertyChangedEvent.GetArrayIndex(MemberProperty->GetFName().ToString());
|
|
|
|
|
if (Tasks.IsValidIndex(ArrayIndex))
|
|
|
|
|
{
|
2022-02-03 09:13:49 -05:00
|
|
|
if (FStateTreeTaskBase* Task = Tasks[ArrayIndex].Node.GetMutablePtr<FStateTreeTaskBase>())
|
2021-10-21 04:08:20 -04:00
|
|
|
{
|
|
|
|
|
Task->Name = FName(Task->Name.ToString() + TEXT(" Duplicate"));
|
|
|
|
|
}
|
2021-11-12 05:48:11 -05:00
|
|
|
Tasks[ArrayIndex].ID = FGuid::NewGuid();
|
2021-10-21 04:08:20 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (MemberProperty->GetFName() == GET_MEMBER_NAME_CHECKED(UStateTreeState, EnterConditions))
|
|
|
|
|
{
|
|
|
|
|
const int32 ArrayIndex = PropertyChangedEvent.GetArrayIndex(MemberProperty->GetFName().ToString());
|
|
|
|
|
if (EnterConditions.IsValidIndex(ArrayIndex))
|
|
|
|
|
{
|
2021-11-12 05:48:11 -05:00
|
|
|
EnterConditions[ArrayIndex].ID = FGuid::NewGuid();
|
2021-10-21 04:08:20 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// TODO: Transition conditions.
|
2022-04-21 08:02:21 -04:00
|
|
|
|
2021-09-28 13:33:17 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-21 08:02:21 -04:00
|
|
|
void UStateTreeState::PostLoad()
|
|
|
|
|
{
|
|
|
|
|
Super::PostLoad();
|
|
|
|
|
|
|
|
|
|
// Make sure state has transactional flags to make it work with undo (to fix a bug where root states were created without this flag).
|
|
|
|
|
if (!HasAnyFlags(RF_Transactional))
|
|
|
|
|
{
|
|
|
|
|
SetFlags(RF_Transactional);
|
|
|
|
|
}
|
2022-05-16 05:13:27 -04:00
|
|
|
|
2022-09-01 09:06:53 -04:00
|
|
|
#if WITH_EDITORONLY_DATA
|
2022-05-16 05:13:27 -04:00
|
|
|
PRAGMA_DISABLE_DEPRECATION_WARNINGS
|
2022-09-01 09:06:53 -04:00
|
|
|
// Move deprecated evaluators to editor data.
|
2022-05-16 05:13:27 -04:00
|
|
|
if (Evaluators_DEPRECATED.Num() > 0)
|
|
|
|
|
{
|
|
|
|
|
if (UStateTreeEditorData* TreeData = GetTypedOuter<UStateTreeEditorData>())
|
|
|
|
|
{
|
|
|
|
|
TreeData->Evaluators.Append(Evaluators_DEPRECATED);
|
|
|
|
|
Evaluators_DEPRECATED.Reset();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
PRAGMA_ENABLE_DEPRECATION_WARNINGS
|
2022-09-01 09:06:53 -04:00
|
|
|
#endif
|
2022-04-21 08:02:21 -04:00
|
|
|
}
|
|
|
|
|
|
2022-04-28 03:54:07 -04:00
|
|
|
void UStateTreeState::UpdateParametersFromLinkedState()
|
|
|
|
|
{
|
|
|
|
|
if (const UStateTreeEditorData* TreeData = GetTypedOuter<UStateTreeEditorData>())
|
|
|
|
|
{
|
|
|
|
|
if (const UStateTreeState* LinkTargetState = TreeData->GetStateByID(LinkedState.ID))
|
|
|
|
|
{
|
|
|
|
|
Parameters.Parameters.MigrateToNewBagInstance(LinkTargetState->Parameters.Parameters);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-04-21 08:02:21 -04:00
|
|
|
|
2021-09-28 13:33:17 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
UStateTreeState* UStateTreeState::GetNextSiblingState() const
|
|
|
|
|
{
|
|
|
|
|
if (!Parent)
|
|
|
|
|
{
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
for (int32 ChildIdx = 0; ChildIdx < Parent->Children.Num(); ChildIdx++)
|
|
|
|
|
{
|
|
|
|
|
if (Parent->Children[ChildIdx] == this)
|
|
|
|
|
{
|
|
|
|
|
const int NextIdx = ChildIdx + 1;
|
|
|
|
|
if (NextIdx < Parent->Children.Num())
|
|
|
|
|
{
|
|
|
|
|
return Parent->Children[NextIdx];
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2022-09-28 01:06:15 -04:00
|
|
|
|