Skip to content

MMC for ability cost and CanActivateAbility() #137

@campbt

Description

@campbt

Hey, there may be an issue with the recommendation in the guide regarding a universal MMC for the ability cost that is referencing a value on the ability class.

The recommended code is as follows:

float UPGMMC_HeroAbilityCooldown::CalculateBaseMagnitude_Implementation(const FGameplayEffectSpec & Spec) const
{
	const UPGGameplayAbility* Ability = Cast<UPGGameplayAbility>(Spec.GetContext().GetAbilityInstance_NotReplicated());

	if (!Ability)
	{
		return 0.0f;
	}

	return Ability->CooldownDuration.GetValueAtLevel(Ability->GetAbilityLevel());
}

I've been using this, and it works when activating an ability. The correct cost for the ability level is deducted, and the ability fails to cast if not enough of the resource exists.

The issue seems to be if calling

Spec.Ability->CanActivateAbility(
    Spec.Handle, 
    AbilitySystem->AbilityActorInfo.Get(), 
    nullptr, 
    nullptr,
    &FailureTags
)

Or, specifically, just:

Spec.Ability->CheckCost(
    Spec.Handle,
    AbilitySystem->AbilityActorInfo.Get(),
    nullptr
)

In these methods, the MMC's ability's level is wrong. When I call CanAcitvateAbility, I confirmed that Spec.Level != 1, but the cost checked is always using the level 1 value. This means CanActivateAbility may return an incorrect value (a false positive if the cost at level 1 is less than the current level, or a false negative if the cost at level 1 is higher than the current level.)

I played around with the MMC and determined that Ability->GetAbilityLevel() returns 1 in the above methods, but Spec->Level remains correct both when casting the ability and when checking via CanActivateAbility()/CheckCost()


I recommend changing the guide's code to:

float UPGMMC_HeroAbilityCooldown::CalculateBaseMagnitude_Implementation(const FGameplayEffectSpec & Spec) const
{
	const UPGGameplayAbility* Ability = Cast<UPGGameplayAbility>(Spec.GetContext().GetAbilityInstance_NotReplicated());

	if (!Ability)
	{
		return 0.0f;
	}

	return Ability->CooldownDuration.GetValueAtLevel(Spec->Level);
}

Unless you know of a reason when this would be incorrect.


I'd also like to thank you for this incredible guide. It's been an absolute lifesaver using GAS

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions