GAS how to get the Effect Target from the Effect Spec?

I have a Question about GAS.
I have created a custom UGameplayModMagnitudeCalculation. In the overwritten CalculateBaseMagnitude_Implementation function, I need the target of this effect. The effect for sure needs to know to whom it is applied.
I have the effect Spec but I can’t seem to find the target from it. While I can capture attributes and tags and also I 've found two GetContext Methods returning the same context, one const. Can someone tell me how I can get the target in this function without making a custom TargetData?
Thanks for your answer.

GameplayEffectContext should contain information about the target, but in any case you want to use GetCapturedAttributeMagnitude() to get the relevant target information, similar to how you would in GameplayEffectExecutionCalculation. See GitHub - tranek/GASDocumentation: My understanding of Unreal Engine 4's GameplayAbilitySystem plug for an example / overall everything you need for GAS.

2 Likes

After a DEEP dive into possibilities and research about this, I found out that there is no straight workaround or proper GAS was to get the ASC or AvatarActor from the MMCalculation class, given it’s nature.

But as a workaround to this, I found two nice potential solutions to this.

  1. If you are using it, you can get the target Actor/Component by the HitResult
Spec.GetContext().GetHitResult()->GetActor();
Spec.GetContext().GetHitResult()->GetComponent();

The downside of this approach is that if you are not applying the GE based on a HitResult, this won’t be a nice way (besides, you could add a HitResult anyways and set Actor manually

  1. The second option is way more versatile, sadly anyways we need to add information manually but it works.

When creating your FGameplayEffectContextHandle, add your target actor like this:

FGameplayEffectContextHandle Context;
TArray<TWeakObjectPtr<AActor>> TargetActorArray;
TargetActorArray.Add(Target);
Context.AddActors(TargetActorArray);

This array of actors is part of the context and you can get those actors (in my case, I’m storing only the target actor).

Spec.GetContext().GetActors();

I would love a better solution, but when there is no other chance, in my opinion, it is more important to not slow down development and use the tools that you do have.

1 Like