Files

186 lines
6.3 KiB
C++
Raw Permalink Normal View History

// Copyright Epic Games, Inc. All Rights Reserved.
#include "SmartObjectBlueprintFunctionLibrary.h"
[SmartObject] some cleanup in the blueprint API for SmartObjects - replaced 'SmartObjectSubsystem.Claim' by 'MarkSlotAsClaimed' for native code and 'USmartObjectBlueprintFunctionLibrary::MarkSmartObjectSlotAsClaimed' for Blueprints - replaced 'SmartObjectSubsystem.Use' by 'MarkSlotAsOccupied' for native code and 'USmartObjectBlueprintFunctionLibrary::MarkSmartObjectSlotAsOccupied' for Blueprints - replaced 'SmartObjectSubsystem.Release' by 'MarkSlotAsFree' for native code and 'USmartObjectBlueprintFunctionLibrary::MarkSmartObjectSlotAsFree' for Blueprints - deprecated 'UGameplayBehaviorSmartObjectsBlueprintFunctionLibrary::UseGameplayBehaviorSmartObject' in favor of the aync task based versions - UFUNCTION versions are redirected directly to the new names. Native versions are deprecated. - updated 'AITask_UseGameplayInteraction' and 'AITask_UseGameplayBehaviorSmartObject' to be as close as possible since they are two similar use cases but for different plugins. - both tasks now propose blueprint callables to create an "interact on spot" or "move to and interact" tasks: - 'UseSmartObjectWithGameplayBehavior' and 'MoveToAndUseSmartObjectWithGameplayBehavior' - 'UseSmartObjectWithGameplayInteraction' and 'MoveToAndUseSmartObjectWithGameplayInteraction' - 'AITask_UseGameplayBehaviorSmartObject::UseClaimedGameplayBehaviorSmartObject' is redirected to 'MoveToAndUseSmartObjectWithGameplayBehavior' to preserve current behavior - 'AITask_UseGameplayInteraction::UseClaimedGameplayInteractionSmartObject' is redirected to 'UseSmartObjectWithGameplayInteraction' to preserve current behavior #jira UE-187209 #rnx [CL 25871194 by yoan stamant in ue5-main branch]
2023-06-08 10:34:40 -04:00
#include "Engine/Engine.h"
#include "SmartObjectSubsystem.h"
#include "BlackboardKeyType_SOClaimHandle.h"
#include "BehaviorTree/BlackboardComponent.h"
#include "BehaviorTree/BTFunctionLibrary.h"
#include UE_INLINE_GENERATED_CPP_BY_NAME(SmartObjectBlueprintFunctionLibrary)
//----------------------------------------------------------------------//
// USmartObjectBlueprintFunctionLibrary
//----------------------------------------------------------------------//
FSmartObjectClaimHandle USmartObjectBlueprintFunctionLibrary::GetValueAsSOClaimHandle(UBlackboardComponent* BlackboardComponent, const FName& KeyName)
{
if (BlackboardComponent == nullptr)
{
return {};
}
return BlackboardComponent->GetValue<UBlackboardKeyType_SOClaimHandle>(KeyName);
}
void USmartObjectBlueprintFunctionLibrary::SetValueAsSOClaimHandle(UBlackboardComponent* BlackboardComponent, const FName& KeyName, const FSmartObjectClaimHandle Value)
{
if (BlackboardComponent == nullptr)
{
return;
}
const FBlackboard::FKey KeyID = BlackboardComponent->GetKeyID(KeyName);
BlackboardComponent->SetValue<UBlackboardKeyType_SOClaimHandle>(KeyID, Value);
}
bool USmartObjectBlueprintFunctionLibrary::AddOrRemoveSmartObject(AActor* SmartObjectActor, const bool bAdd)
{
return AddOrRemoveMultipleSmartObjects({SmartObjectActor}, bAdd);
}
bool USmartObjectBlueprintFunctionLibrary::AddSmartObject(AActor* SmartObjectActor)
{
return AddOrRemoveMultipleSmartObjects({SmartObjectActor}, /*bAdd*/true);
}
bool USmartObjectBlueprintFunctionLibrary::AddMultipleSmartObjects(const TArray<AActor*>& SmartObjectActors)
{
return AddOrRemoveMultipleSmartObjects(SmartObjectActors, /*bAdd*/true);
}
bool USmartObjectBlueprintFunctionLibrary::RemoveSmartObject(AActor* SmartObjectActor)
{
return AddOrRemoveMultipleSmartObjects({SmartObjectActor}, /*bAdd*/false);
}
bool USmartObjectBlueprintFunctionLibrary::RemoveMultipleSmartObjects(const TArray<AActor*>& SmartObjectActors)
{
return AddOrRemoveMultipleSmartObjects(SmartObjectActors, /*bAdd*/false);
}
bool USmartObjectBlueprintFunctionLibrary::AddOrRemoveMultipleSmartObjects(const TArray<AActor*>& SmartObjectActors, const bool bAdd)
{
bool bSuccess = true;
if (SmartObjectActors.IsEmpty())
{
return bSuccess;
}
USmartObjectSubsystem* Subsystem = nullptr;
for (const AActor* SmartObjectActor : SmartObjectActors)
{
if (SmartObjectActor == nullptr)
{
UE_LOG(LogSmartObject, Warning, TEXT("Null actor found and skipped"))
bSuccess = false;
continue;
}
if (Subsystem == nullptr)
{
Subsystem = USmartObjectSubsystem::GetCurrent(SmartObjectActor->GetWorld());
if (Subsystem == nullptr)
{
UE_LOG(LogSmartObject, Warning, TEXT("Unable to find SmartObjectSubsystem for the provided actors."))
return false;
}
}
bSuccess = bAdd ? Subsystem->RegisterSmartObjectActor(*SmartObjectActor) : Subsystem->RemoveSmartObjectActor(*SmartObjectActor) && bSuccess;
}
return bSuccess;
}
bool USmartObjectBlueprintFunctionLibrary::SetSmartObjectEnabled(AActor* SmartObjectActor, const bool bEnabled)
{
return SetMultipleSmartObjectsEnabled({SmartObjectActor}, bEnabled);
}
[SmartObject] some cleanup in the blueprint API for SmartObjects - replaced 'SmartObjectSubsystem.Claim' by 'MarkSlotAsClaimed' for native code and 'USmartObjectBlueprintFunctionLibrary::MarkSmartObjectSlotAsClaimed' for Blueprints - replaced 'SmartObjectSubsystem.Use' by 'MarkSlotAsOccupied' for native code and 'USmartObjectBlueprintFunctionLibrary::MarkSmartObjectSlotAsOccupied' for Blueprints - replaced 'SmartObjectSubsystem.Release' by 'MarkSlotAsFree' for native code and 'USmartObjectBlueprintFunctionLibrary::MarkSmartObjectSlotAsFree' for Blueprints - deprecated 'UGameplayBehaviorSmartObjectsBlueprintFunctionLibrary::UseGameplayBehaviorSmartObject' in favor of the aync task based versions - UFUNCTION versions are redirected directly to the new names. Native versions are deprecated. - updated 'AITask_UseGameplayInteraction' and 'AITask_UseGameplayBehaviorSmartObject' to be as close as possible since they are two similar use cases but for different plugins. - both tasks now propose blueprint callables to create an "interact on spot" or "move to and interact" tasks: - 'UseSmartObjectWithGameplayBehavior' and 'MoveToAndUseSmartObjectWithGameplayBehavior' - 'UseSmartObjectWithGameplayInteraction' and 'MoveToAndUseSmartObjectWithGameplayInteraction' - 'AITask_UseGameplayBehaviorSmartObject::UseClaimedGameplayBehaviorSmartObject' is redirected to 'MoveToAndUseSmartObjectWithGameplayBehavior' to preserve current behavior - 'AITask_UseGameplayInteraction::UseClaimedGameplayInteractionSmartObject' is redirected to 'UseSmartObjectWithGameplayInteraction' to preserve current behavior #jira UE-187209 #rnx [CL 25871194 by yoan stamant in ue5-main branch]
2023-06-08 10:34:40 -04:00
FSmartObjectClaimHandle USmartObjectBlueprintFunctionLibrary::MarkSmartObjectSlotAsClaimed(
UObject* WorldContextObject,
const FSmartObjectSlotHandle SlotHandle,
const AActor* UserActor)
{
const UWorld* World = GEngine->GetWorldFromContextObject(WorldContextObject, EGetWorldErrorMode::LogAndReturnNull);
if (USmartObjectSubsystem* Subsystem = USmartObjectSubsystem::GetCurrent(World))
{
return Subsystem->MarkSlotAsClaimed(SlotHandle, FConstStructView::Make(FSmartObjectActorUserData(UserActor)));
}
return FSmartObjectClaimHandle::InvalidHandle;
}
const USmartObjectBehaviorDefinition* USmartObjectBlueprintFunctionLibrary::MarkSmartObjectSlotAsOccupied(
UObject* WorldContextObject,
const FSmartObjectClaimHandle ClaimHandle,
const TSubclassOf<USmartObjectBehaviorDefinition> DefinitionClass)
{
const UWorld* World = GEngine->GetWorldFromContextObject(WorldContextObject, EGetWorldErrorMode::LogAndReturnNull);
if (USmartObjectSubsystem* Subsystem = USmartObjectSubsystem::GetCurrent(World))
{
Subsystem->MarkSlotAsOccupied(ClaimHandle, DefinitionClass);
}
return nullptr;
}
bool USmartObjectBlueprintFunctionLibrary::MarkSmartObjectSlotAsFree(
UObject* WorldContextObject,
const FSmartObjectClaimHandle ClaimHandle)
{
const UWorld* World = GEngine->GetWorldFromContextObject(WorldContextObject, EGetWorldErrorMode::LogAndReturnNull);
if (USmartObjectSubsystem* Subsystem = USmartObjectSubsystem::GetCurrent(World))
{
return Subsystem->MarkSlotAsFree(ClaimHandle);
}
return false;
}
bool USmartObjectBlueprintFunctionLibrary::SetMultipleSmartObjectsEnabled(const TArray<AActor*>& SmartObjectActors, const bool bEnabled)
{
bool bSuccess = true;
if (SmartObjectActors.IsEmpty())
{
return bSuccess;
}
USmartObjectSubsystem* Subsystem = nullptr;
for (const AActor* SmartObjectActor : SmartObjectActors)
{
if (SmartObjectActor == nullptr)
{
UE_LOG(LogSmartObject, Warning, TEXT("Null actor found and skipped"))
bSuccess = false;
continue;
}
if (Subsystem == nullptr)
{
Subsystem = USmartObjectSubsystem::GetCurrent(SmartObjectActor->GetWorld());
if (Subsystem == nullptr)
{
UE_LOG(LogSmartObject, Warning, TEXT("Unable to find SmartObjectSubsystem for the provided actors."))
return false;
}
}
bSuccess = Subsystem->SetSmartObjectActorEnabled(*SmartObjectActor, bEnabled) && bSuccess;
}
return bSuccess;
}
void USmartObjectBlueprintFunctionLibrary::SetBlackboardValueAsSOClaimHandle(UBTNode* NodeOwner, const FBlackboardKeySelector& Key, const FSmartObjectClaimHandle& Value)
{
if (UBlackboardComponent* BlackboardComp = UBTFunctionLibrary::GetOwnersBlackboard(NodeOwner))
{
BlackboardComp->SetValue<UBlackboardKeyType_SOClaimHandle>(Key.SelectedKeyName, Value);
}
}
FSmartObjectClaimHandle USmartObjectBlueprintFunctionLibrary::GetBlackboardValueAsSOClaimHandle(UBTNode* NodeOwner, const FBlackboardKeySelector& Key)
{
UBlackboardComponent* BlackboardComp = UBTFunctionLibrary::GetOwnersBlackboard(NodeOwner);
return BlackboardComp ? BlackboardComp->GetValue<UBlackboardKeyType_SOClaimHandle>(Key.SelectedKeyName) : FSmartObjectClaimHandle::InvalidHandle;
}