Damage-Based Resource Node
Type: Plugin/Engine only
This example shows a complete harvest/mining node actor using OnInteractionDamaged.
Example
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Data/EDInteractionTypes.h"
#include "MyOreNode.generated.h"
class UArrowComponent;
class UEDInteractableComponent;
class USceneComponent;
class UStaticMeshComponent;
UCLASS()
class AMyOreNode : public AActor
{
GENERATED_BODY()
public:
AMyOreNode();
protected:
virtual void BeginPlay() override;
UPROPERTY(VisibleAnywhere)
TObjectPtr<USceneComponent> Root;
UPROPERTY(VisibleAnywhere)
TObjectPtr<UStaticMeshComponent> OreMesh;
UPROPERTY(VisibleAnywhere)
TObjectPtr<USceneComponent> WidgetHolder;
UPROPERTY(VisibleAnywhere)
TObjectPtr<UArrowComponent> CenterArrow;
UPROPERTY(VisibleAnywhere)
TObjectPtr<UEDInteractableComponent> InteractableComponent;
UFUNCTION()
void HandleDamaged(const FEDInteractionEventContext& Context, float Damage, bool bReachedZeroHP, float NewHitPoints);
};
#include "MyOreNode.h"
#include "Components/ArrowComponent.h"
#include "Components/EDInteractableComponent.h"
#include "Components/SceneComponent.h"
#include "Components/StaticMeshComponent.h"
AMyOreNode::AMyOreNode()
{
Root = CreateDefaultSubobject<USceneComponent>(TEXT("Root"));
SetRootComponent(Root);
OreMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("OreMesh"));
OreMesh->SetupAttachment(Root);
OreMesh->ComponentTags.Add(FName(TEXT("DimensionComponent")));
WidgetHolder = CreateDefaultSubobject<USceneComponent>(TEXT("WidgetHolder"));
WidgetHolder->SetupAttachment(Root);
WidgetHolder->ComponentTags.Add(FName(TEXT("WidgetHolderComponent")));
CenterArrow = CreateDefaultSubobject<UArrowComponent>(TEXT("CenterArrow"));
CenterArrow->SetupAttachment(OreMesh);
CenterArrow->ComponentTags.Add(FName(TEXT("CenterComponent")));
InteractableComponent = CreateDefaultSubobject<UEDInteractableComponent>(TEXT("InteractableComponent"));
}
void AMyOreNode::BeginPlay()
{
Super::BeginPlay();
InteractableComponent->OnInteractionDamaged.AddDynamic(this, &AMyOreNode::HandleDamaged);
}
void AMyOreNode::HandleDamaged(const FEDInteractionEventContext& Context, float Damage, bool bReachedZeroHP, float NewHitPoints)
{
UE_LOG(LogTemp, Log, TEXT("Ore node damaged: %.2f, remaining HP: %.2f"), Damage, NewHitPoints);
if (bReachedZeroHP)
{
UE_LOG(LogTemp, Log, TEXT("Node depleted"));
}
}
Typical extensions
- play effects with
UEDInteractionEffectsComponent - spawn loot when HP reaches zero
- respawn the node later from your own gameplay system