Skip to content

Instantly share code, notes, and snippets.

@onionmk2
Last active June 29, 2024 22:11
Show Gist options
  • Save onionmk2/9a36e7e4e1a53e21f081e522b1373720 to your computer and use it in GitHub Desktop.
Save onionmk2/9a36e7e4e1a53e21f081e522b1373720 to your computer and use it in GitHub Desktop.
https://www.udemy.com/course/unreal-engine-5-gas-top-down-rpg 44. Infinite Effect Application and Removal
UAbilitySystemComponent* ASC = UAbilitySystemBlueprintLibrary::GetAbilitySystemComponent(TargetActor);
if (!IsValid(ASC))
{
return;
}
// Create a Query to specify the GE to be removed.
// Similar to the implementation of UAbilitySystemComponent::RemoveActiveGameplayEffectBySourceEffect, but with additional check by SourceObject.
FGameplayEffectQuery Query;
UAbilitySystemComponent* InstigatorAbilitySystemComponent = ASC;
constexpr int StacksToRemove = 1;
Query.CustomMatchDelegate.BindLambda([&](const FActiveGameplayEffect& CurEffect)
{
bool bMatches = false;
if (CurEffect.Spec.Def && InfiniteGameplayEffectClass == CurEffect.Spec.Def->GetClass())
{
if (InstigatorAbilitySystemComponent)
{
const bool bDoesASCEqualToSpec = InstigatorAbilitySystemComponent == CurEffect.Spec.GetEffectContext().GetInstigatorAbilitySystemComponent();
const bool bDoesSourceObjectEqual = this == CurEffect.Spec.GetEffectContext().GetSourceObject(); // additional check by SourceObject.
bMatches = bDoesASCEqualToSpec && bDoesSourceObjectEqual;
}
}
return bMatches;
});
// Use Query to remove GE from ASC.
// UAbilitySystemComponent::GetActiveGameplayEffects returns const, so we are forced to use const_cast.
const FActiveGameplayEffectsContainer& ActiveGameplayEffectsContainer= ASC->GetActiveGameplayEffects();
FActiveGameplayEffectsContainer& GEs = const_cast<FActiveGameplayEffectsContainer&>(ActiveGameplayEffectsContainer);
GEs.RemoveActiveEffects(Query, StacksToRemove);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment