Class
Introduction Unreal Engine 4 is based around several paradigms, one at the core is Object Orientation. This describes Classes and Objects as basic building blocks, especially in relation to the C++...
Introduction
Unreal Engine 4 is based around several paradigms, one at the core is Object Orientation. This describes Classes and Objects as basic building blocks, especially in relation to the C++ programming language which the Unreal Engine is built ontop of as its foundation.
Inheritance
In Class Based Programming (a style of OOP) inheritance occurs through defining classes of the objects you intend to use.
Example 1
As a basic example YourObject
is declared as a child of Object
.
- Object
- YourObject
YourObject
derives its functionality from its parent. Lets say Object has the function (method) GetClass()
your Object will also have this function since its implemented in its parent.
- Object::GetClass()
- YourObject::GetClass()
Example 2
A more complex example might be two classes extending different Objects.
- Object
- YourObjectA
- ActorComponent::DestroyComponent()
- YourObjectB::DestroyComponent()
In the above only your Object B can call the DestroyComponent
method since that function is implemented in the ActorComponent
the child of Object
.
Where your Object A does not have the DestroyComponent
method, it only has the methods defined in its parent Object
.
It is also important to note that since ActorComponent
is a child of Object
it (along with your Object B) inherits all methods from the parent but none from your Object A because it's not a direct descendant in the Class Hierarchy (or Tree).
It's not just methods which are inherited but all members of that class.
Property System
An object can be a variable, a data structure, a function, or a method, which is why all types in the Property System start with the U prefix.
set to change in UE4.25
Blueprint
Blueprint is used to create classes from within Unreal Editor, these are Asset Types as well as referring to the Graph Editor itself.
Defining Classes
As you can see from the below, all Objects defined UCLASS()
require the GENERATED_BODY()
macro so that property system can properly manage them.
Unreal Engine versions earlier than version 4.11 {{code|GENERATED_UCLASS_BODY()
was used instead.}}
Objects
Prefix for any Object in Unreal Engine 4 is U however your file name should be YourObject.h
UCLASS()
class UYourObject : public UObject
{
GENERATED_BODY()
};
Actors
Prefix for any Actor in Unreal Engine 4 is A however your file name should be YourActor.h
UCLASS()
class AYourActor : public AActor
{
GENERATED_BODY()
};
Constructor
A special type of member function which is called when we instantiate an Object (or Actor) of this Class. This code is typically place at the top of the YourActor.cpp
AYourActor::AYourActor()
{
/* Your Actors Constructor - Not to be confused with Construction Script in Blueprint */
}
Classes with a GENERATED_BODY()
will recieve a default constructor (the above), you may define your own constructor methods in the YourActor.h
;
UCLASS()
class AYourActor : public AActor
{
GENERATED_BODY()
AYourActor(const FObjectInitializer& ObjectInitializer);
};
This allows you to access the ObjectInitializer
in your constructor.
expand on this, needs cpp file end to show usage
Constructor Helpers
Destructor
Abstract Class
This is a special type of Class which cannot be instantiated as an object, methods are called directly on the class itself.
Class Literal
- class
- TSubclassOf
static ConstructorHelpers::FClassFinder PlayerPawnClassFinder(TEXT("/Game/Blueprints/MyCharacter"));
DefaultPawnClass = PlayerPawnClassFinder.Class;
Static Classes
Instantiating Objects
Object in this case refers to a particular instance of a class that is created.
Creation
UStaticMeshComponent* Mesh = NewObject (Owner);
In this example we can see the a new object Mesh
is created from the class UStaticMeshComponent
.
Default Objects
see Component
Destruction
Garbage Collection
Polymorphism
Object Literal
Further Reading
Epic Official Documentation
- Class Creation
- Gameplay Classes
- Objects - Explanations of the basic gameplay elements, Actors and Objects
- Unreal Object Handling - Overview of the features of the UObject system