I want to use BTT to call event GA,But it won’t callback the function
Why is that? I am using Lyra for the project
UBTTask_SendGameplayEvent.h file :
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameplayTagContainer.h"
#include "Abilities/GameplayAbilityTypes.h"
#include "BehaviorTree/BTTaskNode.h"
#include "BTTask_SendGameplayEvent.generated.h"
/**
*
*/
class ULyraGameplayAbility;
struct FGameplayEventData;
UCLASS()
class LYRAGAME_API UBTTask_SendGameplayEvent : public UBTTaskNode
{
GENERATED_BODY()
public:
UBTTask_SendGameplayEvent(FObjectInitializer const& ObjectInitializer);
UPROPERTY(EditAnywhere, Category = "Ability")
FGameplayTag GameplayAbilityTag;
UPROPERTY(EditAnywhere, Category = "Ability")
FGameplayEventData Payload;
UPROPERTY(EditAnywhere, Category = "Blackboard")
FBlackboardKeySelector BlackboardKeyLocation;
protected:
virtual EBTNodeResult::Type ExecuteTask(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory) override;
UFUNCTION()
void OnAbilityEndedCallback();
private:
FDelegateHandle AbilityEndedHandle;
UPROPERTY()
ULyraGameplayAbility* GameplayAbility;
TObjectPtr<APawn> ControlledPawn;
UBehaviorTreeComponent* BehaviorComp;
};
UBTTask_SendGameplayEvent.cpp file
// Fill out your copyright notice in the Description page of Project Settings.
#include "BehaviorTree/BTTask_SendGameplayEvent.h"
#include "UObject/WeakObjectPtr.h"
#include "BehaviorTree/BlackboardComponent.h"
#include "AbilitySystemBlueprintLibrary.h"
#include "AIController.h"
#include "AbilitySystem/LyraAbilitySystemComponent.h"
#include "AbilitySystem/Abilities/LyraGameplayAbility.h"
#include "Abilities/GameplayAbility.h"
UBTTask_SendGameplayEvent::UBTTask_SendGameplayEvent(FObjectInitializer const& ObjectInitializer) : Super(ObjectInitializer)
{
NodeName = "Send Gameplay Event";
bTickIntervals = true;
}
EBTNodeResult::Type UBTTask_SendGameplayEvent::ExecuteTask(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory)
{
if (!OwnerComp.GetAIOwner())
{
return EBTNodeResult::Failed;
}
AAIController* AIController = OwnerComp.GetAIOwner();
ControlledPawn = AIController->GetPawn();
if (!ControlledPawn)
{
return EBTNodeResult::Failed;
}
if (ULyraAbilitySystemComponent* ASC = Cast<ULyraAbilitySystemComponent>(ControlledPawn->GetComponentByClass(UAbilitySystemComponent::StaticClass())))
{
BehaviorComp = &OwnerComp;
UBlackboardComponent* BlackboardComp = OwnerComp.GetBlackboardComponent();
FHitResult HitResult;
HitResult.Location = BlackboardComp->GetValueAsVector(BlackboardKeyLocation.SelectedKeyName);
Payload.TargetData = UAbilitySystemBlueprintLibrary::AbilityTargetDataFromHitResult(HitResult);
ASC->HandleGameplayEvent(GameplayAbilityTag, &Payload);
TArray<FGameplayAbilitySpec> GameplayAbilitySpecs = ASC->GetTriggeredAbilitiesByTag(GameplayAbilityTag);
if (GameplayAbilitySpecs.Num() > 0)
{
GameplayAbility = Cast<ULyraGameplayAbility>(GameplayAbilitySpecs[0].Ability);
AbilityEndedHandle = GameplayAbility->OnLyraGameplayAbilityEnd.AddUObject(this, &UBTTask_SendGameplayEvent::OnAbilityEndedCallback);
}
else
{
return EBTNodeResult::Failed;
}
}
else
{
return EBTNodeResult::Failed;
}
return EBTNodeResult::InProgress;
}
void UBTTask_SendGameplayEvent::OnAbilityEndedCallback()
{
GameplayAbility->OnLyraGameplayAbilityEnd.Remove(AbilityEndedHandle);
AbilityEndedHandle.Reset();
if (BehaviorComp)
{
FinishLatentTask(*BehaviorComp, EBTNodeResult::Succeeded);
}
}
I defined FOnRyraGameplayAbilityEnd in ULyraGameplayAbility
class LYRAGAME_API ULyraGameplayAbility : public UGameplayAbility
{
GENERATED_BODY()
friend class ULyraAbilitySystemComponent;
DECLARE_MULTICAST_DELEGATE(FOnLyraGameplayAbilityEnd);
public:
ULyraGameplayAbility(const FObjectInitializer& ObjectInitializer = FObjectInitializer::Get());
FOnLyraGameplayAbilityEnd OnLyraGameplayAbilityEnd;
Broadcast during EndAbility
void ULyraGameplayAbility::EndAbility(const FGameplayAbilitySpecHandle Handle, const FGameplayAbilityActorInfo* ActorInfo, const FGameplayAbilityActivationInfo ActivationInfo, bool bReplicateEndAbility, bool bWasCancelled)
{
ClearCameraMode();
OnLyraGameplayAbilityEnd.Broadcast();
Super::EndAbility(Handle, ActorInfo, ActivationInfo, bReplicateEndAbility, bWasCancelled);
}
Please help me