Skip to content
TheChatArchiveThe Chat Archive
gpt-4-0314· openaiHugging Face Suzhen/CodeChat

How can I confirm the target data from waiting from a task in WaitTargetDataUsingActor…

  1. User

    How can I confirm the target data from waiting from a task in WaitTargetDataUsingActor in the player controller instead of broadcasting the TargetDataReadyDelegate delegate?

  2. Assistant

    To confirm the target data without broadcasting the TargetDataReadyDelegate, you can directly call a function in your player controller to handle the received target data. Here's an example of how you can do this:

    1. In your player controller class, create a new function to handle the target data, for example:
    UCLASS()
    class YOURGAME_API AYourPlayerController : public APlayerController
    {
        GENERATED_BODY()
    
        // ...
    
    public:
        void HandleTargetData(FGameplayAbilityTargetDataHandle TargetData);
    };
    
    1. Implement the function in your .cpp file:
    void AYourPlayerController::HandleTargetData(FGameplayAbilityTargetDataHandle TargetData)
    {
        // Process the target data as needed
    }
    
    1. When calling WaitTargetDataUsingActor, pass a reference to your player controller (or any other relevant class) to the ExternalConfirm parameter:
    UAbilityTask_WaitTargetDataUsingActor* Task = UAbilityTask_WaitTargetDataUsingActor::WaitTargetDataUsingActor(this, Name, StartLocation, Orientation, TaskParams, NULL, false, true, Reticle, Class, false);
    
    1. In your WaitTargetData task, receive this parameter and store it:
    UPROPERTY()
    TWeakObjectPtr<AActor> StoredExternalConfirm;
    
    bool UAbilityTask_WaitTargetDataUsingActor::BeginWaitingTargetData(FName TaskInstanceName, FGameplayAbilityTargetingLocationInfo InStartLocation, FRotator InStartOrientation, TEnumAsByte<EWaitTargetDataCommitMode::Type> InCommitMode, TEnumAsByte<EWaitTargetDataCommitMode::Type> InFinishOnTriggerMode, AGameplayAbilityTargetActor* InTargetActorClass, const FGameplayTargetDataFilterHandle& TargetFilter, FName Optional20CancelEventHandler1RxString, FName Optional21ExternalConfirmRxString)
    {
        // ...
        // Make sure the ConfirmDelegate already exists in your task
        // Receive the "ExternalConfirm"
        if (IsNetLocal())
        {
            ConfirmDelegate.BindUObject(InTargetActor->MasterPC, Optional21ExternalConfirmRxString);
        }
        StoredExternalConfirm = InTargetActor->MasterPC;
        // ...
    }
    
    1. Finally, call the HandleTargetData function directly instead of broadcasting the delegate:
    void UAbilityTask_WaitTargetDataUsingActor::OnTargetChanged()
    {
        // ...
        // Instead of broadcasting the delegate, call the stored function to handle the target data
        if (!ConfirmDelegate.IsBound() && StoredExternalConfirm.IsValid())
        {
            AYourPlayerController* PlayerController = Cast<AYourPlayerController>(StoredExternalConfirm.Get());
            if (PlayerController)
            {
                PlayerController->HandleTargetData(Data);
            }
        }
        // ...
    }
    

    This way, when the target data is ready, the HandleTargetData function will be called directly in your player controller without using the broadcast delegate. Make sure to adjust the class names and function names according to your own project.

  3. User

    Is it better to let the target actor handle notifying the task that the target data is confirmed or is there a better way to do it?

  4. Assistant

    It depends on your specific use case, but generally, it is better to let the target actor handle notifying the task when the target data is confirmed. This follows the principle of separating responsibilities, making the code more modular and easier to maintain. The target actor's purpose is to gather the required target data, and once it's done, it should notify the ability task.

    If you use a delegate, you can easily add additional listeners in the future or even rewire the delegate to call a different event handler without modifying the target actor's code. It follows the publish-subscribe pattern, so the target actor does not need to know who specifically is using the gathered target data, making the code more flexible.

    However, there may be cases when you want to have more direct control over the target data handling within the ability system, and calling the function directly can prove beneficial. But in most scenarios, it is better to use the delegate approach.

  5. User

    How would you feel making a delegate in the player controller and when the player left clicks, the player controller checks if we are currently targeting to use an ability and it handles notifying the target actor and then the target notifies that the target data is ready?

  6. Assistant

    This approach can be useful in certain scenarios, and it also helps to separate responsibilities and make your code more modular. To implement this, you can follow these steps:

    1. In your player controller class, create a new multicast delegate:
    UCLASS()
    class YOURGAME_API AYourPlayerController : public APlayerController
    {
        GENERATED_BODY()
    
        // ...
    
    public:
        DECLARE_MULTICAST_DELEGATE(FOnLeftClickDelegate)
    
        FOnLeftClickDelegate OnLeftClickDelegate;
    
        void HandleLeftClick();
    };
    
    1. Implement the HandleLeftClick function:
    void AYourPlayerController::HandleLeftClick()
    {
        // Check if the player is currently targeting for an ability
        // ...
    
        // Notify the target actor
        OnLeftClickDelegate.Broadcast();
    
        // ...
    }
    
    1. Bind your target actor's confirm function to the delegate in the player controller when you create your target actor:
    // Assuming "YourTargetActor" as a pointer to your target actor
    YourTargetActor->MasterPC->OnLeftClickDelegate.AddUObject(YourTargetActor, &AYourTargetActor::ConfirmFunction);
    
    
    1. In your target actor class, make sure to have the ConfirmFunction:
    void AYourTargetActor::ConfirmFunction()
    {
        // Handle the confirmed target data
        // Notify the WaitTargetData Task that the target data is ready
    }
    

    Don't forget to unbind the delegate when the target actor or the related ability is finished or canceled.

    With this approach, the player controller has control over when the target data should be confirmed, and the target actor doesn't need to know when the player controller's left click happens. The flow of notifying about the left click via a delegate ensures flexibility and modularity in the code.

  7. User

    What's the best to check if another ability is active that differs from the ability that I want to activate in the latest version of GAS in UE5.2

  8. Assistant

    To check if another ability is active that differs from the ability you want to activate in GAS, you can use the GetActiveAbilitiesWithTags function from the UAbilitySystemComponent. Here's an example:

    1. First, create a function in your character or player controller class that takes the ability class or ability tags as input and returns a boolean indicating if another ability is active:
    UCLASS()
    class YOURGAME_API AYourCharacter : public ACharacter
    {
        GENERATED_BODY()
    
        // ...
    
    public:
        UFUNCTION(BlueprintCallable, Category = "Abilities")
        bool IsAnotherAbilityActive(TSubclassOf<UGameplayAbility> AbilityToActivate, FGameplayTagContainer IgnoreAbilityTags);
    };
    
    1. Implement the function in your .cpp file:
    bool AYourCharacter::IsAnotherAbilityActive(TSubclassOf<UGameplayAbility> AbilityToActivate, FGameplayTagContainer IgnoreAbilityTags)
    {
        UAbilitySystemComponent* AbilitySystemComponent = GetAbilitySystemComponent(); // You may have a different function to get the AbilitySystemComponent.
        if (AbilitySystemComponent)
        {
            TArray<FGameplayAbilitySpec*> ActiveAbilities;
            AbilitySystemComponent->GetActiveAbilitiesWithTags(IgnoreAbilityTags, ActiveAbilities);
    
            // Iterate through the active abilities
            for (FGameplayAbilitySpec* ActiveAbilitySpec : ActiveAbilities)
            {
                // Check if it's a different ability than the one we want to activate
                if (ActiveAbilitySpec->Ability->GetClass() != AbilityToActivate)
                {
                    return true;
                }
            }
        }
    
        return false;
    }
    
    1. When you want to activate a new ability, call this function to check if another ability is active before attempting to activate the new one:
    TSubclassOf<UGameplayAbility> NewAbility = ...; // The ability you want to activate
    FGameplayTagContainer IgnoreTags = ...; // The tags of the ability you want to ignore
    
    if (!IsAnotherAbilityActive(NewAbility, IgnoreTags))
    {
        GetAbilitySystemComponent()->TryActivateAbilityByClass(NewAbility);
    }
    

    This code will prevent the new ability from being activated if another ability with different tags is already active. Make sure to adjust the class names and function names according to your project. This should work in Unreal Engine 5.2 with the GAS plugin.

  9. User

    How do I cancel an ability from the FGameplayAbilitySpec?

  10. Assistant

    To cancel an ability from the FGameplayAbilitySpec, you can use the UAbilitySystemComponent::CancelAbility function. Here's an example:

    1. First, create a function in your character or player controller class that takes an FGameplayAbilitySpec as input:
    UCLASS()
    class YOURGAME_API AYourCharacter : public ACharacter
    {
        GENERATED_BODY()
    
        // ...
    
    public:
        UFUNCTION(BlueprintCallable, Category = "Abilities")
        void CancelAbilityFromSpec(FGameplayAbilitySpec AbilitySpec);
    };
    
    1. Implement the function in your .cpp file:
    void AYourCharacter::CancelAbilityFromSpec(FGameplayAbilitySpec AbilitySpec)
    {
        UAbilitySystemComponent* AbilitySystemComponent = GetAbilitySystemComponent(); // You may have a different function to get the AbilitySystemComponent.
        if (AbilitySystemComponent)
        {
            // Calling the CancelAbility function of AbilitySystemComponent
            AbilitySystemComponent->CancelAbility(AbilitySpec.Ability);
        }
    }
    
    1. When you want to cancel an ability, call this function and pass the FGameplayAbilitySpec you want to cancel:
    FGameplayAbilitySpec AbilitySpecToCancel = ...; // The ability you want to cancel
    
    CancelAbilityFromSpec(AbilitySpecToCancel);
    

    This code will cancel the given ability if it's currently active. Make sure to adjust the class names and function names according to your project.

  11. User

    How do I return a spec from a method the cleanest?

  12. Assistant

    To return an FGameplayAbilitySpec from a method in a clean and efficient way, you could use a pointer to FGameplayAbilitySpec and return a TArray<FGameplayAbilitySpec*>. Here's an example:

    1. First, create a function in your character or player controller class that returns a pointer to FGameplayAbilitySpec:
    UCLASS()
    class YOURGAME_API AYourCharacter : public ACharacter
    {
        GENERATED_BODY()
    
        // ...
    
    public:
        UFUNCTION(BlueprintCallable, Category = "Abilities")
        TArray<FGameplayAbilitySpec*> GetActiveAbilitiesWithTags(FGameplayTagContainer AbilityTags) const;
    };
    
    1. Implement the function in your .cpp file:
    TArray<FGameplayAbilitySpec*> AYourCharacter::GetActiveAbilitiesWithTags(FGameplayTagContainer AbilityTags) const
    {
        TArray<FGameplayAbilitySpec*> ActiveAbilities;
    
        const UAbilitySystemComponent* AbilitySystemComponent = GetAbilitySystemComponent(); // You may have a different function to get the AbilitySystemComponent.
    
        if (AbilitySystemComponent)
        {
            TArray<FGameplayAbilitySpec*> AllActiveAbilities;
            AbilitySystemComponent->GetActiveAbilitiesWithTags(AbilityTags, AllActiveAbilities);
    
            // Filter the active abilities based on your criteria (e.g., certain tags)
            for (FGameplayAbilitySpec* ActiveAbilitySpec : AllActiveAbilities)
            {
                if (ActiveAbilitySpec->HasAllOfTags(AbilityTags))
                {
                    ActiveAbilities.Add(ActiveAbilitySpec);
                }
            }
        }
    
        return ActiveAbilities;
    }
    
    1. Call this function to get the FGameplayAbilitySpec pointers of the desired abilities:
    FGameplayTagContainer DesiredAbilityTags = ...; // The tags you want to search for
    TArray<FGameplayAbilitySpec*> DesiredAbilities = GetActiveAbilitiesWithTags(DesiredAbilityTags);
    

    By returning an array of FGameplayAbilitySpec pointers, you minimize the overhead of copying the structure and follow a cleaner and more efficient pattern. Note that you should be cautious when accessing pointers and always check for validity before using them.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.