// Copyright Epic Games, Inc. All Rights Reserved. #pragma once #include "Engine/DataAsset.h" #include "GameplayTagContainer.h" #include "Math/Box.h" #include "WorldConditionQuery.h" #include "WorldConditions/SmartObjectWorldConditionSchema.h" #include "SmartObjectDefinition.generated.h" struct FSmartObjectSlotIndex; class UGameplayBehaviorConfig; class USmartObjectSlotValidationFilter; enum class ESmartObjectTagFilteringPolicy: uint8; enum class ESmartObjectTagMergingPolicy: uint8; /** Indicates how Tags from slots and parent object are combined to be evaluated by a TagQuery from a find request. */ UENUM() enum class ESmartObjectSlotShape : uint8 { Circle, Rectangle }; /** * Abstract class that can be extended to bind a new type of behavior framework * to the smart objects by defining the required definition. */ UCLASS(Abstract, NotBlueprintable, EditInlineNew, CollapseCategories, HideDropdown) class SMARTOBJECTSMODULE_API USmartObjectBehaviorDefinition : public UObject { GENERATED_BODY() }; /** * Persistent and sharable definition of a smart object slot. */ USTRUCT() struct SMARTOBJECTSMODULE_API FSmartObjectSlotDefinition { GENERATED_BODY() #if WITH_EDITORONLY_DATA UPROPERTY(EditDefaultsOnly, Category = "SmartObject") FName Name; UPROPERTY(EditAnywhere, Category = "SmartObject", meta = (DisplayName = "Color")) FColor DEBUG_DrawColor = FColor::Yellow; UPROPERTY(EditAnywhere, Category = "SmartObject", meta = (DisplayName = "Shape")) ESmartObjectSlotShape DEBUG_DrawShape = ESmartObjectSlotShape::Circle; UPROPERTY(EditAnywhere, Category = "SmartObject", meta = (DisplayName = "Size")) float DEBUG_DrawSize = 40.0f; UPROPERTY(EditAnywhere, Category = "SmartObject", meta = (Hidden)) FGuid ID; #endif // WITH_EDITORONLY_DATA /** Whether the slot is enable initially. */ UPROPERTY(EditDefaultsOnly, Category = "SmartObject") bool bEnabled = true; /** Initial runtime tags. */ UPROPERTY(EditDefaultsOnly, Category = "SmartObject") FGameplayTagContainer RuntimeTags; /** This slot is available only for users matching this query. */ UPROPERTY(EditDefaultsOnly, Category = "SmartObject") FGameplayTagQuery UserTagFilter; /** * Tags identifying this slot's use case. Can be used while looking for slots supporting given activity. * Depending on the tag filtering policy these tags can override the parent object's tags * or be combined with them while applying filters from requests. */ UPROPERTY(EditDefaultsOnly, Category = "SmartObject") FGameplayTagContainer ActivityTags; /** Preconditions that must pass for the slot to be selected. */ UPROPERTY(EditDefaultsOnly, Category = "SmartObject") FWorldConditionQueryDefinition SelectionPreconditions; /** Offset relative to the parent object where the slot is located. */ UPROPERTY(EditDefaultsOnly, Category = "SmartObject") FVector3f Offset = FVector3f::ZeroVector; /** Rotation relative to the parent object. */ UPROPERTY(EditDefaultsOnly, Category = "SmartObject") FRotator3f Rotation = FRotator3f::ZeroRotator; /** Custom data (struct inheriting from SmartObjectSlotDefinitionData) that can be added to the slot definition and accessed through a FSmartObjectSlotView */ UPROPERTY(EditDefaultsOnly, Category = "SmartObject", meta = (BaseStruct = "/Script/SmartObjectsModule.SmartObjectSlotDefinitionData", ExcludeBaseStruct)) TArray Data; /** * All available definitions associated to this slot. * This allows multiple frameworks to provide their specific behavior definition to the slot. * Note that there should be only one definition of each type since the first one will be selected. */ UPROPERTY(EditDefaultsOnly, Category = "SmartObject", Instanced) TArray> BehaviorDefinitions; }; /** * Data used for previewing in the Smart Object editor. */ USTRUCT() struct SMARTOBJECTSMODULE_API FSmartObjectDefinitionPreviewData { GENERATED_BODY() /** Actor class used as the object for previewing the definition in the asset editor. */ UPROPERTY(EditDefaultsOnly, Category = "Object Preview") TSoftClassPtr ObjectActorClass; /** Path of the static mesh used as the object for previewing the definition in the asset editor. */ UPROPERTY(EditDefaultsOnly, Category = "Object Preview", meta = (AllowedClasses = "/Script/Engine.StaticMesh")) FSoftObjectPath ObjectMeshPath; /** Actor class used for previewing the smart object user actor in the asset editor. */ UPROPERTY(EditDefaultsOnly, Category = "User Preview") TSoftClassPtr UserActorClass; /** Validation filter used for previewing the smart object user in the asset editor. */ UPROPERTY(EditDefaultsOnly, Category = "User Preview") TSoftClassPtr UserValidationFilterClass; }; /** * SmartObject definition asset. Contains sharable information that can be used by multiple SmartObject instances at runtime. */ UCLASS(BlueprintType, Blueprintable, CollapseCategories) class SMARTOBJECTSMODULE_API USmartObjectDefinition : public UDataAsset { GENERATED_BODY() public: explicit USmartObjectDefinition(const FObjectInitializer& ObjectInitializer); /** * Retrieves a specific type of behavior definition for a given slot. * When the slot doesn't provide one or if the provided index is not valid * the search will look in the object default definitions. * * @param SlotIndex Index of the slot for which the definition is requested * @param DefinitionClass Type of the requested behavior definition * @return The behavior definition found or null if none are available for the requested type. */ const USmartObjectBehaviorDefinition* GetBehaviorDefinition(const int32 SlotIndex, const TSubclassOf& DefinitionClass) const; /** @return Preconditions that must pass for the object to be found/used. */ const FWorldConditionQueryDefinition& GetPreconditions() const { return Preconditions; } /** @return mutable Preconditions that must pass for the object to be found/used. */ FWorldConditionQueryDefinition& GetMutablePreconditions() { return Preconditions; } /** @return a view on all the slot definitions */ TConstArrayView GetSlots() const { return Slots; } /** @return slot definition stored at a given index */ const FSmartObjectSlotDefinition& GetSlot(const int32 Index) const { return Slots[Index]; } /** @return mutable slot definition stored at a given index */ FSmartObjectSlotDefinition& GetMutableSlot(const int32 Index) { return Slots[Index]; } /** @return True if specified slot index is valid. */ bool IsValidSlotIndex(const int32 SlotIndex) const { return Slots.IsValidIndex(SlotIndex); } #if WITH_EDITOR /** Returns a view on all the slot definitions */ TArrayView GetMutableSlots() { return Slots; } /** @return validation filter class for preview. */ TSubclassOf GetPreviewValidationFilterClass() const; #endif /** Return bounds encapsulating all slots */ FBox GetBounds() const; /** Adds and returns a reference to a defaulted slot (used for testing purposes) */ FSmartObjectSlotDefinition& DebugAddSlot() { return Slots.AddDefaulted_GetRef(); } /** * Returns the transform (in world space) of the given slot index. * @param OwnerTransform Transform (in world space) of the slot owner. * @param SlotIndex Index within the list of slots. * @return Transform (in world space) of the slot associated to SlotIndex. * @note Method will ensure on invalid invalid index. */ PRAGMA_DISABLE_DEPRECATION_WARNINGS UE_DEPRECATED(5.3, "Please use GetSlotWorldTransform() instead.") TOptional GetSlotTransform(const FTransform& OwnerTransform, const FSmartObjectSlotIndex SlotIndex) const; PRAGMA_ENABLE_DEPRECATION_WARNINGS /** * Returns the transform (in world space) of the given slot index. * @param OwnerTransform Transform (in world space) of the slot owner. * @param SlotIndex Index within the list of slots. * @return Transform (in world space) of the slot associated to SlotIndex, or OwnerTransform if index is invalid. * @note Method will ensure on invalid invalid index. */ FTransform GetSlotWorldTransform(const int32 SlotIndex, const FTransform& OwnerTransform) const; /** * Fills the provided GameplayTagContainer with the activity tags associated to the slot according to the tag merging policy. * @param SlotIndex Index of the slot for which the tags are requested * @param OutActivityTags Tag container to fill with the activity tags associated to the slot */ void GetSlotActivityTags(const int32 SlotIndex, FGameplayTagContainer& OutActivityTags) const; /** * Fills the provided GameplayTagContainer with the activity tags associated to the slot according to the tag merging policy. * @param SlotDefinition Definition of the slot for which the tags are requested * @param OutActivityTags Tag container to fill with the activity tags associated to the slot */ void GetSlotActivityTags(const FSmartObjectSlotDefinition& SlotDefinition, FGameplayTagContainer& OutActivityTags) const; /** Returns the tag query to run on the user tags provided by a request to accept this definition */ const FGameplayTagQuery& GetUserTagFilter() const { return UserTagFilter; } /** Sets the tag query to run on the user tags provided by a request to accept this definition */ void SetUserTagFilter(const FGameplayTagQuery& InUserTagFilter) { UserTagFilter = InUserTagFilter; } /** Returns the tag query to run on the runtime tags of a smart object instance to accept it */ UE_DEPRECATED(5.2, "Use FWorldCondition_SmartObjectActorTagQuery or FSmartObjectWorldConditionObjectTagQuery in Preconditions instead.") const FGameplayTagQuery& GetObjectTagFilter() const { static FGameplayTagQuery Dummy; return Dummy; } /** Sets the tag query to run on the runtime tags of a smart object instance to accept it */ UE_DEPRECATED(5.2, "Use FWorldCondition_SmartObjectActorTagQuery or FSmartObjectWorldConditionObjectTagQuery in Preconditions instead.") void SetObjectTagFilter(const FGameplayTagQuery& InObjectTagFilter) {} /** Returns the list of tags describing the activity associated to this definition */ const FGameplayTagContainer& GetActivityTags() const { return ActivityTags; } /** Sets the list of tags describing the activity associated to this definition */ void SetActivityTags(const FGameplayTagContainer& InActivityTags) { ActivityTags = InActivityTags; } /** Returns the tag filtering policy that should be applied on User tags by this definition */ ESmartObjectTagFilteringPolicy GetUserTagsFilteringPolicy() const { return UserTagsFilteringPolicy; } /** Sets the tag filtering policy to apply on User tags by this definition */ void SetUserTagsFilteringPolicy(const ESmartObjectTagFilteringPolicy InUserTagsFilteringPolicy) { UserTagsFilteringPolicy = InUserTagsFilteringPolicy; } /** Returns the tag merging policy to apply on Activity tags from this definition */ ESmartObjectTagMergingPolicy GetActivityTagsMergingPolicy() const { return ActivityTagsMergingPolicy; } /** Sets the tag merging policy to apply on Activity tags from this definition */ void SetActivityTagsMergingPolicy(const ESmartObjectTagMergingPolicy InActivityTagsMergingPolicy) { ActivityTagsMergingPolicy = InActivityTagsMergingPolicy; } /** * Performs validation for the current definition. The method will return on the first error encountered by default * but could go through all validations and report all errors (e.g. when saving the asset errors are reported to the user). * An object using an invalid definition will not be registered in the simulation. * The result of the validation is stored until next validation and can be retrieved using `IsValid`. * @param ErrorsToReport Optional list of error messages that could be provided to report them * @return true if the definition is valid */ bool Validate(TArray* ErrorsToReport = nullptr) const; /** Provides a description of the definition */ friend FString LexToString(const USmartObjectDefinition& Definition) { return FString::Printf(TEXT("NumSlots=%d NumDefs=%d HasUserFilter=%s HasPreConditions=%s"), Definition.Slots.Num(), Definition.DefaultBehaviorDefinitions.Num(), *LexToString(!Definition.UserTagFilter.IsEmpty()), *LexToString(Definition.Preconditions.IsValid())); } /** Returns result of the last validation if `Validate` was called; unset otherwise. */ TOptional IsValid() const { return bValid; } #if WITH_EDITORONLY_DATA /** Actor class used for previewing the definition in the asset editor. */ UE_DEPRECATED(5.3, "Use ObjectActorClass in PreviewData instead.") UPROPERTY() TSoftClassPtr PreviewClass_DEPRECATED; /** Path of the static mesh used for previewing the definition in the asset editor. */ UE_DEPRECATED(5.3, "Use ObjectMeshPath in PreviewData instead.") UPROPERTY() FSoftObjectPath PreviewMeshPath_DEPRECATED; /** Actor class used for previewing the user in the asset editor. */ UPROPERTY() FSmartObjectDefinitionPreviewData PreviewData; #endif // WITH_EDITORONLY_DATA const USmartObjectWorldConditionSchema* GetWorldConditionSchema() const { return WorldConditionSchemaClass.GetDefaultObject(); } const TSubclassOf& GetWorldConditionSchemaClass() const { return WorldConditionSchemaClass; } protected: #if WITH_EDITOR /** @return Index of the slot that has the specified ID, or INDEX_NONE if not found. */ int32 FindSlotByID(const FGuid ID) const; void UpdateSlotReferences(); virtual void PostEditChangeChainProperty(FPropertyChangedChainEvent& PropertyChangedEvent) override; virtual void PreSave(FObjectPreSaveContext SaveContext) override; virtual EDataValidationResult IsDataValid(class FDataValidationContext& Context) const override; #endif // WITH_EDITOR virtual void PostLoad() override; private: /** Finds first behavior definition of a given class in the provided list of definitions. */ static const USmartObjectBehaviorDefinition* GetBehaviorDefinitionByType(const TArray& BehaviorDefinitions, const TSubclassOf& DefinitionClass); /** * Where SmartObject's user needs to stay to be able to activate it. These * will be used by AI to approach the object. Locations are relative to object's location. */ UPROPERTY(EditDefaultsOnly, Category = "SmartObject") TArray Slots; /** List of behavior definitions of different types provided to SO's user if the slot does not provide one. */ UPROPERTY(EditDefaultsOnly, Category = "SmartObject", Instanced) TArray> DefaultBehaviorDefinitions; /** This object is available if user tags match this query; always available if query is empty. */ UPROPERTY(EditDefaultsOnly, Category = "SmartObject") FGameplayTagQuery UserTagFilter; #if WITH_EDITORONLY_DATA /** This object is available if instance tags match this query; always available if query is empty. */ UE_DEPRECATED(5.2, "FWorldCondition_SmartObjectActorTagQuery or FSmartObjectWorldConditionObjectTagQuery used in Preconditions instead.") UPROPERTY(meta = (DeprecatedProperty, DeprecationMessage = "Use FWorldCondition_SmartObjectActorTagQuery or FSmartObjectWorldConditionObjectTagQuery in Preconditions instead.")) FGameplayTagQuery ObjectTagFilter; #endif // WITH_EDITORONLY_DATA /** Preconditions that must pass for the object to be found/used. */ UPROPERTY(EditDefaultsOnly, Category = "SmartObject") FWorldConditionQueryDefinition Preconditions; private: /** Tags identifying this Smart Object's use case. Can be used while looking for objects supporting given activity */ UPROPERTY(EditDefaultsOnly, Category = "SmartObject") FGameplayTagContainer ActivityTags; UPROPERTY(EditDefaultsOnly, Category = "SmartObject", AdvancedDisplay) TSubclassOf WorldConditionSchemaClass; /** Indicates how Tags from slots and parent object are combined to be evaluated by a TagQuery from a find request. */ UPROPERTY(EditAnywhere, Category = "SmartObject", AdvancedDisplay) ESmartObjectTagMergingPolicy ActivityTagsMergingPolicy; /** Indicates how TagQueries from slots and parent object will be processed against User Tags from a find request. */ UPROPERTY(EditAnywhere, Category = "SmartObject", AdvancedDisplay) ESmartObjectTagFilteringPolicy UserTagsFilteringPolicy; mutable TOptional bValid; friend class FSmartObjectSlotReferenceDetails; }; #if UE_ENABLE_INCLUDE_ORDER_DEPRECATED_IN_5_2 #include "SmartObjectTypes.h" #endif