Apply Damage To Interactor
Type: Plugin/Engine only
This full actor example damages whoever interacts with it, using Unreal's standard damage pipeline.
Example
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Data/EDInteractionTypes.h"
#include "MyTrapConsoleActor.generated.h"
class UArrowComponent;
class UEDInteractableComponent;
class USceneComponent;
class UStaticMeshComponent;
UCLASS()
class AMyTrapConsoleActor : public AActor
{
GENERATED_BODY()
public:
AMyTrapConsoleActor();
protected:
virtual void BeginPlay() override;
UPROPERTY(VisibleAnywhere)
TObjectPtr<USceneComponent> Root;
UPROPERTY(VisibleAnywhere)
TObjectPtr<UStaticMeshComponent> ConsoleMesh;
UPROPERTY(VisibleAnywhere)
TObjectPtr<USceneComponent> WidgetHolder;
UPROPERTY(VisibleAnywhere)
TObjectPtr<UArrowComponent> CenterArrow;
UPROPERTY(VisibleAnywhere)
TObjectPtr<UEDInteractableComponent> InteractableComponent;
UPROPERTY(EditAnywhere, Category = "Damage")
float DamageToApply = 15.f;
UFUNCTION()
void HandleInteractionSuccessful(const FEDInteractionEventContext& Context);
};
#include "MyTrapConsoleActor.h"
#include "Components/ArrowComponent.h"
#include "Components/EDInteractableComponent.h"
#include "Components/SceneComponent.h"
#include "Components/StaticMeshComponent.h"
#include "GameFramework/Pawn.h"
#include "Kismet/GameplayStatics.h"
AMyTrapConsoleActor::AMyTrapConsoleActor()
{
Root = CreateDefaultSubobject<USceneComponent>(TEXT("Root"));
SetRootComponent(Root);
ConsoleMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("ConsoleMesh"));
ConsoleMesh->SetupAttachment(Root);
ConsoleMesh->ComponentTags.Add(FName(TEXT("DimensionComponent")));
WidgetHolder = CreateDefaultSubobject<USceneComponent>(TEXT("WidgetHolder"));
WidgetHolder->SetupAttachment(Root);
WidgetHolder->SetRelativeLocation(FVector(0.f, 0.f, 70.f));
WidgetHolder->ComponentTags.Add(FName(TEXT("WidgetHolderComponent")));
CenterArrow = CreateDefaultSubobject<UArrowComponent>(TEXT("CenterArrow"));
CenterArrow->SetupAttachment(ConsoleMesh);
CenterArrow->ComponentTags.Add(FName(TEXT("CenterComponent")));
InteractableComponent = CreateDefaultSubobject<UEDInteractableComponent>(TEXT("InteractableComponent"));
}
void AMyTrapConsoleActor::BeginPlay()
{
Super::BeginPlay();
InteractableComponent->OnInteractionSuccessful.AddDynamic(this, &AMyTrapConsoleActor::HandleInteractionSuccessful);
}
void AMyTrapConsoleActor::HandleInteractionSuccessful(const FEDInteractionEventContext& Context)
{
APawn* InstigatorPawn = Context.Instigator ? Context.Instigator->GetPawn() : nullptr;
if (!InstigatorPawn)
{
return;
}
UGameplayStatics::ApplyDamage(InstigatorPawn, DamageToApply, Context.Instigator, this, UDamageType::StaticClass());
}
Why this is a good pattern
- It works with Unreal's standard health/damage systems.
- It composes cleanly with effects, UI, and cooldowns.