Class

From UE4: Community Wiki
Jump to: navigation, search


This article is a preliminary version, it is subject to change.


Introduction[edit]

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++ and C# programming languages which the Unreal Engine is built ontop of as its foundation.

Inheritance[edit]

In Class Based Programming (a style of OOP) inheritance occurs through defining classes of the objects you intend to use.

Example 1[edit]

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[edit]

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[edit]

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[edit]

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[edit]

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 GENERATED_UCLASS_BODY() was used instead.

Objects[edit]

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[edit]

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[edit]

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[edit]

Destructor[edit]

Abstract Class[edit]

This is a special type of Class which cannot be instantiated as an object, methods are called directly on the class itself.

Class Literal[edit]

  • class
  • TSubclassOf
static ConstructorHelpers::FClassFinder<APawn> PlayerPawnClassFinder(TEXT("/Game/Blueprints/MyCharacter"));
DefaultPawnClass = PlayerPawnClassFinder.Class;

Static Classes[edit]

Instantiating Objects[edit]

Object in this case refers to a particular instance of a class that is created.

Creation[edit]

UStaticMeshComponent* Mesh = NewObject <UStaticMeshComponent>(Owner);

In this example we can see the a new object Mesh is created from the class UStaticMeshComponent.

Default Objects[edit]

see Component

Destruction[edit]

Garbage Collection[edit]

Polymorphism[edit]

Object Literal[edit]

Further Reading[edit]

Wikipedia[edit]

Others[edit]