2022-04-12 15:55:39 -04:00
// Copyright Epic Games, Inc. All Rights Reserved.
# include "StateTreeReference.h"
# include "StateTree.h"
2022-05-10 10:15:17 -04:00
2022-04-12 15:55:39 -04:00
# if WITH_EDITOR
# include "Editor.h"
# endif
2022-05-10 10:15:17 -04:00
FStateTreeReference : : FStateTreeReference ( )
2022-04-12 15:55:39 -04:00
{
2022-05-10 10:15:17 -04:00
# 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 )
2022-04-12 15:55:39 -04:00
{
2022-05-10 10:15:17 -04:00
if ( RequiresParametersSync ( ) )
2022-04-12 15:55:39 -04:00
{
2022-05-10 10:15:17 -04:00
SyncParameters ( ) ;
UE_LOG ( LogStateTree , Warning , TEXT ( " Parameters for '%s' stored in StateTreeReference were auto-fixed to be usable at runtime. " ) , * GetNameSafe ( StateTree ) ) ;
2022-04-12 15:55:39 -04:00
}
2022-05-10 10:15:17 -04:00
} ) ;
# endif
}
FStateTreeReference : : ~ FStateTreeReference ( )
{
# if WITH_EDITOR
// Unregister all our delegates
FEditorDelegates : : PreBeginPIE . Remove ( PIEHandle ) ;
# endif
}
# if WITH_EDITOR
void FStateTreeReference : : PostSerialize ( const FArchive & Ar )
{
2022-05-17 13:18:02 -04:00
if ( Ar . IsLoading ( ) & & Ar . IsPersistent ( ) )
2022-05-10 10:15:17 -04:00
{
// This might modify the object but we don't want to dirty on load
SyncParameters ( ) ;
2022-04-12 15:55:39 -04:00
}
}
2022-05-10 10:15:17 -04:00
void FStateTreeReference : : SyncParameters ( FInstancedPropertyBag & ParametersToSync ) const
2022-04-12 15:55:39 -04:00
{
if ( StateTree = = nullptr )
{
2022-05-10 10:15:17 -04:00
ParametersToSync . Reset ( ) ;
2022-04-12 15:55:39 -04:00
}
2022-05-10 10:15:17 -04:00
else
2022-04-12 15:55:39 -04:00
{
2022-05-10 10:15:17 -04:00
ParametersToSync . MigrateToNewBagInstance ( StateTree - > GetDefaultParameters ( ) ) ;
2022-04-12 15:55:39 -04:00
}
2022-05-10 10:15:17 -04:00
}
2022-04-12 15:55:39 -04:00
2022-05-10 10:15:17 -04:00
bool FStateTreeReference : : RequiresParametersSync ( ) const
{
return ( StateTree = = nullptr & & Parameters . IsValid ( ) )
| | ( StateTree ! = nullptr & & StateTree - > GetDefaultParameters ( ) . GetPropertyBagStruct ( ) ! = Parameters . GetPropertyBagStruct ( ) ) ;
2022-04-12 15:55:39 -04:00
}
# endif // WITH_EDITOR