Skip to content

Commit

Permalink
Implemented Apply Effect using GAS
Browse files Browse the repository at this point in the history
Removed const_casts and replaced with EffectSpecs
  • Loading branch information
deanhsjones committed Dec 11, 2023
1 parent 8b67e85 commit c839b81
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 47 deletions.
Binary file modified Content/Blueprints/Actor/BP_HealthPotion.uasset
Binary file not shown.
Binary file modified Content/Maps/StartupMap.umap
Binary file not shown.
82 changes: 48 additions & 34 deletions Source/Aura/Private/Actor/AuraEffectActor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,66 @@


#include "Actor/AuraEffectActor.h"
#include "AbilitySystemBlueprintLibrary.h"

#include "AbilitySystemComponent.h"
#include "AbilitySystemInterface.h"
#include "AbilitySystem/AuraAttributeSet.h"

#include "Components/SphereComponent.h"
//#include "AbilitySystemInterface.h"
//#include "AbilitySystem/AuraAttributeSet.h"
//
//#include "Components/SphereComponent.h"


AAuraEffectActor::AAuraEffectActor()
{
PrimaryActorTick.bCanEverTick = false;

Mesh = CreateDefaultSubobject<UStaticMeshComponent>("Mesh");
SetRootComponent(Mesh);
Sphere = CreateDefaultSubobject<USphereComponent>("Sphere");
Sphere->SetupAttachment(GetRootComponent());

//Mesh = CreateDefaultSubobject<UStaticMeshComponent>("Mesh");
//SetRootComponent(Mesh);
//Sphere = CreateDefaultSubobject<USphereComponent>("Sphere");
//Sphere->SetupAttachment(GetRootComponent());

SetRootComponent(CreateDefaultSubobject<USceneComponent>("SceneRoot"));

}

void AAuraEffectActor::OnOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor,
UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
//void AAuraEffectActor::OnOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor,
// UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
//{
//
// //TO DO: Change this to apply a Gameplay Effect; for now using const cast as a hack just to see how it works ;)
//
// if (IAbilitySystemInterface* ASCInterface = Cast<IAbilitySystemInterface>(OtherActor))
// {
// const UAuraAttributeSet* AuraAttributeSet = Cast<UAuraAttributeSet>(ASCInterface->GetAbilitySystemComponent()->GetAttributeSet(UAuraAttributeSet::StaticClass()));
// //here is the const cast! This is dangerous and should be changed!
// UAuraAttributeSet* MutableAuraAttributeSet = const_cast<UAuraAttributeSet*>(AuraAttributeSet);
// MutableAuraAttributeSet->SetHealth(AuraAttributeSet->GetHealth() + 25.f);
// MutableAuraAttributeSet->SetMana(AuraAttributeSet->GetMana() - 25.f);
// //that was the const cast! Change it later!
// Destroy();
// }
//}
//
//void AAuraEffectActor::EndOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor,
// UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
//{
//}

//TO DO: Change this to apply a Gameplay Effect; for now using const cast as a hack just to see how it works ;)

if (IAbilitySystemInterface* ASCInterface = Cast<IAbilitySystemInterface>(OtherActor))
{
const UAuraAttributeSet* AuraAttributeSet = Cast<UAuraAttributeSet>(ASCInterface->GetAbilitySystemComponent()->GetAttributeSet(UAuraAttributeSet::StaticClass()));
//here is the const cast! This is dangerous and should be changed!
UAuraAttributeSet* MutableAuraAttributeSet = const_cast<UAuraAttributeSet*>(AuraAttributeSet);
MutableAuraAttributeSet->SetHealth(AuraAttributeSet->GetHealth() + 25.f);
MutableAuraAttributeSet->SetMana(AuraAttributeSet->GetMana() - 25.f);
//that was the const cast! Change it later!
Destroy();
}
}

void AAuraEffectActor::EndOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor,
UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
}
//void AAuraEffectActor::BeginPlay()
//{
// //Super::BeginPlay();
// //Sphere->OnComponentBeginOverlap.AddDynamic(this, &AAuraEffectActor::OnOverlap);
// //Sphere->OnComponentEndOverlap.AddDynamic(this, &AAuraEffectActor::EndOverlap);
//}

void AAuraEffectActor::BeginPlay()
void AAuraEffectActor::ApplyEffectToTarget(AActor* Target, TSubclassOf<UGameplayEffect> GameplayEffectClass)
{
Super::BeginPlay();
Sphere->OnComponentBeginOverlap.AddDynamic(this, &AAuraEffectActor::OnOverlap);
Sphere->OnComponentEndOverlap.AddDynamic(this, &AAuraEffectActor::EndOverlap);
UAbilitySystemComponent* TargetAbilitySystemComponent = UAbilitySystemBlueprintLibrary::GetAbilitySystemComponent(Target);
if (TargetAbilitySystemComponent == nullptr) return;

check(GameplayEffectClass);
FGameplayEffectContextHandle EffectContextHandle = TargetAbilitySystemComponent->MakeEffectContext();
EffectContextHandle.AddSourceObject(this);
const FGameplayEffectSpecHandle EffectSpecHandle = TargetAbilitySystemComponent->MakeOutgoingSpec(GameplayEffectClass, 1.f, EffectContextHandle);
TargetAbilitySystemComponent->ApplyGameplayEffectSpecToSelf(*EffectSpecHandle.Data.Get());
}
33 changes: 20 additions & 13 deletions Source/Aura/Public/Actor/AuraEffectActor.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
#include "AuraEffectActor.generated.h"


class USphereComponent;
//class UStaticMeshComponent;
//class USphereComponent;
class UGameplayEffect;


UCLASS()
class AURA_API AAuraEffectActor : public AActor
Expand All @@ -18,24 +19,30 @@ class AURA_API AAuraEffectActor : public AActor
public:
AAuraEffectActor();

UFUNCTION()
virtual void OnOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor,
UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
//UFUNCTION()
//virtual void OnOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor,
// UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);

UFUNCTION()
virtual void EndOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor,
UPrimitiveComponent* OtherComp, int32 OtherBodyIndex);
//UFUNCTION()
//virtual void EndOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor,
// UPrimitiveComponent* OtherComp, int32 OtherBodyIndex);



protected:
virtual void BeginPlay() override;
/*virtual void BeginPlay() override;*/

UFUNCTION(BlueprintCallable)
void ApplyEffectToTarget(AActor* Target, TSubclassOf<UGameplayEffect> GameplayEffectClass);

UPROPERTY(EditAnywhere, Category = "Applied Effects")
TSubclassOf<UGameplayEffect> InstantGameplayEffectClass;

private:
UPROPERTY(VisibleAnywhere)
TObjectPtr<USphereComponent> Sphere;
//UPROPERTY(VisibleAnywhere)
//TObjectPtr<USphereComponent> Sphere;

UPROPERTY(VisibleAnywhere)
TObjectPtr<UStaticMeshComponent> Mesh;
//UPROPERTY(VisibleAnywhere)
//TObjectPtr<UStaticMeshComponent> Mesh;

};

0 comments on commit c839b81

Please sign in to comment.