How can I confirm the target data from waiting from a task in WaitTargetDataUsingActor…
- 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?
- 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:
- 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); };- Implement the function in your
.cppfile:
void AYourPlayerController::HandleTargetData(FGameplayAbilityTargetDataHandle TargetData) { // Process the target data as needed }- When calling
WaitTargetDataUsingActor, pass a reference to your player controller (or any other relevant class) to theExternalConfirmparameter:
UAbilityTask_WaitTargetDataUsingActor* Task = UAbilityTask_WaitTargetDataUsingActor::WaitTargetDataUsingActor(this, Name, StartLocation, Orientation, TaskParams, NULL, false, true, Reticle, Class, false);- In your
WaitTargetDatatask, 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; // ... }- Finally, call the
HandleTargetDatafunction 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
HandleTargetDatafunction 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. - 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?
- 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.
- 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?
- 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:
- 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(); };- Implement the
HandleLeftClickfunction:
void AYourPlayerController::HandleLeftClick() { // Check if the player is currently targeting for an ability // ... // Notify the target actor OnLeftClickDelegate.Broadcast(); // ... }- 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);- 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.
- 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
- Assistant
To check if another ability is active that differs from the ability you want to activate in GAS, you can use the
GetActiveAbilitiesWithTagsfunction from theUAbilitySystemComponent. Here's an example:- 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); };- Implement the function in your
.cppfile:
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; }- 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.
- User
How do I cancel an ability from the FGameplayAbilitySpec?
- Assistant
To cancel an ability from the
FGameplayAbilitySpec, you can use theUAbilitySystemComponent::CancelAbilityfunction. Here's an example:- First, create a function in your character or player controller class that takes an
FGameplayAbilitySpecas input:
UCLASS() class YOURGAME_API AYourCharacter : public ACharacter { GENERATED_BODY() // ... public: UFUNCTION(BlueprintCallable, Category = "Abilities") void CancelAbilityFromSpec(FGameplayAbilitySpec AbilitySpec); };- Implement the function in your
.cppfile:
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); } }- When you want to cancel an ability, call this function and pass the
FGameplayAbilitySpecyou 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.
- First, create a function in your character or player controller class that takes an
- User
How do I return a spec from a method the cleanest?
- Assistant
To return an
FGameplayAbilitySpecfrom a method in a clean and efficient way, you could use a pointer toFGameplayAbilitySpecand return aTArray<FGameplayAbilitySpec*>. Here's an example:- 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; };- Implement the function in your
.cppfile:
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; }- Call this function to get the
FGameplayAbilitySpecpointers of the desired abilities:
FGameplayTagContainer DesiredAbilityTags = ...; // The tags you want to search for TArray<FGameplayAbilitySpec*> DesiredAbilities = GetActiveAbilitiesWithTags(DesiredAbilityTags);By returning an array of
FGameplayAbilitySpecpointers, 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. - First, create a function in your character or player controller class that returns a pointer to
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.