Download

Gameplay Ability System - Custom ChanceToApplyToTarget

I would like to be able to dynamically control the ChanceToApply on a UGameplayEffect. The ‘ChanceToApply’ variable works, but I’m not sure if it would give me the flexibility I’d like.

Currently working on an RPG game in which I would like the ChanceToApply be based on a dice roll and then be able to add this roll to a Combat Log before attempting to apply the effect.

So far I’ve set up the GamePlayAbility system such that the GameplayAbility is triggering, the GameplayEffect is firing, and the following UGameplayEffectExecutionCalculation is running… but the GameplayEffect still applies every time! I set the spec to a 0% hit chance in the Execution. What have I done incorrectly here?


#include "BaseToHitCalculation.h"
#include "Math/UnrealMathUtility.h"
#include "GameplayEffect.h"

void UBaseToHitCalculation::Execute_Implementation(const FGameplayEffectCustomExecutionParameters& ExecutionParams, FGameplayEffectCustomExecutionOutput& OutExecutionOutput) const {

int32 d20Roll = FMath::RandRange(1, 20);
UE_LOG(LogTemp, Error, TEXT("D20 roll: %d"), d20Roll);

FGameplayEffectSpec Spec = ExecutionParams.GetOwningSpec();

UE_LOG(LogTemp, Error, TEXT("SETTING HIT CHANGE TO MISS"));
Spec.ChanceToApplyToTarget = 0.0f;

}

So I’ve managed to find a way to do what I was trying to do, but I’m not sure if it is the ‘recommended’ way. The Gameplay Ability System can feel overwhelming at times.



void UBaseToHitCalculation::Execute_Implementation(const FGameplayEffectCustomExecutionParameters& ExecutionParams, FGameplayEffectCustomExecutionOutput& OutExecutionOutput) const {

  int32 d20Roll = FMath::RandRange(1, 20);
  UE_LOG(LogTemp, Error, TEXT("D20 roll: %d"), d20Roll);


  if (d20Roll > 10) {
    FGameplayModifierEvaluatedData DamageModifier = FGameplayModifierEvaluatedData();
    DamageModifier.Attribute = URPGAttributes::GetHpAttribute();
    DamageModifier.ModifierOp = EGameplayModOp::Type::Additive;
    DamageModifier.IsValid = true;
    DamageModifier.Magnitude = -1.0f;
    OutExecutionOutput.AddOutputModifier(DamageModifier);
  }
}


Turns out the above will not work when the GameplayEffect is part of a ‘Cost’ for a GameplayAbility. Even though the modifier is executed, it does not seem to be picked up by the ‘CheckCost()’ function.

Meaning if you’re using an execution to manually create modifiers, abilities can be executed even when they do not meet the cost requirements. I’m a bit lost.