Battery Collector Step #1 to #3

I am following the tutorial Battery Collector for Unreal Engine. This tutorial describes how to create pickups in C++.

Building The Base Level

I create a new project based on 3rd Person C++ template. I name this project BatteryCollector. In this project, I need to migrate assets from the Content Examples project. Be careful, there is an issue with the Content Examples in version 4.9: it doesn’t contain the P_electricity_arc effect. To have it, you have to install Content Examples in version 4.8. The assets to migrate are:

  • BP_DemoRoom
  • SM_Battery_Medium
  • P_electricity_arc

In the new project, I create a new level using the default template. I save it in a new folder Maps naming it CollectionLevel. I remove the floor and drag the BP_DemoRoom in it. In the Details panel, I click on “Mirror Room” to double the size, turn off the Lights,  Open Roof, Switch Color and Glass Walls.

BP_DemoRoom

I place the PlayerStart in the middle of the room, build the Lightning, and it’s ready to play:

BatteryCollectorSetupLevel

Making Your First Pickup Class

In the C++ Classes folder, I add a new Class. I choose Actor and name it “Pickup“. It opens Visual Studio, with the header and the cpp files already opened.

In the header file (Pickup.h), it is important to keep “Pickup.generated.h” as the last include.

Because the pickup is a very simple actor, in the constructor, I set PrimaryActorTick.bCanEverTick to false to improve performances:

PrimaryActorTick.bCanEverTick = false; 

In the header I add a private PickupMesh component and its public accessor:

    /** Return the mesh for pickup */
    FORCEINLINE class UStaticMeshComponent* GetMesh() const { return PickupMesh; }
private:
    /** Static mesh to represent the pickup in the level */
    UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Pickup", meta = (AllowPrivateAccess = "true"))
    class UStaticMeshComponent* PickupMesh;

Then I create the PickupMesh in the constructor:

// Sets default values
 APickup::APickup()
 {
     // Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
     PrimaryActorTick.bCanEverTick = false;
     
     // Create the static mesh componet
     PickupMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("PickupMesh"));
     RootComponent = PickupMesh;
 }

Next Steps

I will continue to follow the tutorial with adding variables and functions.

About

Gamer

Tagged with:

Leave a Reply