RSExampleDashAbility.cpp
Click a line number to link it. Shift-click to select a range.
| 1 | // Copyright (c) 2026 Reignance Studios. MIT License, see LICENSE. |
| 2 | |
| 3 | #include "RSExampleDashAbility.h" |
| 4 | |
| 5 | #include "GameFramework/Character.h" |
| 6 | |
| 7 | URSExampleDashAbility::URSExampleDashAbility() |
| 8 | { |
| 9 | // Client runs it at once and asks the server; the server confirms or rolls back. |
| 10 | NetExecutionPolicy = EGameplayAbilityNetExecutionPolicy::LocalPredicted; |
| 11 | |
| 12 | // One instance per actor: no new UObject per activation, and state survives |
| 13 | // between the predicted and the confirmed run. |
| 14 | InstancingPolicy = EGameplayAbilityInstancingPolicy::InstancedPerActor; |
| 15 | |
| 16 | // Set in the Blueprint child or here: |
| 17 | // CostGameplayEffectClass = GE_Cost_Dash (Instant, Stamina -25) |
| 18 | // CooldownGameplayEffectClass = GE_Cooldown_Dash (HasDuration 1.5 s, grants Cooldown.Dash) |
| 19 | } |
| 20 | |
| 21 | void URSExampleDashAbility::ActivateAbility( |
| 22 | const FGameplayAbilitySpecHandle Handle, |
| 23 | const FGameplayAbilityActorInfo* ActorInfo, |
| 24 | const FGameplayAbilityActivationInfo ActivationInfo, |
| 25 | const FGameplayEventData* TriggerEventData) |
| 26 | { |
| 27 | // Commit first, inside the activation's prediction window. Committing after a |
| 28 | // latent task (a delay, a montage wait, target data) runs outside that window: |
| 29 | // the client cannot predict the cost or cooldown, so the UI jumps when the |
| 30 | // server value lands. |
| 31 | if (!CommitAbility(Handle, ActorInfo, ActivationInfo)) |
| 32 | { |
| 33 | EndAbility(Handle, ActorInfo, ActivationInfo, true, true); |
| 34 | return; |
| 35 | } |
| 36 | |
| 37 | if (ACharacter* Character = Cast<ACharacter>(ActorInfo->AvatarActor.Get())) |
| 38 | { |
| 39 | // Runs on the predicting client and on the server. Kept simple on purpose: |
| 40 | // launch timing can differ by a frame and cause a small correction. For a |
| 41 | // shipped dash, use a root motion source task or a custom CMC saved move. |
| 42 | const FVector Direction = Character->GetActorForwardVector(); |
| 43 | Character->LaunchCharacter(Direction * DashStrength, true, false); |
| 44 | } |
| 45 | |
| 46 | EndAbility(Handle, ActorInfo, ActivationInfo, true, false); |
| 47 | } |