Files
UnrealEngineUWP/Engine/Plugins/Runtime/StateTree/Source/StateTreeModule/Private/StateTreeReference.cpp
mikko mononen 68a3b6aa67 StateTree: Made StateTreeComponent to use StateTreeRef, and fixed StateTreeRef on Actor Components
#preflight 62da4a10185da2495f802bae

#ROBOMERGE-AUTHOR: mikko.mononen
#ROBOMERGE-SOURCE: CL 21217415 via CL 21217580 via CL 21217683
#ROBOMERGE-BOT: UE5 (Release-Engine-Staging -> Main) (v972-20964824)

[CL 21218420 by mikko mononen in ue5-main branch]
2022-07-22 05:33:11 -04:00

68 lines
1.7 KiB
C++

// Copyright Epic Games, Inc. All Rights Reserved.
#include "StateTreeReference.h"
#include "StateTree.h"
#if WITH_EDITOR
#include "Editor.h"
#endif
FStateTreeReference::FStateTreeReference()
{
#if WITH_EDITOR
// Make sure any modifications to the parameters since the last sync (PostSerialize) won't cause runtime issues.
PIEHandle = FEditorDelegates::PreBeginPIE.AddLambda([this](const bool bBegan)
{
if (RequiresParametersSync())
{
SyncParameters();
UE_LOG(LogStateTree, Warning, TEXT("Parameters for '%s' stored in StateTreeReference were auto-fixed to be usable at runtime."), *GetNameSafe(StateTree));
}
});
#endif
}
FStateTreeReference::~FStateTreeReference()
{
#if WITH_EDITOR
// Unregister all our delegates
FEditorDelegates::PreBeginPIE.Remove(PIEHandle);
#endif
}
#if WITH_EDITOR
void FStateTreeReference::PostSerialize(const FArchive& Ar)
{
if (Ar.IsLoading() && Ar.IsPersistent())
{
// Make sure the StateTree asset is fully loaded, so that the SyncParameters() will execute on valid data.
if (StateTree != nullptr)
{
FArchive* NonConstAr = const_cast<FArchive*>(&Ar);
NonConstAr->Preload(StateTree);
}
// This might modify the object but we don't want to dirty on load
SyncParameters();
}
}
void FStateTreeReference::SyncParameters(FInstancedPropertyBag& ParametersToSync) const
{
if (StateTree == nullptr)
{
ParametersToSync.Reset();
}
else
{
ParametersToSync.MigrateToNewBagInstance(StateTree->GetDefaultParameters());
}
}
bool FStateTreeReference::RequiresParametersSync() const
{
return (StateTree == nullptr && Parameters.IsValid())
|| (StateTree != nullptr && StateTree->GetDefaultParameters().GetPropertyBagStruct() != Parameters.GetPropertyBagStruct());
}
#endif // WITH_EDITOR