While there are a number of different types of Actors that ship with Unreal as part of the default installation, you will find yourself needing to create custom Actors at some point during your project's development. This might happen when you need to add functionality to an existing class, combine Components in a combination not present in the default subclasses, or add additional member variables to a class. The next two recipes demonstrate how to use either composition or inheritance to customize Actors.
Make sure you have installed Visual Studio and Unreal 4 as per the recipe in Chapter 1, UE4 Development Tools. You'll also need to have either an existing project, or create a new one using the Unreal-provided wizard.
MyFirstActor
, then click on OK to launch Visual Studio.MyFirstActor.h #pragma once #include "GameFramework/Actor.h" #include "MyFirstActor.generated.h" UCLASS() class UE4COOKBOOK_API AMyFirstActor : public AActor { GENERATED_BODY() public: AMyFirstActor(); }; MyFirstActor.cpp #include "UE4Cookbook.h" #include "MyFirstActor.h" AMyFirstActor::AMyFirstActor() { PrimaryActorTick.bCanEverTick = true; }
In time, you'll become familiar enough with the standard code, so you will be able to just create new classes from Visual Studio without using the Unreal wizard.
#pragma once
: This preprocessor statement, or pragma
, is Unreal's expected method of implementing include guards—pieces of code that prevent an include
file from causing errors by being referenced multiple times.#include "GameFramework/Actor.h"
: We're going to create an Actor
subclass, so naturally, we need to include the header
file for the class we are inheriting from.#include "MyFirstActor.generated.h"
: All actor classes need to include their generated.h
file. This file is automatically created by Unreal Header Tool (UHT) based on the macros that it detects in your files.UCLASS()
: UCLASS
is one such macro, which allows us to indicate that a class will be exposed to Unreal's reflection system. Reflection allows us to inspect and iterate object properties during runtime as well as manage references to our objects for garbage collection.class UE4COOKBOOK_API AMyFirstActor : public AActor
: This is the actual declaration of our class. The UE4COOKBOOK_API
macro is created by UHT, and is necessary to help our project compile properly on Windows by ensuring that our project module's classes are exported correctly in the DLL. You will also notice that both MyFirstActor
and Actor
have the prefix A
—this is the naming convention that Unreal requires for native classes that are inherited from Actor
.GENERATED_BODY()
: GENERATED_BODY
is another UHT macro that has been expanded to include the automatically generated functions that the underlying UE type system requires.PrimaryActorTick.bCanEverTick = true;
: Inside the constructor implementation, this line enables ticking for this Actor
. All Actors have a function called Tick
, and this Boolean variable means that the Actor
will have that function called once per frame enabling the actor to perform actions in every frame as necessary. As a performance optimization, this is disabled by default.18.188.142.146