Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MMC for ability cost and CanActivateAbility() #137

Open
campbt opened this issue Aug 11, 2024 · 0 comments
Open

MMC for ability cost and CanActivateAbility() #137

campbt opened this issue Aug 11, 2024 · 0 comments

Comments

@campbt
Copy link

campbt commented Aug 11, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant