Course contentsShow
← Unreal Engine Game Development
Lesson 651 of 8809. Gameplay Ability System (GAS)Free lesson

Implementing Damage Formulas with Execution Calculations

Calculate dynamic damage values by combining attacker and defender attributes within a custom execution calculation to support complex RPG mechanics.

In this lesson, you will learn how to build a dynamic RPG damage formula by mathematically combining attacker and defender attributes inside a UGameplayEffectExecutionCalculation.

Why Use Execution Calculations?

While standard Gameplay Effect modifiers (like "Add 10 Damage") are great for simple cases, they cannot handle complex, multi-attribute interactions. An Execution Calculation allows you to access the current state of both the Source (attacker) and Target (defender) attributes at the exact moment the effect is applied, enabling formulas like Damage = (AttackPower * 2.0) - (DefensePower * 0.5).

Capturing Attributes

First, ensure you have defined your FGameplayEffectAttributeCaptureDefinition structs in your Execution Calculation’s constructor. You must capture the relevant attributes (e.g., AttackPower from the Source and DefensePower from the Target) so they are available during the Execute_Implementation call.

Implementing the Formula

Once attributes are captured, you use the FAggregatorEvaluateParameters to retrieve their current values. You can then perform your custom math logic:

void UMyDamageExecution::Execute_Implementation(...) const
{
    // Retrieve captured values
    float AttackPower = 0.0f;
    ExecutionParams.AttemptCalculateCapturedAttributeMagnitude(AttackDef, EvalParams, AttackPower);

    float DefensePower = 0.0f;
    ExecutionParams.AttemptCalculateCapturedAttributeMagnitude(DefenseDef, EvalParams, DefensePower);

    // RPG Formula: Final Damage = (Attack * 1.5) - (Defense * 0.5)
    float DamageDone = (AttackPower * 1.5f) - (DefensePower * 0.5f);

    // Apply the result to the target
    if (DamageDone > 0.0f)
    {
        OutExecutionOutput.AddOutputModifier(FGameplayModifierEvaluatedData(
            UHealthAttributeSet::GetDamageAttribute(), 
            EGameplayModOp::Additive, 
            DamageDone));
    }
}

By manipulating the OutExecutionOutput, you can pass this dynamic value back to the Gameplay Ability System. Because this happens in C++ during the effect execution, the logic is performant and handles complex scaling perfectly, ensuring your game balance remains centralized within your C++ logic rather than scattered across individual Gameplay Effects.

Key Takeaway: Execution Calculations enable complex, state-dependent combat math by allowing you to capture and calculate multiple attributes from both attacker and defender in real-time.