2021-09-28 13:33:17 -04:00
// Copyright Epic Games, Inc. All Rights Reserved.
# include "SmartObjectRuntime.h"
2023-01-18 10:52:51 -05:00
# include "SmartObjectComponent.h"
2022-01-19 14:16:16 -05:00
2022-09-28 01:06:15 -04:00
# include UE_INLINE_GENERATED_CPP_BY_NAME(SmartObjectRuntime)
2021-09-28 13:33:17 -04:00
const FSmartObjectClaimHandle FSmartObjectClaimHandle : : InvalidHandle = { } ;
//----------------------------------------------------------------------//
// FSmartObjectRuntime
//----------------------------------------------------------------------//
2021-11-26 09:52:13 -05:00
FSmartObjectRuntime : : FSmartObjectRuntime ( const USmartObjectDefinition & InDefinition )
: Definition ( & InDefinition )
2022-11-01 15:11:25 -04:00
, bEnabled ( true )
2021-09-28 13:33:17 -04:00
{
2022-01-19 14:16:16 -05:00
const int32 NumSlotDefinitions = InDefinition . GetSlots ( ) . Num ( ) ;
SlotHandles . SetNum ( NumSlotDefinitions ) ;
2021-09-28 13:33:17 -04:00
}
2023-01-18 10:52:51 -05:00
AActor * FSmartObjectRuntime : : GetOwnerActor ( ) const
{
USmartObjectComponent * Component = OwnerComponent . Get ( ) ;
return Component ! = nullptr ? Component - > GetOwner ( ) : nullptr ;
}
2021-09-28 13:33:17 -04:00
//----------------------------------------------------------------------//
2022-11-01 15:11:25 -04:00
// FSmartObjectRuntimeSlot
2021-09-28 13:33:17 -04:00
//----------------------------------------------------------------------//
2022-11-01 15:11:25 -04:00
bool FSmartObjectRuntimeSlot : : Claim ( const FSmartObjectUserHandle & InUser )
2021-09-28 13:33:17 -04:00
{
2023-01-18 10:52:51 -05:00
if ( CanBeClaimed ( ) )
2021-09-28 13:33:17 -04:00
{
State = ESmartObjectSlotState : : Claimed ;
User = InUser ;
return true ;
}
return false ;
}
2022-01-19 14:16:16 -05:00
2022-11-01 15:11:25 -04:00
bool FSmartObjectRuntimeSlot : : Release ( const FSmartObjectClaimHandle & ClaimHandle , const bool bAborted )
2022-01-19 14:16:16 -05:00
{
if ( ! ensureMsgf ( ClaimHandle . IsValid ( ) , TEXT ( " Attempting to release a slot using an invalid handle: %s " ) , * LexToString ( ClaimHandle ) ) )
{
return false ;
}
bool bReleased = false ;
if ( State ! = ESmartObjectSlotState : : Claimed & & State ! = ESmartObjectSlotState : : Occupied )
{
UE_LOG ( LogSmartObject , Error , TEXT ( " Expected slot state is 'Claimed' or 'Occupied' but current state is '%s'. Slot will not be released " ) ,
* UEnum : : GetValueAsString ( State ) ) ;
}
else if ( ClaimHandle . UserHandle ! = User )
{
UE_LOG ( LogSmartObject , Error , TEXT ( " User '%s' is trying to release slot claimed or used by other user '%s'. Slot will not be released " ) ,
* LexToString ( ClaimHandle . UserHandle ) , * LexToString ( User ) ) ;
}
else
{
if ( bAborted )
{
const bool bFunctionWasExecuted = OnSlotInvalidatedDelegate . ExecuteIfBound ( ClaimHandle , State ) ;
UE_LOG ( LogSmartObject , Verbose , TEXT ( " Slot invalidated callback was%scalled for %s " ) , bFunctionWasExecuted ? TEXT ( " " ) : TEXT ( " not " ) , * LexToString ( ClaimHandle ) ) ;
}
2022-11-01 15:11:25 -04:00
State = ESmartObjectSlotState : : Free ;
2022-03-18 12:31:49 -04:00
User . Invalidate ( ) ;
2023-02-02 18:43:13 -05:00
UserData . Reset ( ) ;
2022-01-19 14:16:16 -05:00
bReleased = true ;
}
return bReleased ;
}
2022-09-28 01:06:15 -04:00