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

So I am using UE5.2 and GAS. In my game, when you click on an enemy, the player…

  1. User

    So I am using UE5.2 and GAS. In my game, when you click on an enemy, the player character will walk to the enemy and start attacking him. I also have a way for the user to press an ability and use on the monster. The player will walk to the monster based on an attack range and start a cast ability bar if available and cast the ability once the cast bar finishes. I am having a problem where if I use an ability on a monster and then immediately left click the monster to auto attack, it will start the cast bar and ability while on top of that auto attacking the monster. We shouldn't be using any targeted abilities if we are going to auto attack a monster so it should cancel the cast ability when we go auto attack a monster. How should I do this?

  2. Assistant

    To prevent the ability cast and auto-attack from overlapping, you can add a "cancel ability" function and call this function before starting the auto-attack. You can follow these steps to implement this logic:

    1. Go to the blueprint where your ability-related logic is handled (possibly in PlayerController or PlayerCharacter). Create a new function called CancelAbility:
    • In this function, remove any target indicators.
    • Reset or stop the cast ability bar if it's in progress.
    • Disable any post-casting logic.
    1. In the same Blueprint, find the place where you handle the auto-attack logic. Before starting the auto-attack, call the newly created CancelAbility function.
    • Check if the targeted ability cast is in progress, and if so, call the CancelAbility function.
    • It's important to call the CancelAbility function before initiating the auto-attack logic so that the running cast is cancelled instantly.

    This way, when you click on a monster, it will cancel the ability casting and start auto-attacking the monster as you intend.

  3. User

    Can you show me c++ code of this?

  4. Assistant

    Sure! Here's a C++ implementation of the CancelAbility function and its usage.

    1. In your PlayerController or PlayerCharacter header file (.h), declare the function CancelAbility():
    // PlayerController.h or PlayerCharacter.h
    public:
        UFUNCTION(BlueprintCallable, Category = "Abilities")
        void CancelAbility();
    
    1. In your PlayerController or PlayerCharacter source file (.cpp), implement the CancelAbility() function:
    // PlayerController.cpp or PlayerCharacter.cpp
    void AYourPlayerController::CancelAbility()
    {
        // Remove any target indicators.
        // Reset or stop the cast ability bar if it's in progress.
        // Disable any post-casting logic.
    
        // Example:
        CastAbilityBar->SetVisibility(false);
        GetWorld()->GetTimerManager().ClearTimer(AbilityCastHandle);
    }
    
    1. In the same source file (.cpp), find the place where you handle the auto-attack logic. Before starting the auto-attack, call the CancelAbility() function:
    void AYourPlayerController::AutoAttack(AEnemy* TargetEnemy)
    {
        if (TargetEnemy)
        {
            // Check if the targeted ability cast is in progress, and if so, call the CancelAbility function.
            if (AbilityCastHandle.IsValid() && GetWorld()->GetTimerManager().GetTimerRemaining(AbilityCastHandle) > 0)
            {
                CancelAbility();
            }
    
            // Continue with your auto-attack logic
            // Example:
            MoveToEnemy(TargetEnemy);
            StartAutoAttack(TargetEnemy);
        }
    }
    

    Make sure to replace AYourPlayerController with the name of your actual PlayerController or PlayerCharacter class, and modify the example logic to fit your actual implementation.

    This way, when you click on a monster, it will call CancelAbility before auto-attacking the monster to prevent overlapping.