In time, you'll become familiar with the standard code, so you will be able to just create new classes from Visual Studio without using the Unreal wizard.
In MyFirstActor.h, we have the following aspects:
- #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 "CoreMinimal.h": This file includes a number of definitions of classes that are often used, such as FString, TArray, Vector, and so on, and is included by default in created script files for that reason, though it could still compile at this point without it.
- #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 so that we know about its contents.
- #include "MyFirstActor.generated.h": All Actor classes need to include their generated.h file. This file is automatically created by the Unreal Header Tool (UHT) based on the macros that it detects in your files.
- UCLASS(): UCLASS is one such macro that 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 CHAPTER_04_API AMyFirstActor : public AActor: This is the actual declaration of our class. The CHAPTER_04_API macro is created by the 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.
Note that, in this case, Chapter_04 is the name of the project, and your project may have a different name.
- 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.
Inside the MyFirstActor.cpp file, we have the following aspects to note:
- 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.
- BeginPlay/Tick: You can also see the implementation of two default methods, BeginPlay and Tick, which are called once an object is spawned and every frame it is alive, respectively. Currently, these only call the parent's version of the function via Super::FunctionName.