2022-05-31 04:51:18 -04:00
|
|
|
// Copyright Epic Games, Inc. All Rights Reserved.
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include "Engine/DataAsset.h"
|
|
|
|
|
#include "StateTreeSchema.h"
|
|
|
|
|
#include "StateTreePropertyBindings.h"
|
|
|
|
|
#include "StateTreeInstanceData.h"
|
|
|
|
|
#include "StateTree.generated.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Custom serialization version for StateTree Asset */
|
|
|
|
|
struct STATETREEMODULE_API FStateTreeCustomVersion
|
|
|
|
|
{
|
|
|
|
|
enum Type
|
|
|
|
|
{
|
|
|
|
|
// Before any version changes were made in the plugin
|
|
|
|
|
BeforeCustomVersionWasAdded = 0,
|
|
|
|
|
// Separated conditions to shared instance data.
|
|
|
|
|
SharedInstanceData,
|
|
|
|
|
// Moved evaluators to be global.
|
|
|
|
|
GlobalEvaluators,
|
|
|
|
|
// Moved instance data to arrays.
|
|
|
|
|
InstanceDataArrays,
|
|
|
|
|
// Added index types.
|
|
|
|
|
IndexTypes,
|
2022-09-01 09:06:53 -04:00
|
|
|
// Added events.
|
|
|
|
|
AddedEvents,
|
2022-12-02 07:57:31 -05:00
|
|
|
// Testing mishap
|
2022-09-19 19:47:11 -04:00
|
|
|
AddedFoo,
|
2022-12-02 07:57:31 -05:00
|
|
|
// Changed transition delay
|
|
|
|
|
TransitionDelay,
|
2023-01-23 12:48:04 -05:00
|
|
|
// Added external transitions
|
|
|
|
|
AddedExternalTransitions,
|
2023-02-10 07:22:48 -05:00
|
|
|
// Changed how bindings are represented
|
|
|
|
|
ChangedBindingsRepresentation,
|
2022-05-31 04:51:18 -04:00
|
|
|
|
|
|
|
|
// -----<new versions can be added above this line>-------------------------------------------------
|
|
|
|
|
VersionPlusOne,
|
|
|
|
|
LatestVersion = VersionPlusOne - 1
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/** The GUID for this custom version number */
|
|
|
|
|
const static FGuid GUID;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
FStateTreeCustomVersion() {}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#if WITH_EDITOR
|
|
|
|
|
/** Struct containing information about the StateTree runtime memory usage. */
|
|
|
|
|
struct FStateTreeMemoryUsage
|
|
|
|
|
{
|
|
|
|
|
FStateTreeMemoryUsage() = default;
|
|
|
|
|
FStateTreeMemoryUsage(const FString InName, const FStateTreeStateHandle InHandle = FStateTreeStateHandle::Invalid)
|
|
|
|
|
: Name(InName)
|
|
|
|
|
, Handle(InHandle)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AddUsage(FConstStructView View);
|
|
|
|
|
void AddUsage(const UObject* Object);
|
|
|
|
|
|
|
|
|
|
FString Name;
|
|
|
|
|
FStateTreeStateHandle Handle;
|
|
|
|
|
int32 NodeCount = 0;
|
|
|
|
|
int32 EstimatedMemoryUsage = 0;
|
|
|
|
|
int32 ChildNodeCount = 0;
|
|
|
|
|
int32 EstimatedChildMemoryUsage = 0;
|
|
|
|
|
};
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* StateTree asset. Contains the StateTree definition in both editor and runtime (baked) formats.
|
|
|
|
|
*/
|
|
|
|
|
UCLASS(BlueprintType)
|
|
|
|
|
class STATETREEMODULE_API UStateTree : public UDataAsset
|
|
|
|
|
{
|
|
|
|
|
GENERATED_BODY()
|
|
|
|
|
|
|
|
|
|
public:
|
2022-09-07 17:12:18 -04:00
|
|
|
/** @return Shared instance data. */
|
|
|
|
|
TSharedPtr<FStateTreeInstanceData> GetSharedInstanceData() const;
|
2022-05-31 04:51:18 -04:00
|
|
|
|
|
|
|
|
/** @return Number of data views required for StateTree execution (Evaluators, Tasks, Conditions, External data). */
|
|
|
|
|
int32 GetNumDataViews() const { return NumDataViews; }
|
|
|
|
|
|
|
|
|
|
/** @return List of external data required by the state tree */
|
|
|
|
|
TConstArrayView<FStateTreeExternalDataDesc> GetExternalDataDescs() const { return ExternalDataDescs; }
|
|
|
|
|
|
2022-09-19 19:47:11 -04:00
|
|
|
/** @return List of context data enforced by the schema that must be provided through the execution context. */
|
|
|
|
|
TConstArrayView<FStateTreeExternalDataDesc> GetContextDataDescs() const { return ContextDataDescs; }
|
2022-05-31 04:51:18 -04:00
|
|
|
|
|
|
|
|
/** @return List of default parameters of the state tree. Default parameter values can be overridden at runtime by the execution context. */
|
|
|
|
|
const FInstancedPropertyBag& GetDefaultParameters() const { return Parameters; }
|
|
|
|
|
|
|
|
|
|
/** @return true if the tree asset can be used at runtime. */
|
|
|
|
|
bool IsReadyToRun() const;
|
|
|
|
|
|
2022-08-29 14:47:43 -04:00
|
|
|
/** @return schema that was used to compile the StateTree. */
|
|
|
|
|
const UStateTreeSchema* GetSchema() const { return Schema; }
|
|
|
|
|
|
2023-03-14 13:35:46 -04:00
|
|
|
/** @return Pointer to a state or null if state not found */
|
|
|
|
|
const FCompactStateTreeState* GetStateFromHandle(const FStateTreeStateHandle StateHandle) const;
|
|
|
|
|
|
|
|
|
|
/** @return State handle matching a given Id; invalid handle if state not found. */
|
|
|
|
|
FStateTreeStateHandle GetStateHandleFromId(const FGuid Id) const;
|
|
|
|
|
|
|
|
|
|
/** @return Id of the state matching a given state handle; invalid Id if state not found. */
|
2023-03-15 11:59:44 -04:00
|
|
|
FGuid GetStateIdFromHandle(const FStateTreeStateHandle Handle) const;
|
2023-03-14 13:35:46 -04:00
|
|
|
|
2023-03-15 11:59:44 -04:00
|
|
|
/** @return Struct view of the node matching a given node index; invalid view if state not found. */
|
|
|
|
|
FConstStructView GetNode(const int32 NodeIndex) const;
|
2023-03-14 13:35:46 -04:00
|
|
|
|
|
|
|
|
/** @return View of all states. */
|
|
|
|
|
TConstArrayView<FCompactStateTreeState> GetStates() const { return States; }
|
|
|
|
|
|
2022-05-31 04:51:18 -04:00
|
|
|
#if WITH_EDITOR
|
|
|
|
|
/** Resets the compiled data to empty. */
|
|
|
|
|
void ResetCompiled();
|
|
|
|
|
|
|
|
|
|
/** Calculates runtime memory usage for different sections of the tree. */
|
|
|
|
|
TArray<FStateTreeMemoryUsage> CalculateEstimatedMemoryUsage() const;
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#if WITH_EDITORONLY_DATA
|
|
|
|
|
/** Edit time data for the StateTree, instance of UStateTreeEditorData */
|
|
|
|
|
UPROPERTY()
|
|
|
|
|
TObjectPtr<UObject> EditorData;
|
2023-04-14 16:36:43 -04:00
|
|
|
|
|
|
|
|
FDelegateHandle OnObjectsReinstancedHandle;
|
2023-03-14 13:35:46 -04:00
|
|
|
#endif
|
2022-05-31 04:51:18 -04:00
|
|
|
|
2023-03-14 13:35:46 -04:00
|
|
|
/** Hash of the editor data from last compile. Also used to detect mismatching events from recorded traces. */
|
2022-05-31 04:51:18 -04:00
|
|
|
UPROPERTY()
|
|
|
|
|
uint32 LastCompiledEditorDataHash = 0;
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Resolves references between data in the StateTree.
|
|
|
|
|
* @return true if all references to internal and external data are resolved properly, false otherwise.
|
|
|
|
|
*/
|
|
|
|
|
[[nodiscard]] bool Link();
|
|
|
|
|
|
|
|
|
|
virtual void PostLoad() override;
|
2022-11-25 15:40:13 -05:00
|
|
|
#if WITH_EDITORONLY_DATA
|
|
|
|
|
static void DeclareConstructClasses(TArray<FTopLevelAssetPath>& OutConstructClasses, const UClass* SpecificSubclass);
|
|
|
|
|
#endif
|
2022-05-31 04:51:18 -04:00
|
|
|
virtual void Serialize(FStructuredArchiveRecord Record) override;
|
|
|
|
|
|
|
|
|
|
#if WITH_EDITOR
|
2023-04-14 16:36:43 -04:00
|
|
|
using FReplacementObjectMap = TMap<UObject*, UObject*>;
|
|
|
|
|
void OnObjectsReinstanced(const FReplacementObjectMap& ObjectMap);
|
|
|
|
|
virtual void PostInitProperties() override;
|
|
|
|
|
virtual void BeginDestroy() override;
|
2022-05-31 04:51:18 -04:00
|
|
|
virtual void GetAssetRegistryTags(TArray<FAssetRegistryTag>& OutTags) const override;
|
Deprecating ANY_PACKAGE.
This change consists of multiple changes:
Core:
- Deprecation of ANY_PACKAGE macro. Added ANY_PACKAGE_DEPRECATED macro which can still be used for backwards compatibility purposes (only used in CoreUObject)
- Deprecation of StaticFindObjectFast* functions that take bAnyPackage parameter
- Added UStruct::GetStructPathName function that returns FTopLevelAssetPath representing the path name (package + object FName, super quick compared to UObject::GetPathName) + wrapper UClass::GetClassPathName to make it look better when used with UClasses
- Added (Static)FindFirstObject* functions that find a first object given its Name (no Outer). These functions are used in places I consider valid to do global UObject (UClass) lookups like parsing command line parameters / checking for unique object names
- Added static UClass::TryFindType function which serves a similar purpose as FindFirstObject however it's going to throw a warning (with a callstack / maybe ensure in the future?) if short class name is provided. This function is used in places that used to use short class names but now should have been converted to use path names to catch any potential regressions and or edge cases I missed.
- Added static UClass::TryConvertShortNameToPathName utility function
- Added static UClass::TryFixShortClassNameExportPath utility function
- Object text export paths will now also include class path (Texture2D'/Game/Textures/Grass.Grass' -> /Script/Engine.Texture2D'/Game/Textures/Grass.Grass')
- All places that manually generated object export paths for objects will now use FObjectPropertyBase::GetExportPath
- Added a new startup test that checks for short type names in UClass/FProperty MetaData values
AssetRegistry:
- Deprecated any member variables (FAssetData / FARFilter) or functions that use FNames to represent class names and replaced them with FTopLevelAssetPath
- Added new member variables and new function overloads that use FTopLevelAssetPath to represent class names
- This also applies to a few other modules' APIs to match AssetRegistry changes
Everything else:
- Updated code that used ANY_PACKAGE (depending on the use case) to use FindObject(nullptr, PathToObject), UClass::TryFindType (used when path name is expected, warns if it's a short name) or FindFirstObject (usually for finding types based on user input but there's been a few legitimate use cases not related to user input)
- Updated code that used AssetRegistry API to use FTopLevelAssetPaths and USomeClass::StaticClass()->GetClassPathName() instead of GetFName()
- Updated meta data and hardcoded FindObject(ANY_PACKAGE, "EEnumNameOrClassName") calls to use path names
#jira UE-99463
#rb many.people
[FYI] Marcus.Wassmer
#preflight 629248ec2256738f75de9b32
#codereviewnumbers 20320742, 20320791, 20320799, 20320756, 20320809, 20320830, 20320840, 20320846, 20320851, 20320863, 20320780, 20320765, 20320876, 20320786
#ROBOMERGE-OWNER: robert.manuszewski
#ROBOMERGE-AUTHOR: robert.manuszewski
#ROBOMERGE-SOURCE: CL 20430220 via CL 20433854 via CL 20435474 via CL 20435484
#ROBOMERGE-BOT: UE5 (Release-Engine-Staging -> Main) (v949-20362246)
[CL 20448496 by robert manuszewski in ue5-main branch]
2022-06-01 03:46:59 -04:00
|
|
|
virtual void PostLoadAssetRegistryTags(const FAssetData& InAssetData, TArray<FAssetRegistryTag>& OutTagsAndValuesToUpdate) const;
|
2022-05-31 04:51:18 -04:00
|
|
|
#endif
|
|
|
|
|
|
2022-09-24 13:28:37 -04:00
|
|
|
static void AddReferencedObjects(UObject* InThis, FReferenceCollector& Collector);
|
|
|
|
|
|
2022-05-31 04:51:18 -04:00
|
|
|
private:
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Reset the data generated by Link(), this in turn will cause IsReadyToRun() to return false.
|
|
|
|
|
* Used during linking, or to invalidate the linked data when data version is old (requires recompile).
|
|
|
|
|
*/
|
|
|
|
|
void ResetLinked();
|
|
|
|
|
|
|
|
|
|
// Data created during compilation, source data in EditorData.
|
|
|
|
|
|
|
|
|
|
/** Schema used to compile the StateTree. */
|
|
|
|
|
UPROPERTY(Instanced)
|
|
|
|
|
TObjectPtr<UStateTreeSchema> Schema = nullptr;
|
|
|
|
|
|
|
|
|
|
/** Runtime states, root state at index 0 */
|
|
|
|
|
UPROPERTY()
|
|
|
|
|
TArray<FCompactStateTreeState> States;
|
|
|
|
|
|
|
|
|
|
/** Runtime transitions. */
|
|
|
|
|
UPROPERTY()
|
|
|
|
|
TArray<FCompactStateTransition> Transitions;
|
|
|
|
|
|
|
|
|
|
/** Evaluators, Tasks, and Condition nodes. */
|
|
|
|
|
UPROPERTY()
|
2022-12-05 09:16:27 -05:00
|
|
|
FInstancedStructContainer Nodes;
|
2022-05-31 04:51:18 -04:00
|
|
|
|
|
|
|
|
/** Default node instance data (e.g. evaluators, tasks). */
|
|
|
|
|
UPROPERTY()
|
|
|
|
|
FStateTreeInstanceData DefaultInstanceData;
|
|
|
|
|
|
|
|
|
|
/** Shared node instance data (e.g. conditions). */
|
|
|
|
|
UPROPERTY()
|
|
|
|
|
FStateTreeInstanceData SharedInstanceData;
|
|
|
|
|
|
2022-09-07 17:12:18 -04:00
|
|
|
mutable FRWLock PerThreadSharedInstanceDataLock;
|
|
|
|
|
mutable TArray<TSharedPtr<FStateTreeInstanceData>> PerThreadSharedInstanceData;
|
|
|
|
|
|
2022-05-31 04:51:18 -04:00
|
|
|
/** List of names external data enforced by the schema, created at compilation. */
|
|
|
|
|
UPROPERTY()
|
2022-09-19 19:47:11 -04:00
|
|
|
TArray<FStateTreeExternalDataDesc> ContextDataDescs;
|
2022-05-31 04:51:18 -04:00
|
|
|
|
|
|
|
|
UPROPERTY()
|
|
|
|
|
FStateTreePropertyBindings PropertyBindings;
|
|
|
|
|
|
2023-03-14 13:35:46 -04:00
|
|
|
/** Mapping of state guid for the Editor and state handles, created at compilation. */
|
|
|
|
|
UPROPERTY()
|
|
|
|
|
TArray<FStateTreeStateIdToHandle> IDToStateMappings;
|
|
|
|
|
|
2022-05-31 04:51:18 -04:00
|
|
|
/**
|
|
|
|
|
* Parameters that could be used for bindings within the Tree.
|
|
|
|
|
* Default values are stored within the asset but StateTreeReference can be used to parameterized the tree.
|
|
|
|
|
* @see FStateTreeReference
|
|
|
|
|
*/
|
|
|
|
|
UPROPERTY()
|
|
|
|
|
FInstancedPropertyBag Parameters;
|
|
|
|
|
|
|
|
|
|
/** Data view index of the tree Parameters */
|
|
|
|
|
UPROPERTY()
|
|
|
|
|
FStateTreeIndex8 ParametersDataViewIndex = FStateTreeIndex8::Invalid;
|
|
|
|
|
|
|
|
|
|
/** Index of first evaluator in Nodes. */
|
|
|
|
|
UPROPERTY()
|
|
|
|
|
uint16 EvaluatorsBegin = 0;
|
|
|
|
|
|
|
|
|
|
/** Number of evaluators. */
|
|
|
|
|
UPROPERTY()
|
|
|
|
|
uint16 EvaluatorsNum = 0;
|
|
|
|
|
|
2023-01-10 15:44:28 -05:00
|
|
|
/** Index of first global task in Nodes. */
|
|
|
|
|
UPROPERTY()
|
|
|
|
|
uint16 GlobalTasksBegin = 0;
|
|
|
|
|
|
|
|
|
|
/** Number of global tasks. */
|
|
|
|
|
UPROPERTY()
|
|
|
|
|
uint16 GlobalTasksNum = 0;
|
|
|
|
|
|
2023-01-23 12:48:04 -05:00
|
|
|
/** True if any global task is a transition task. */
|
|
|
|
|
UPROPERTY()
|
|
|
|
|
bool bHasGlobalTransitionTasks = false;
|
|
|
|
|
|
2022-05-31 04:51:18 -04:00
|
|
|
// Data created during linking.
|
|
|
|
|
|
|
|
|
|
/** List of external data required by the state tree, created during linking. */
|
|
|
|
|
UPROPERTY(Transient)
|
|
|
|
|
TArray<FStateTreeExternalDataDesc> ExternalDataDescs;
|
|
|
|
|
|
|
|
|
|
/** Base index of external data, created during linking. */
|
|
|
|
|
UPROPERTY(Transient)
|
|
|
|
|
int32 ExternalDataBaseIndex = 0;
|
|
|
|
|
|
|
|
|
|
/** Total number of data views, created during linking. */
|
|
|
|
|
UPROPERTY(Transient)
|
|
|
|
|
int32 NumDataViews = 0;
|
|
|
|
|
|
|
|
|
|
/** True if the StateTree was linked successfully. */
|
|
|
|
|
bool bIsLinked = false;
|
|
|
|
|
|
|
|
|
|
friend struct FStateTreeInstance;
|
|
|
|
|
friend struct FStateTreeExecutionContext;
|
|
|
|
|
#if WITH_EDITORONLY_DATA
|
|
|
|
|
friend struct FStateTreeCompiler;
|
|
|
|
|
#endif
|
|
|
|
|
};
|
|
|
|
|
|
2023-01-25 02:42:36 -05:00
|
|
|
|
|
|
|
|
#if UE_ENABLE_INCLUDE_ORDER_DEPRECATED_IN_5_2
|
|
|
|
|
#include "Misc/ScopeRWLock.h"
|
|
|
|
|
#endif
|