2021-09-28 13:33:17 -04:00
// Copyright Epic Games, Inc. All Rights Reserved.
# pragma once
2021-11-02 11:09:09 -04:00
# include "SmartObjectCollection.h"
2021-09-28 13:33:17 -04:00
# include "Templates/SubclassOf.h"
# include "SmartObjectTypes.h"
# include "SmartObjectRuntime.h"
# include "Subsystems/WorldSubsystem.h"
# include "SmartObjectSubsystem.generated.h"
class USmartObjectComponent ;
2022-02-03 14:03:16 -05:00
class UMassEntitySubsystem ;
2022-02-21 01:10:34 -05:00
class ASmartObjectSubsystemRenderingActor ;
class FDebugRenderSceneProxy ;
2021-09-28 13:33:17 -04:00
2021-11-03 15:46:53 -04:00
# if WITH_EDITOR
2022-02-25 14:18:42 -05:00
/** Called when an event related to the main collection occured. */
DECLARE_MULTICAST_DELEGATE ( FOnMainCollectionEvent ) ;
2021-11-03 15:46:53 -04:00
# endif
2021-09-28 13:33:17 -04:00
/**
* Struct that can be used to filter results of a smart object request when trying to find or claim a smart object
*/
2021-11-22 16:32:17 -05:00
USTRUCT ( BlueprintType )
2021-09-28 13:33:17 -04:00
struct SMARTOBJECTSMODULE_API FSmartObjectRequestFilter
{
2021-11-22 16:32:17 -05:00
GENERATED_BODY ( )
2021-09-28 13:33:17 -04:00
FSmartObjectRequestFilter ( ) = default ;
2021-11-22 16:32:17 -05:00
UPROPERTY ( EditAnywhere , BlueprintReadWrite , Category = SmartObject )
2021-09-28 13:33:17 -04:00
FGameplayTagContainer UserTags ;
2021-11-22 16:32:17 -05:00
UPROPERTY ( EditAnywhere , BlueprintReadWrite , Category = SmartObject )
2021-09-28 13:33:17 -04:00
FGameplayTagQuery ActivityRequirements ;
2021-11-22 16:32:17 -05:00
UPROPERTY ( EditAnywhere , BlueprintReadWrite , Category = SmartObject )
2021-11-26 12:55:06 -05:00
TSubclassOf < USmartObjectBehaviorDefinition > BehaviorDefinitionClass ;
2021-09-28 13:33:17 -04:00
2022-01-19 14:16:16 -05:00
TFunction < bool ( FSmartObjectHandle ) > Predicate ;
2021-09-28 13:33:17 -04:00
} ;
/**
* Struct used to find a smart object within a specific search range and with optional filtering
*/
2021-11-22 16:32:17 -05:00
USTRUCT ( BlueprintType )
2021-09-28 13:33:17 -04:00
struct SMARTOBJECTSMODULE_API FSmartObjectRequest
{
2021-11-22 16:32:17 -05:00
GENERATED_BODY ( )
FSmartObjectRequest ( ) = default ;
2021-09-28 13:33:17 -04:00
FSmartObjectRequest ( const FBox & InQueryBox , const FSmartObjectRequestFilter & InFilter )
: QueryBox ( InQueryBox )
, Filter ( InFilter )
{ }
/** Box defining the search range */
2021-11-22 16:32:17 -05:00
UPROPERTY ( EditAnywhere , BlueprintReadWrite , Category = SmartObject )
FBox QueryBox = FBox ( ForceInitToZero ) ;
2021-09-28 13:33:17 -04:00
/** Struct used to filter out some results (all results allowed by default) */
2021-11-22 16:32:17 -05:00
UPROPERTY ( EditAnywhere , BlueprintReadWrite , Category = SmartObject )
2021-09-28 13:33:17 -04:00
FSmartObjectRequestFilter Filter ;
} ;
/**
* Struct that holds the object and slot selected by processing a smart object request .
*/
2021-11-22 16:32:17 -05:00
USTRUCT ( BlueprintType )
2021-09-28 13:33:17 -04:00
struct SMARTOBJECTSMODULE_API FSmartObjectRequestResult
{
2021-11-22 16:32:17 -05:00
GENERATED_BODY ( )
2022-03-17 19:07:54 -04:00
explicit FSmartObjectRequestResult ( const FSmartObjectHandle InSmartObjectHandle , const FSmartObjectSlotHandle InSlotHandle = { } )
2022-01-19 14:16:16 -05:00
: SmartObjectHandle ( InSmartObjectHandle )
, SlotHandle ( InSlotHandle )
2021-09-28 13:33:17 -04:00
{ }
FSmartObjectRequestResult ( ) = default ;
2022-01-19 14:16:16 -05:00
bool IsValid ( ) const { return SmartObjectHandle . IsValid ( ) & & SlotHandle . IsValid ( ) ; }
2021-09-28 13:33:17 -04:00
2021-11-22 16:32:17 -05:00
bool operator = = ( const FSmartObjectRequestResult & Other ) const
{
2022-03-18 12:31:49 -04:00
return SmartObjectHandle = = Other . SmartObjectHandle
2022-01-19 14:16:16 -05:00
& & SlotHandle = = Other . SlotHandle ;
2021-11-22 16:32:17 -05:00
}
bool operator ! = ( const FSmartObjectRequestResult & Other ) const
{
return ! ( * this = = Other ) ;
}
2022-01-19 14:16:16 -05:00
friend FString LexToString ( const FSmartObjectRequestResult & Result )
2021-09-28 13:33:17 -04:00
{
2022-01-19 14:16:16 -05:00
return FString : : Printf ( TEXT ( " Object:%s Slot:%s " ) , * LexToString ( Result . SmartObjectHandle ) , * LexToString ( Result . SlotHandle ) ) ;
2021-09-28 13:33:17 -04:00
}
2022-01-19 14:16:16 -05:00
UPROPERTY ( Transient , VisibleAnywhere , BlueprintReadOnly , Category = SmartObject )
FSmartObjectHandle SmartObjectHandle ;
2021-11-22 16:32:17 -05:00
2022-01-19 14:16:16 -05:00
UPROPERTY ( Transient , VisibleAnywhere , Category = SmartObject )
FSmartObjectSlotHandle SlotHandle ;
2021-09-28 13:33:17 -04:00
} ;
2022-01-12 16:15:32 -05:00
/**
* Result code indicating if the Collection was successfully registered or why it was not .
*/
UENUM ( )
2022-03-02 13:25:37 -05:00
enum class ESmartObjectCollectionRegistrationResult : uint8
2022-01-12 16:15:32 -05:00
{
Failed_InvalidCollection ,
Failed_AlreadyRegistered ,
Failed_NotFromPersistentLevel ,
2022-03-02 13:25:37 -05:00
Succeeded
2022-01-31 18:52:49 -05:00
} ;
2022-03-30 15:13:42 -04:00
/**
* Mode that indicates how the unregistration of the SmartObjectComponent affects its runtime instance .
*/
UENUM ( )
enum class ESmartObjectUnregistrationMode : uint8
{
KeepRuntimeInstanceActive ,
DestroyRuntimeInstance
} ;
2021-09-28 13:33:17 -04:00
/**
* Subsystem that holds all registered smart object instances and offers the API for spatial queries and reservations .
*/
2022-02-21 01:10:34 -05:00
UCLASS ( config = SmartObjects , defaultconfig , Transient )
2021-09-28 13:33:17 -04:00
class SMARTOBJECTSMODULE_API USmartObjectSubsystem : public UWorldSubsystem
{
GENERATED_BODY ( )
public :
static USmartObjectSubsystem * GetCurrent ( const UWorld * World ) ;
2022-01-12 16:15:32 -05:00
ESmartObjectCollectionRegistrationResult RegisterCollection ( ASmartObjectCollection & InCollection ) ;
2021-09-28 13:33:17 -04:00
void UnregisterCollection ( ASmartObjectCollection & InCollection ) ;
ASmartObjectCollection * GetMainCollection ( ) const { return MainCollection ; }
2022-03-30 15:13:42 -04:00
/**
* Registers to the runtime simulation all SmartObject components for a given actor .
* @ param SmartObjectActor Actor owning the components to register
* @ return true when components are found and all successfully registered , false otherwise
*/
bool RegisterSmartObjectActor ( const AActor & SmartObjectActor ) ;
/**
* Unregisters from the simulation all SmartObject components for a given actor .
* @ param SmartObjectActor Actor owning the components to unregister
* @ return true when components are found and all successfully unregistered , false otherwise
*/
bool UnregisterSmartObjectActor ( const AActor & SmartObjectActor ) ;
/**
* Registers a SmartObject components to the runtime simulation .
* @ param SmartObjectComponent SmartObject component to register
* @ return true when component is successfully registered , false otherwise
*/
2021-09-28 13:33:17 -04:00
bool RegisterSmartObject ( USmartObjectComponent & SmartObjectComponent ) ;
2022-03-30 15:13:42 -04:00
/**
* Unregisters a SmartObject components from the runtime simulation .
* @ param SmartObjectComponent SmartObject component to unregister
* @ return true when component is successfully unregistered , false otherwise
*/
2021-09-28 13:33:17 -04:00
bool UnregisterSmartObject ( USmartObjectComponent & SmartObjectComponent ) ;
/**
* Returns the component associated to the claim handle if still
* accessible . In some scenarios the component may no longer exist
* but its smart object data could ( e . g . streaming )
2022-03-18 12:31:49 -04:00
* @ param ClaimHandle Handle to a claimed slot returned by any of the Claim methods .
2021-09-28 13:33:17 -04:00
* @ return A pointer to the USmartObjectComponent * associated to the handle .
*/
2021-11-22 16:32:17 -05:00
UFUNCTION ( BlueprintCallable , Category = " SmartObject " )
2021-09-28 13:33:17 -04:00
USmartObjectComponent * GetSmartObjectComponent ( const FSmartObjectClaimHandle & ClaimHandle ) const ;
/**
* Spatial lookup
* @ return First valid smart object in range . Not the closest one , just the one
2022-03-16 03:47:02 -04:00
* that happens to be retrieved first from space partition
2021-09-28 13:33:17 -04:00
*/
2021-11-22 16:32:17 -05:00
UFUNCTION ( BlueprintCallable , Category = " SmartObject " )
2022-02-17 03:40:43 -05:00
FSmartObjectRequestResult FindSmartObject ( const FSmartObjectRequest & Request ) const ;
2021-09-28 13:33:17 -04:00
/**
* Spatial lookup
* @ return All valid smart objects in range .
*/
2021-11-22 16:32:17 -05:00
UFUNCTION ( BlueprintCallable , Category = " SmartObject " )
2022-02-17 03:40:43 -05:00
bool FindSmartObjects ( const FSmartObjectRequest & Request , TArray < FSmartObjectRequestResult > & OutResults ) const ;
2021-09-28 13:33:17 -04:00
2022-01-31 18:52:49 -05:00
/**
* Returns slots of a given smart object matching the filter .
2022-03-18 12:31:49 -04:00
* @ param Handle Handle to the smart object .
* @ param Filter Filter to apply on object and slots .
2022-01-31 18:52:49 -05:00
* @ param OutSlots Available slots found that match the filter
*/
2022-03-02 13:25:37 -05:00
void FindSlots ( const FSmartObjectHandle Handle , const FSmartObjectRequestFilter & Filter , TArray < FSmartObjectSlotHandle > & OutSlots ) const ;
2021-09-28 13:33:17 -04:00
/**
2022-03-18 12:31:49 -04:00
* Claims smart object from a request result .
* @ param RequestResult Request result for given smart object and slot index .
* @ return A handle binding the claimed smart object , its slot and a user id .
2021-09-28 13:33:17 -04:00
*/
2021-11-22 16:32:17 -05:00
UFUNCTION ( BlueprintCallable , Category = " SmartObject " )
2022-03-18 12:31:49 -04:00
FSmartObjectClaimHandle Claim ( const FSmartObjectRequestResult & RequestResult ) { return Claim ( RequestResult . SmartObjectHandle , RequestResult . SlotHandle ) ; }
2021-09-28 13:33:17 -04:00
2022-03-16 03:47:02 -04:00
/**
2022-03-18 12:31:49 -04:00
* Claim smart object from object and slot handles .
* @ param Handle Handle to the smart object .
* @ param SlotHandle Handle to a smart object slot .
* @ return A handle binding the claimed smart object , its slot and a user id .
2022-03-16 03:47:02 -04:00
*/
2022-03-17 19:07:54 -04:00
UE_NODISCARD FSmartObjectClaimHandle Claim ( const FSmartObjectHandle Handle , FSmartObjectSlotHandle SlotHandle ) ;
2022-03-16 03:47:02 -04:00
/**
2022-03-18 12:31:49 -04:00
* Claim smart object from object and slot handles .
* @ param Handle Handle to the smart object .
* @ param Filter Optional filter to apply on object and slots .
* @ return A handle binding the claimed smart object , its slot and a user id .
2022-03-16 03:47:02 -04:00
*/
2022-03-17 19:07:54 -04:00
UE_NODISCARD FSmartObjectClaimHandle Claim ( const FSmartObjectHandle Handle , const FSmartObjectRequestFilter & Filter = { } ) ;
2022-03-18 12:31:49 -04:00
/**
* Indicates if the object referred to by the given handle is still accessible in the simulation .
* This should only be required when a handle is stored and used later .
* @ param Handle Handle to the smart object .
* @ return True if the handle is valid and its associated object is accessible ; false otherwise .
*/
bool IsSmartObjectValid ( const FSmartObjectHandle Handle ) const ;
2021-09-28 13:33:17 -04:00
/**
2022-03-16 03:47:02 -04:00
* Indicates if the object / slot referred to by the given handle are still accessible in the simulation .
* This should only be required when a handle is stored and later needed to access slot or object information ( e . g . SlotView )
* Otherwise a valid ClaimHandle can be use directly after calling ' Claim ' .
2022-03-18 12:31:49 -04:00
* @ param ClaimHandle Handle to a claimed slot returned by any of the Claim methods .
* @ return True if the claim handle is valid and its associated object is accessible ; false otherwise .
2022-03-16 03:47:02 -04:00
*/
2022-03-18 12:31:49 -04:00
bool IsClaimedSmartObjectValid ( const FSmartObjectClaimHandle & ClaimHandle ) const ;
2022-03-16 03:47:02 -04:00
/**
* Indicates if the slot referred to by the given handle is still accessible in the simulation .
* This should only be required when a handle is stored and later needed to access slot information ( e . g . SlotView )
* Otherwise a valid SlotHandle can be use directly after calling any of the ' Find ' or ' Claim ' methods .
2022-03-18 12:31:49 -04:00
* @ param SlotHandle Handle to a smart object slot .
* @ return True if the handle is valid and its associated slot is accessible ; false otherwise .
2022-03-16 03:47:02 -04:00
*/
2022-03-18 12:31:49 -04:00
bool IsSmartObjectSlotValid ( const FSmartObjectSlotHandle SlotHandle ) const { return SlotHandle . IsValid ( ) & & RuntimeSlotStates . Find ( SlotHandle ) ! = nullptr ; }
2022-03-16 03:47:02 -04:00
/**
* Start using a claimed smart object slot .
2022-03-18 12:31:49 -04:00
* @ param ClaimHandle Handle to a claimed slot returned by any of the Claim methods .
2022-03-16 03:47:02 -04:00
* @ param DefinitionClass The type of behavior definition the user wants to use .
* @ return The base class pointer of the requested behavior definition class associated to the slot
2021-09-28 13:33:17 -04:00
*/
2021-11-22 16:32:17 -05:00
UFUNCTION ( BlueprintCallable , Category = " SmartObject " )
2021-11-26 12:55:06 -05:00
const USmartObjectBehaviorDefinition * Use ( const FSmartObjectClaimHandle & ClaimHandle , const TSubclassOf < USmartObjectBehaviorDefinition > & DefinitionClass ) ;
2021-09-28 13:33:17 -04:00
/**
2022-03-16 03:47:02 -04:00
* Start using a claimed smart object slot .
2022-03-18 12:31:49 -04:00
* @ param ClaimHandle Handle to a claimed slot returned by any of the Claim methods .
2022-03-16 03:47:02 -04:00
* @ return The requested behavior definition class pointer associated to the slot
2021-09-28 13:33:17 -04:00
*/
2021-11-26 12:55:06 -05:00
template < typename DefinitionType >
const DefinitionType * Use ( const FSmartObjectClaimHandle & ClaimHandle )
2021-09-28 13:33:17 -04:00
{
2021-11-26 12:55:06 -05:00
static_assert ( TIsDerivedFrom < DefinitionType , USmartObjectBehaviorDefinition > : : IsDerived , " DefinitionType must derive from USmartObjectBehaviorDefinition " ) ;
return Cast < const DefinitionType > ( Use ( ClaimHandle , DefinitionType : : StaticClass ( ) ) ) ;
2021-09-28 13:33:17 -04:00
}
/**
2022-03-16 03:47:02 -04:00
* Release claim on a smart object .
2022-03-18 12:31:49 -04:00
* @ param ClaimHandle Handle to a claimed slot returned by any of the Claim methods .
2022-03-16 03:47:02 -04:00
* @ return Whether the claim was successfully released or not
2021-09-28 13:33:17 -04:00
*/
2021-11-22 16:32:17 -05:00
UFUNCTION ( BlueprintCallable , Category = " SmartObject " )
2021-09-28 13:33:17 -04:00
bool Release ( const FSmartObjectClaimHandle & ClaimHandle ) ;
2022-01-26 17:33:02 -05:00
/**
2022-03-16 03:47:02 -04:00
* Return the behavior definition of a given type from a claimed object .
2022-03-18 12:31:49 -04:00
* @ param ClaimHandle Handle to a claimed slot returned by any of the Claim methods .
2022-03-16 03:47:02 -04:00
* @ param DefinitionClass The type of behavior definition .
* @ return The base class pointer of the requested behavior definition class associated to the slotClaim handle
2022-01-26 17:33:02 -05:00
*/
UFUNCTION ( BlueprintCallable , Category = " SmartObject " )
const USmartObjectBehaviorDefinition * GetBehaviorDefinition ( const FSmartObjectClaimHandle & ClaimHandle , const TSubclassOf < USmartObjectBehaviorDefinition > & DefinitionClass ) ;
/**
2022-03-16 03:47:02 -04:00
* Return the behavior definition of a given type from a claimed object .
2022-03-18 12:31:49 -04:00
* @ param ClaimHandle Handle to a claimed slot returned by any of the Claim methods .
2022-03-16 03:47:02 -04:00
* @ return The requested behavior definition class pointer associated to the Claim handle
2022-01-26 17:33:02 -05:00
*/
template < typename DefinitionType >
const DefinitionType * GetBehaviorDefinition ( const FSmartObjectClaimHandle & ClaimHandle )
{
static_assert ( TIsDerivedFrom < DefinitionType , USmartObjectBehaviorDefinition > : : IsDerived , " DefinitionType must derive from USmartObjectBehaviorDefinition " ) ;
return Cast < const DefinitionType > ( GetBehaviorDefinition ( ClaimHandle , DefinitionType : : StaticClass ( ) ) ) ;
}
2022-03-17 19:07:54 -04:00
ESmartObjectSlotState GetSlotState ( const FSmartObjectSlotHandle SlotHandle ) const ;
2022-01-19 14:16:16 -05:00
/**
* Adds state data ( through a deferred command ) to a slot instance . Data must be a struct that inherits
* from FSmartObjectSlotStateData and passed as a struct view ( e . g . FConstStructView : : Make ( FSomeStruct ) )
2022-03-18 12:31:49 -04:00
* @ param ClaimHandle Handle to a claimed slot returned by any of the Claim methods .
* @ param InData A view on the struct to add .
2022-01-19 14:16:16 -05:00
*/
void AddSlotDataDeferred ( const FSmartObjectClaimHandle & ClaimHandle , FConstStructView InData ) const ;
2022-03-16 03:47:02 -04:00
/**
2022-03-18 12:31:49 -04:00
* Creates and returns a view to the data associated to a slot handle .
* @ return A view on the slot associated to SlotHandle . Caller should use IsValid ( ) on the view
* before using it since the provided handle might no longer refer to a slot registered in the simulation .
2022-03-16 03:47:02 -04:00
*/
2022-03-17 19:07:54 -04:00
FSmartObjectSlotView GetSlotView ( const FSmartObjectSlotHandle SlotHandle ) const ;
2022-01-19 14:16:16 -05:00
2021-09-28 13:33:17 -04:00
/**
* Returns the position ( in world space ) of the slot associated to the given claim handle .
2022-03-18 12:31:49 -04:00
* @ param ClaimHandle Handle to a claimed slot returned by any of the Claim methods .
2021-09-28 13:33:17 -04:00
* @ return Position ( in world space ) of the slot associated to ClaimHandle .
*/
2022-03-18 12:31:49 -04:00
TOptional < FVector > GetSlotLocation ( const FSmartObjectClaimHandle & ClaimHandle ) const { return GetSlotLocation ( ClaimHandle . SlotHandle ) ; }
2021-11-22 16:32:17 -05:00
/**
* Returns the position ( in world space ) of the slot associated to the given claim handle .
2022-03-18 12:31:49 -04:00
* @ param ClaimHandle Handle to a claimed slot returned by any of the Claim methods .
2021-11-22 16:32:17 -05:00
* @ param OutSlotLocation Position ( in world space ) of the slot associated to ClaimHandle .
* @ return Whether the location was found and assigned to ' OutSlotLocation '
*/
UFUNCTION ( BlueprintCallable , Category = " SmartObject " )
bool GetSlotLocation ( const FSmartObjectClaimHandle & ClaimHandle , FVector & OutSlotLocation ) const ;
2021-09-28 13:33:17 -04:00
/**
* Returns the position ( in world space ) of the slot associated to the given request result .
2022-03-18 12:31:49 -04:00
* @ param Result A request result returned by any of the Find methods .
2022-02-17 03:40:43 -05:00
* @ return Position ( in world space ) of the slot associated to Result .
2021-09-28 13:33:17 -04:00
*/
2022-03-18 12:31:49 -04:00
TOptional < FVector > GetSlotLocation ( const FSmartObjectRequestResult & Result ) const { return GetSlotLocation ( Result . SlotHandle ) ; }
2021-09-28 13:33:17 -04:00
/**
2022-01-19 14:16:16 -05:00
* Returns the position ( in world space ) of the slot represented by the provided slot handle .
* @ param SlotHandle Handle to a smart object slot .
2022-02-17 03:40:43 -05:00
* @ return Position ( in world space ) of the slot associated to SlotHandle .
2021-09-28 13:33:17 -04:00
*/
2022-03-17 19:07:54 -04:00
TOptional < FVector > GetSlotLocation ( const FSmartObjectSlotHandle SlotHandle ) const ;
2021-09-28 13:33:17 -04:00
/**
* Returns the transform ( in world space ) of the slot associated to the given claim handle .
2022-03-18 12:31:49 -04:00
* @ param ClaimHandle Handle to a claimed slot returned by any of the Claim methods .
2021-09-28 13:33:17 -04:00
* @ return Transform ( in world space ) of the slot associated to ClaimHandle .
*/
2022-03-18 12:31:49 -04:00
TOptional < FTransform > GetSlotTransform ( const FSmartObjectClaimHandle & ClaimHandle ) const { return GetSlotTransform ( ClaimHandle . SlotHandle ) ; }
2021-09-28 13:33:17 -04:00
2021-11-22 16:32:17 -05:00
/**
* Returns the transform ( in world space ) of the slot associated to the given claim handle .
2022-03-18 12:31:49 -04:00
* @ param ClaimHandle Handle to a claimed slot returned by any of the Claim methods .
2021-11-22 16:32:17 -05:00
* @ param OutSlotTransform Transform ( in world space ) of the slot associated to ClaimHandle .
* @ return Whether the transform was found and assigned to ' OutSlotTransform '
*/
UFUNCTION ( BlueprintCallable , Category = " SmartObject " )
bool GetSlotTransform ( const FSmartObjectClaimHandle & ClaimHandle , FTransform & OutSlotTransform ) const ;
2022-03-18 12:31:49 -04:00
2021-09-28 13:33:17 -04:00
/**
* Returns the transform ( in world space ) of the slot associated to the given request result .
2022-03-18 12:31:49 -04:00
* @ param Result A request result returned by any of the Find methods .
2022-02-17 03:40:43 -05:00
* @ return Transform ( in world space ) of the slot associated to Result .
2021-09-28 13:33:17 -04:00
*/
2022-03-18 12:31:49 -04:00
TOptional < FTransform > GetSlotTransform ( const FSmartObjectRequestResult & Result ) const { return GetSlotTransform ( Result . SlotHandle ) ; }
2021-09-28 13:33:17 -04:00
/**
2022-01-19 14:16:16 -05:00
* Returns the transform ( in world space ) of the slot represented by the provided slot handle .
* @ param SlotHandle Handle to a smart object slot .
2022-02-17 03:40:43 -05:00
* @ return Transform ( in world space ) of the slot associated to SlotHandle .
2021-09-28 13:33:17 -04:00
*/
2022-03-17 19:07:54 -04:00
TOptional < FTransform > GetSlotTransform ( const FSmartObjectSlotHandle SlotHandle ) const ;
2022-02-17 03:40:43 -05:00
2021-09-28 13:33:17 -04:00
/**
2022-03-18 12:31:49 -04:00
* Returns the list of tags associated to the smart object instance represented by the provided handle .
* @ param Handle Handle to the smart object .
* @ return Container of tags associated to the smart object instance .
2022-03-16 03:47:02 -04:00
*/
UFUNCTION ( BlueprintCallable , Category = " SmartObject " )
const FGameplayTagContainer & GetInstanceTags ( const FSmartObjectHandle Handle ) const ;
/**
2022-03-18 12:31:49 -04:00
* Adds a single tag to the smart object instance represented by the provided handle .
* @ param Handle Handle to the smart object .
* @ param Tag Tag to add to the smart object instance .
2022-03-16 03:47:02 -04:00
*/
UFUNCTION ( BlueprintCallable , Category = " SmartObject " )
void AddTagToInstance ( const FSmartObjectHandle Handle , const FGameplayTag & Tag ) ;
/**
* Removes a single tag from the smartobject instance represented by the provided handle .
2022-03-18 12:31:49 -04:00
* @ param Handle Handle to the smart object .
2022-03-16 03:47:02 -04:00
* @ param Tag Tag to remove from the SmartObject instance .
*/
UFUNCTION ( BlueprintCallable , Category = " SmartObject " )
void RemoveTagFromInstance ( const FSmartObjectHandle Handle , const FGameplayTag & Tag ) ;
/**
* Register a callback to be notified if the claimed slot is no longer available and user need to perform cleanup .
2022-03-18 12:31:49 -04:00
* @ param ClaimHandle Handle to a claimed slot returned by any of the Claim methods .
2022-03-16 03:47:02 -04:00
* @ param Callback Delegate that will be called to notify that a slot gets invalidated and can no longer be used .
2021-09-28 13:33:17 -04:00
*/
void RegisterSlotInvalidationCallback ( const FSmartObjectClaimHandle & ClaimHandle , const FOnSlotInvalidated & Callback ) ;
/**
2022-03-16 03:47:02 -04:00
* Unregisters a callback to be notified if the claimed slot is no longer available and user need to perform cleanup .
2022-03-18 12:31:49 -04:00
* @ param ClaimHandle Handle to a claimed slot returned by any of the Claim methods .
2021-09-28 13:33:17 -04:00
*/
void UnregisterSlotInvalidationCallback ( const FSmartObjectClaimHandle & ClaimHandle ) ;
2022-02-21 01:10:34 -05:00
# if UE_ENABLE_DEBUG_DRAWING
void DebugDraw ( FDebugRenderSceneProxy * DebugProxy ) const ;
void DebugDrawCanvas ( UCanvas * Canvas , APlayerController * PlayerController ) const { }
# endif
2021-11-03 15:46:53 -04:00
# if WITH_EDITOR
2022-02-25 14:18:42 -05:00
mutable FOnMainCollectionEvent OnMainCollectionChanged ;
mutable FOnMainCollectionEvent OnMainCollectionDirtied ;
2021-11-03 15:46:53 -04:00
# endif
2021-09-28 13:33:17 -04:00
protected :
2022-03-16 03:47:02 -04:00
friend class USmartObjectComponent ;
2021-11-02 11:09:09 -04:00
2022-03-30 15:13:42 -04:00
bool RegisterSmartObjectInternal ( USmartObjectComponent & SmartObjectComponent ) ;
bool UnregisterSmartObjectInternal ( USmartObjectComponent & SmartObjectComponent , const ESmartObjectUnregistrationMode UnregistrationMode ) ;
2021-11-02 11:09:09 -04:00
/**
* Callback overriden to gather loaded collections , spawn missing one and set the main collection .
* @ note we use this method instead of ` Initialize ` or ` PostInitialize ` so active level is set and actors registered .
*/
virtual void OnWorldComponentsUpdated ( UWorld & World ) override ;
/**
* BeginPlay will push all objects stored in the collection to the runtime simulation
* and initialize octree using collection bounds .
*/
virtual void OnWorldBeginPlay ( UWorld & World ) override ;
2021-09-28 13:33:17 -04:00
2022-02-17 03:40:43 -05:00
/** Creates all runtime data using main collection */
void InitializeRuntime ( ) ;
/** Removes all runtime data */
void CleanupRuntime ( ) ;
2022-03-16 03:47:02 -04:00
/** Returns the runtime instance associated to the provided handle */
2022-03-17 19:07:54 -04:00
FSmartObjectRuntime * GetRuntimeInstance ( const FSmartObjectHandle SmartObjectHandle ) { return RuntimeSmartObjects . Find ( SmartObjectHandle ) ; }
2022-03-16 03:47:02 -04:00
2022-03-18 12:31:49 -04:00
/**
* Indicates if the handle is set and the slot referred to is still accessible in the simulation .
* Log is produced for any failing condition using provided LogContext .
* @ param SlotHandle Handle to a smart object slot .
* @ param LogContext String describing the context in which the method is called ( e . g . caller function name )
* @ return True if the handle is valid and its associated slot is accessible ; false otherwise .
*/
bool IsSlotValidVerbose ( const FSmartObjectSlotHandle SlotHandle , const TCHAR * LogContext ) const ;
2022-03-16 03:47:02 -04:00
/**
* Returns the const runtime instance associated to the provided handle .
2022-03-18 12:31:49 -04:00
* Method produces log messages with provided context if provided handle is not set or associated instance can ' t be found .
2022-03-16 03:47:02 -04:00
*/
2022-03-17 19:07:54 -04:00
const FSmartObjectRuntime * GetValidatedRuntime ( const FSmartObjectHandle Handle , const TCHAR * Context ) const ;
2022-03-16 03:47:02 -04:00
/**
* Returns the mutable runtime instance associated to the provided handle
2022-03-18 12:31:49 -04:00
* Method produces log messages with provided context if provided handle is not set or associated instance can ' t be found .
2022-03-16 03:47:02 -04:00
*/
2022-03-17 19:07:54 -04:00
FSmartObjectRuntime * GetValidatedMutableRuntime ( const FSmartObjectHandle Handle , const TCHAR * Context ) ;
2022-03-16 03:47:02 -04:00
void AddTagToInstance ( FSmartObjectRuntime & SmartObjectRuntime , const FGameplayTag & Tag ) ;
void RemoveTagFromInstance ( FSmartObjectRuntime & SmartObjectRuntime , const FGameplayTag & Tag ) ;
void UpdateRuntimeInstanceStatus ( FSmartObjectRuntime & SmartObjectRuntime ) ;
2022-03-02 13:25:37 -05:00
/** Goes through all defined slots of smart object represented by SmartObjectRuntime and finds the ones matching the filter. */
void FindSlots ( const FSmartObjectRuntime & SmartObjectRuntime , const FSmartObjectRequestFilter & Filter , TArray < FSmartObjectSlotHandle > & OutResults ) const ;
/** Applies filter on provided definition and fills OutValidIndices with indices of all valid slots. */
static void FindMatchingSlotDefinitionIndices ( const USmartObjectDefinition & Definition , const FSmartObjectRequestFilter & Filter , TArray < int32 > & OutValidIndices ) ;
2021-09-28 13:33:17 -04:00
2022-01-26 17:33:02 -05:00
static const USmartObjectBehaviorDefinition * GetBehaviorDefinition ( const FSmartObjectRuntime & SmartObjectRuntime , const FSmartObjectClaimHandle & ClaimHandle , const TSubclassOf < USmartObjectBehaviorDefinition > & DefinitionClass ) ;
2022-01-19 14:16:16 -05:00
const USmartObjectBehaviorDefinition * Use ( const FSmartObjectRuntime & SmartObjectRuntime , const FSmartObjectClaimHandle & ClaimHandle , const TSubclassOf < USmartObjectBehaviorDefinition > & DefinitionClass ) ;
2021-09-28 13:33:17 -04:00
2022-03-16 03:47:02 -04:00
void AbortAll ( FSmartObjectRuntime & SmartObjectRuntime , const ESmartObjectSlotState NewState ) ;
2021-09-28 13:33:17 -04:00
2022-01-19 14:16:16 -05:00
FSmartObjectSlotClaimState * GetMutableSlotState ( const FSmartObjectClaimHandle & ClaimHandle ) ;
2021-09-28 13:33:17 -04:00
/** Make sure that all SmartObjectCollection actors from our associated world are registered. */
void RegisterCollectionInstances ( ) ;
2022-03-16 03:47:02 -04:00
/**
* Registers a collection entry to the simulation and creates its associated runtime instance .
* This method must be used only when the associated actor component is not available ( e . g . not loaded ) .
*/
FSmartObjectRuntime * AddCollectionEntryToSimulation ( const FSmartObjectCollectionEntry & Entry , const USmartObjectDefinition & Definition ) ;
/**
* Registers a collection entry to the simulation and creates its associated runtime instance .
* @ param SmartObjectComponent The component to add to the simulation and for which a runtime entry might be created or an existing one found
* @ param CollectionEntry The associated collection entry that got created to add the component to the simulation .
*/
FSmartObjectRuntime * AddComponentToSimulation ( USmartObjectComponent & SmartObjectComponent , const FSmartObjectCollectionEntry & CollectionEntry ) ;
/**
* Binds a smartobject component to an existing instance in the simulation .
* @ param SmartObjectComponent The component to add to the simulation and for which a runtime instance must exist
*/
void BindComponentToSimulation ( USmartObjectComponent & SmartObjectComponent ) ;
/**
* Unbinds a smartobject component from an existing instance in the simulation .
* @ param SmartObjectComponent The component to remove from the simulation
*/
void UnbindComponentFromSimulation ( USmartObjectComponent & SmartObjectComponent ) ;
void RemoveRuntimeInstanceFromSimulation ( const FSmartObjectHandle Handle ) ;
void RemoveCollectionEntryFromSimulation ( const FSmartObjectCollectionEntry & Entry ) ;
void RemoveComponentFromSimulation ( USmartObjectComponent & SmartObjectComponent ) ;
2021-09-28 13:33:17 -04:00
2022-02-21 01:10:34 -05:00
/**
* Name of the Space partition class to use .
* Usage :
* [ / Script / SmartObjectsModule . SmartObjectSubsystem ]
* SpacePartitionClassName = / Script / SmartObjectsModule . < SpacePartitionClassName >
*/
2022-05-22 10:30:02 -04:00
UPROPERTY ( config , meta = ( MetaClass = " /Script/SmartObjectsModule.SmartObjectSpacePartition " , DisplayName = " Spatial Representation Structure Class " ) )
2022-02-21 01:10:34 -05:00
FSoftClassPath SpacePartitionClassName ;
2021-09-28 13:33:17 -04:00
2022-02-03 14:03:16 -05:00
UPROPERTY ( )
2022-02-21 01:10:34 -05:00
TSubclassOf < USmartObjectSpacePartition > SpacePartitionClass ;
2022-02-03 14:03:16 -05:00
2022-02-21 01:10:34 -05:00
UPROPERTY ( )
TObjectPtr < USmartObjectSpacePartition > SpacePartition ;
UPROPERTY ( )
TObjectPtr < ASmartObjectSubsystemRenderingActor > RenderingActor ;
UPROPERTY ( )
TObjectPtr < ASmartObjectCollection > MainCollection ;
UPROPERTY ( )
TObjectPtr < UMassEntitySubsystem > EntitySubsystem ;
2021-09-28 13:33:17 -04:00
2022-01-19 14:16:16 -05:00
TMap < FSmartObjectHandle , FSmartObjectRuntime > RuntimeSmartObjects ;
TMap < FSmartObjectSlotHandle , FSmartObjectSlotClaimState > RuntimeSlotStates ;
2021-11-02 11:09:09 -04:00
/** Keep track of Ids associated to objects entirely created at runtime (i.e. not part of the initial collection) */
2022-01-19 14:16:16 -05:00
TArray < FSmartObjectHandle > RuntimeCreatedEntries ;
2021-11-02 11:09:09 -04:00
2022-03-16 03:47:02 -04:00
/** List of registered components. */
UPROPERTY ( Transient )
TArray < USmartObjectComponent * > RegisteredSOComponents ;
2022-01-19 14:16:16 -05:00
uint32 NextFreeUserID = 1 ;
2021-09-28 13:33:17 -04:00
2021-11-02 11:09:09 -04:00
/** Flag to indicate that all entries from the baked collection are registered and new registrations will be considered runtime entries (i.e. no persistence) */
bool bInitialCollectionAddedToSimulation = false ;
2021-09-28 13:33:17 -04:00
# if WITH_EDITOR
friend class ASmartObjectCollection ;
void RebuildCollection ( ASmartObjectCollection & InCollection ) ;
2022-03-16 03:47:02 -04:00
void SpawnMissingCollection ( ) const ;
2021-11-02 11:09:09 -04:00
/**
* Compute bounds from given world and store result in provided collection
* @ param World World from which the bounds must be computed
* @ param Collection Collection that will store computed bounds
*/
void ComputeBounds ( const UWorld & World , ASmartObjectCollection & Collection ) const ;
2021-09-28 13:33:17 -04:00
# endif // WITH_EDITOR
# if WITH_SMARTOBJECT_DEBUG
public :
2021-11-02 11:09:09 -04:00
uint32 DebugGetNumRuntimeObjects ( ) const { return RuntimeSmartObjects . Num ( ) ; }
2022-01-19 14:16:16 -05:00
const TMap < FSmartObjectHandle , FSmartObjectRuntime > & DebugGetRuntimeObjects ( ) const { return RuntimeSmartObjects ; }
const TMap < FSmartObjectSlotHandle , FSmartObjectSlotClaimState > & DebugGetRuntimeSlots ( ) const { return RuntimeSlotStates ; }
2022-03-16 03:47:02 -04:00
uint32 DebugGetNumRegisteredComponents ( ) const { return RegisteredSOComponents . Num ( ) ; }
2021-11-02 11:09:09 -04:00
/** Debugging helper to remove all registered smart objects from the simulation */
2021-09-28 13:33:17 -04:00
void DebugUnregisterAllSmartObjects ( ) ;
2021-11-02 11:09:09 -04:00
/** Debugging helpers to add all registered smart objects to the simulation */
2021-09-28 13:33:17 -04:00
void DebugRegisterAllSmartObjects ( ) ;
2022-02-17 03:40:43 -05:00
/** Debugging helper to emulate the start of the simulation to create all runtime data */
void DebugInitializeRuntime ( ) ;
2022-06-14 15:16:17 -04:00
/** Debugging helper to force rebuild collection for tests scenarios when collection is built on demand */
void DebugRebuildCollection ( ) ;
2022-02-17 03:40:43 -05:00
/** Debugging helper to emulate the stop of the simulation to destroy all runtime data */
void DebugCleanupRuntime ( ) ;
2021-09-28 13:33:17 -04:00
# endif // WITH_SMARTOBJECT_DEBUG
2022-03-18 12:31:49 -04:00
} ;