Creating a Pickup Base Actor Class for Unreal Engine in a Third-Person Shooter

Unreal Engine

In this article, I will share the header file (.h) of a PickupBaseActor class that I wrote for my third-person shooting action game. This class serves as a base class for various types of pickups such as health, armor, quest items, and more. Developers can easily expand this into a .cpp file and add their custom functionality.

Why a Pickup Base Actor?

In Unreal Engine, using a pickup base class allows developers to streamline the process of handling different types of pickups. You can have consistent behavior across items like health, armor, or weapons, while customizing their effects and characteristics.

The PickupBaseActor Header File

Here is the header file for the PickupBaseActor class:


#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Components/StaticMeshComponent.h"
#include "Sound/SoundBase.h"
#include "NiagaraSystem.h"
#include "UObject/NoExportTypes.h"
#include "PickupBaseActor.generated.h"

// Enum for different types of pickups
UENUM(BlueprintType)
enum class EPickupType : uint8
{
    Health UMETA(DisplayName = "Health"),
    Armor UMETA(DisplayName = "Armor"),
    QuestItem UMETA(DisplayName = "Quest Item"),
    Trap UMETA(DisplayName = "Trap"),
    Shield UMETA(DisplayName = "Shield"),
    Coin UMETA(DisplayName = "Coin"),
    Ammo UMETA(DisplayName = "Ammo"),
    Key UMETA(DisplayName = "Key"),
    Weapon UMETA(DisplayName = "Weapon"),
    PowerUp UMETA(DisplayName = "Power Up"),
    Collectible UMETA(DisplayName = "Collectible"),
    Special UMETA(DisplayName = "Special"),
    Resource UMETA(DisplayName = "Resource"),
    Consumable UMETA(DisplayName = "Consumable"),
    Tool UMETA(DisplayName = "Tool"),
    Artifact UMETA(DisplayName = "Artifact"),
    Material UMETA(DisplayName = "Material")
};

// Struct to hold pickup item data
USTRUCT(BlueprintType)
struct FPickupItemData
{
    GENERATED_BODY()

    // The type of pickup item
    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Pickup")
    EPickupType PickupType;

    // The value of the pickup item (e.g., health amount, armor amount)
    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Pickup")
    float ItemValue;

    // Special tag for quest items or other purposes
    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Pickup")
    FName SpecialTag;

    // The rarity of the item (common, rare, epic, etc.)
    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Pickup")
    FString ItemRarity;

    // Notes or custom messages associated with the item
    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Pickup")
    FString ItemNotes;

    // The duration of the item's effect (if applicable)
    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Pickup")
    float EffectDuration;

    // Respawn time after being picked up (if applicable)
    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Pickup")
    float RespawnTime;

    // Quantity for stackable items
    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Pickup")
    int32 Quantity;

    // Image or icon to represent the item in the UI
    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "UI")
    UTexture2D* ItemImage;

    // The color associated with the item for UI and text representation
    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "UI")
    FLinearColor ItemColor;

    // Expiration time for time-limited items
    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Pickup")
    float ExpirationTime;

    // Durability of the item (for items that degrade over time)
    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Pickup")
    float Durability;

    // Weight multiplier based on the item's condition or type
    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Pickup")
    float WeightMultiplier;

    // Reference to the owner or entity who dropped the item (if applicable)
    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Pickup")
    AActor* ItemOwner;
};

UCLASS()
class ASHENVEIL_API APickupBaseActor : public AActor
{
    GENERATED_BODY()

public:	
    // Sets default values for this actor's properties
    APickupBaseActor();

protected:
    // Called when the game starts or when spawned
    virtual void BeginPlay() override;
    // Handle for managing the cooldown timer
    FTimerHandle CooldownTimerHandle;

public:	
    // Called every frame
    virtual void Tick(float DeltaTime) override;

    // The static mesh for the pickup item
    UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Components")
    UStaticMeshComponent* PickupMesh;

    // The data associated with this pickup item
    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Pickup")
    FPickupItemData PickupItemData;

    // Niagara VFX for the pickup item
    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Effects")
    UNiagaraSystem* PickupVFX;

    // Sound effect 1 to play when interacting with the pickup
    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Audio")
    USoundBase* PickupSound1;

    // Sound effect 2 to play when interacting with the pickup
    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Audio")
    USoundBase* PickupSound2;

    // Sound effect to play on destruction of the pickup
    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Audio")
    USoundBase* DestroySound;

    // Whether the item is consumable or reusable
    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Pickup")
    bool bIsConsumable;

    // Cooldown time before the item can be picked up again
    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Pickup")
    float CooldownTime;

    // Weight of the pickup item (useful for inventory systems)
    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Pickup")
    float ItemWeight;

    // Function to activate the pickup (to be called when the player interacts with it)
    UFUNCTION(BlueprintCallable, Category = "Pickup")
    virtual void ActivatePickup();

    // Function to handle the destruction of the pickup
    UFUNCTION(BlueprintCallable, Category = "Pickup")
    virtual void DestroyPickup();

    // Function to apply the effect of the pickup item to the player
    UFUNCTION(BlueprintCallable, Category = "Pickup")
    virtual void ApplyEffect();

    // Function to check if the pickup is available based on conditions (like cooldown or player state)
    UFUNCTION(BlueprintCallable, Category = "Pickup")
    virtual bool IsPickupAvailable() const;

    // Function to handle the cooldown logic
    UFUNCTION(BlueprintCallable, Category = "Pickup")
    virtual void StartCooldown();

    // Function to handle quest-related logic when the item is picked up
    UFUNCTION(BlueprintCallable, Category = "Pickup")
    virtual void HandleQuestItem();

    // Function to trigger destruction logic, such as playing effects and sounds
    UFUNCTION(BlueprintCallable, Category = "Pickup")
    virtual void TriggerDestruction();

    // Function to respawn the item after a certain time (if applicable)
    UFUNCTION(BlueprintCallable, Category = "Pickup")
    virtual void RespawnPickup();

    // Function to check and handle item expiration
    UFUNCTION(BlueprintCallable, Category = "Pickup")
    virtual void CheckExpiration();

    // Function to reduce item durability over time or usage
    UFUNCTION(BlueprintCallable, Category = "Pickup")
    virtual void ReduceDurability(float Amount);
};

This code defines the foundation of a pickup system in your game. You can handle different types of items such as Health, Armor, Weapons, and more by defining specific types using the EPickupType enum.

Customizing the Pickup

This class includes a variety of properties and methods that you can expand in the .cpp file. Some of the most important methods include:

  • ActivatePickup: Called when the player interacts with the pickup.
  • ApplyEffect: Used to apply the specific effect of the pickup (such as restoring health).
  • StartCooldown: Handles the cooldown mechanism, making the pickup temporarily unavailable after use.
  • HandleQuestItem: For special quest items, this method can be expanded to trigger quest-related actions.
  • TriggerDestruction: Plays destruction effects and sounds when the pickup is destroyed.

Expanding the System

Some ideas for future expansion include:

  • Inventory System Integration: Allow players to store items and manage them in an inventory.
  • Niagara VFX: Add Niagara particle effects to enhance the appearance of the pickups.
  • Multiplayer Support: Ensure that your pickup system works in a multiplayer environment by handling replication.

Learn More About Unreal Engine

To deepen your understanding of Unreal Engine’s capabilities, check out its history here.

External Resources

  1. Official Unreal Engine Documentation
  2. Game Development on Stack Overflow
  3. Niagara VFX for Unreal Engine

By starting with this foundation, you can easily add your own functionality in the .cpp file and create a flexible pickup system tailored to your game’s needs.


Stay tuned for more game development articles on Rambod.net where I explore Unreal Engine development in greater detail.

Recommended Posts

No comment yet, add your voice below!


Add a Comment

Your email address will not be published. Required fields are marked *

five × 1 =