CrazedFan Games
  1. Posts/

DevLog 0002 - Creating an Indie FPS in Unreal Engine 5: Creating your first Actor Class using C++

·5 mins

Yesterday, I created a blank game from scratch and introduced the basic player movement using blueprints. Blueprints are a great way to get something done quickly, but I don’t want to rely on them for player movement. For that, I want to use C++. So this post begins my journey to learn C++ and how to apply it to Unreal Engine.

My journey with C++ started with Borland C++ in the early 90s. I spent my hard-earned lawnmower money on it, but it has been a very long time. My preference would be to use Go (Golang). However, it has been my language of choice for almost a decade now—anyway, it is time for me to get back on the C++ horse and take advantage of Unreal.

First, a Family Update #

Today, we watched some football, did laundry, went swimming, and my wife and daughter baked a cake for my mother, who has a birthday coming up.

Accomplishments #

I have created and documented how to create a game from scratch in Unreal Engine 5, specifically, control player movement and render the most basic HUD (a square).

Creating your first Actor Class using C++ #

In Unreal Engine, the Object class is the base class for everything, and we can not place an object directly in the game. Instead, most classes we place in-game are of the type Actor. Unreal Engine provides several parent classes to choose from when we create a new C++ actor:

Unreal Engine 5 C++ Class Basics

There are several parent classes available to select when creating a new Class. I’ll focus on the following basics in my education for now: None Actor: An object that can be placed or spawned in the world. Pawn: A Pawn is an actor that can be ‘possessed’ by a controller and played. Character: A type of Pawn that can walk around the world. Actor Component: A reusable component available to add to any actor (e.g., weapon, armor, etc.) My first C++ class will have the following inheritance: Object Base Class Actor Parent Class MyFirstActor Child Class

Creating My First C++ Class #

To create a new C++ Class in Unreal Engine 5, go into the Content Browser and click Engine C++ Classes and press + Add. Next, create a New C++ Class, choose Actor as the parent, and don’t change anything else (e.g., public/private). To create a new class. After a moment, Visual Studio opens and is ready with two files: MyFirstActor.h populated with other headers to enable access to Unreal Engine utilities. MyFirstActor.cpp stubbed with a few methods to establish defaults, hook into spawning for the class, and a hook into a function called for every tick of the engine (e.g., frame rendered) Once Unreal Engine generates the code I clicked built the project in Visual Studio to verify things worked…. and they did not. When I built the solution, I observed a strange error:

"UnrealBuildTool : error : Unhandled exception:
System.ComponentModel.Win32Exception (5): Access is denied."

I could not find any solutions online after more than an hour of searching. I finally uninstalled Visual Studio and Unreal Engine 5 and installed them both again. This time, I made sure to follow the instructions when installing Visual Studio Community 2019 – specifically, I was careful to enable “Unreal Installer” for Visual Studio. After reinstalling (and following instructions), compiling the project worked as expected.

With Visual Studio correctly installed, I followed the Programming Quick Start tutorial published to the Unreal Engine 4.27 documentation site: https://docs.unrealengine.com/4.27/en-US/ProgrammingAndScripting/ProgrammingWithCPP/CPPProgrammingQuickStart/. Although I’m using Unreal Engine 5, this tutorial worked as expected – I won’t duplicate the content here.

Next, I wanted to play with logging some simple debugging statements, so I added the line to the Tick function:

UE_LOG(LogTemp, Warning, TEXT("DeltaTime: %f"), DeltaTime);

After building and compiling the solution in Visual Studio, pulling up the logging window in Unreal, and playing in the editor, I saw the wall of spam I expected in the console output. So next, I modified the constructor to interact with Unreal’s reflection system to print the name of the actor: UE_LOG(LogTemp, Warning, TEXT("Actor: %s was created!"), *GetName()). At this point, I renamed the actor in the World Outliner and was able to see the revised name print on the console.

Exposing Class Variables to Blueprints and the Unreal Editor #

Modify the header (.h) file in the public section, and place the UPROPERTY macro, and include a specifier to establish the scope or visibility of the variable:

UPROPERTY(VisibleAnywhere)
float ActorAge

UPROPERTY(EditAnywhere)
int32 ActorLevel

The VisibleAnywhere specifier will allow the below variable to be visible (read) within the details panel of the Blueprint Editor and the details panel when an instance is selected. Specifying EditAnywhere allows us to control (write) the value from those same panels. Thus, once we create a blueprint, you should see both a read-only and an editable value. Of course, there are many other values we could specify. See Property Specifiers Property Specifiers | Unreal Engine Documentation.

Explanation of Terms and Entities #

What is a Tick in Unreal Engine? #

The engine calls a Tick function for every frame rendered by the game engine. Ticks are not predictable. As your game fluctuates in demands on the host system, the tickets will vary – hopefully between 60 and 120 times per second (e.g., framerate). If we use a tick to update the state, we can experience very different results per machine – and we’ll have to handle that in later iterations. The Tick function accepts a float that expresses the time since the last frame or tick.