Hello, I'm Joohwan Kim
This is the fourth class of Unreal C++ basics
C++ Unreal Structure
During this time, inheritance, polymorphism, and template
are the elements of C++ that we'll learn
And we'll learn about the C++ inheritance structure within the Unreal Engine
And in order to implement reflection
We'll learn about UPROPERTY and UFUNCTION
Finally, we'll learn about the life cycle of actors
Then after the lecture
you will be ready for C++ programming using Unreal
Understanding Inheritance, Polymorphism, and Template
First, the other elements of C++
It's actually impossible to explain all the elements of C++
But in order to do Unreal C++ Programming
a must-have part is
inheritance
We learned about class in the previous lecture
Inheritance is collecting a certain part
and sending it to the parent object
So it inherits the parent
It's like us inheriting in real life
A certain property, or content
Usually data and function, are inherited
When parent class is inherited
The daughter becomes the parent's member variable, or member function
It becomes the member of the parent
We can call it parent's member variable or member function, either works
We make this inheritance because
since we're making multiple classes
the similar parts that overlap
need to be reused
That's why this grammar was made
For example
Birds, cats, whales
These objects were constructed with class
But after construction
They had the similarity of being a living thing
And the similarity of moving
And the similarity of having a name
All these parts are overlapping
So we make a parent class called living thing
And by inheriting that
we make birds, cats, dogs, and whales
The inherited form is called Class Hierarchy
In English, it's called hierarchy
Hierarchy is similar to a family tree
That's also called Hierarchy
Just like the family tree
The parent class, the middle class
and the bottom class are depicted
So creating this hierarchy
in object-oriented programming
it corresponds to the construction part
It's a very important part
There's a lot of philosophy to it
As I mentioned earlier
Are we going to link it by similar parts
or are we going to link them by similar forms
or link them by similar functions
There are various approaches
Based on that, we would link them differently
The things I mentioned earlier
We may link them as living things
or moveable
having a name
able to move
able to attack, able to be hit
Like those, they may also be classified by functions
So in order to implement those
What we need to know it
Function Overriding
We need to redefine the function
What this means is that
The daughter classes that inherited the parent class
Still has the member variable of the parent
It's not private, but more than protected
If the access modifier is made
The data within the parent class can be used
As for functions
we can also use the parent class
But rather than the parent operation
If we want to operate differently
Then we do overriding
That's why it's function overriding
When we do function overriding
Operating in the parent
and operating in the daughter can be made differently
That's called polymorphism
It's also called dynamic binding
Usually, we call it subtype polymorphism
There are many types of polymorphisms
Out of them, the sub type
So between the parent and the daughter
Since the sub type is made from the parent
the polymorphism that has the form of parent-daughter relationship
is called sub-type polymorphism
And through polymorphisms
By combining multiple objects into one parent
The parent function
Usually the virtual function, is overridden
Using the parent function to control
the daughter class functions
is called abstraction
To tell you about polymorphism further
If we take a look at the link about polymorphism
The page you're seeing right now
is called GitHub
You'll use it a lot as a programmer
It uses the version control system called Git
The sources that Microsoft possesses
can be saved in this page
If you take a look, there's a particular page
and the 4 types of C++ polymorphism
It says The Four Polymorphisms in C++
It's an explanation of the four types of polymorphisms of C++
Polymorphism exists in more than 4 types
These days, we even say 7 types or 12 types
To put it in simple terms
When we do a certain operation
It's the same operation
But it acts differently based on the object
It's the same command
but it acts differently, and that's what polymorphism is
So just like the first line, there's subtype polymorphism
And second, parameter polymorphism
and temporary polymorphism and forced polymorphism
I'll only explain about the subtype polymorphism
It's also called runtime polymorphism
As I told you
Linking cats, tigers, and cheetahs
and calling them "moving", is what the example shows
To tell you about abstraction
Through subtype polymorphism
a certain object is literally becoming abstracted
Dogs, cats, and whales can all move but
Dogs will run
Birds will fly
And whales will swim
But by placing a parent object
and calling it "move", is abstracting
It abstracts by using polymorphism
It's similar to hiding data using capsulation
In summary, it's a method of abstracting class and method
It's the method of abstraction, and it uses polymorphism
To implement polymorphism, to implement abstraction
The most important part of C++ grammar
There's the virtual keyword
When the keyword virtual is placed in front of a function
It becomes a virtual function
Called virtual function in English
As I mentioned earlier
Implementing subtype polymorphism
It's the most normal and the only way
In the grammar, create the virtual function in the parent
and in the daughter, take the virtual function
and override it
Then when it's called from the parent
Even though it's the same call
It will show different operations based on the daughter type
For overriding and virtual function, the signature has to match
Signature, as I mentioned before
For the signature of a function
The return type and the parameter
and how the function looks, are called signature
Having the two integer parameters
A function that has the integer return form
Then we say that the signature is the same
So when the signature of the parent and the daughter is the same
The method of calling the function will also be the same
Then through function call, we can implement polymorphism
For polymorphism and virtual function, destructor is possible
For destructor, when it disappears
in order to destruct polymorphically
it can use them
It's impossible for constructor
And that's because
in the hierarchy, the parent and the daughter has to be formed
But when the parent is constructed
we don't know the daughter
So it's not the target of virtual function
Once we know virtual function, the way how virtual functions work
or the way it compiles as one, also needs to be learned
First, there's something called static binding
What static binding is
is that it's related to how the function is called over
At the side where the function is called over to
In order to call the function
In order to call the function text
they need to know the address of the function text
So what decides the address
When you compile, you can know the function call address
Of course, you'll need to address when it operates
But when it compiles
The address that calls the function
gets inserted into the calling part
This is called static binding
also called early binding
In other words, when calling the function
For normal functions, at the time of call
we say that we know where the function is at
So for normal member functions, static binding
is done
For normally made functions
Whether they're member functions or not
Since we're talking about class right now, they're member functions
But for normal functions, they're called static
But as before, in order to implement subtype polymorphism
Function made as virtual function
can be decided at the time of operation
Because the pointer of the parent
can point to the daughter
But when the parent is pointing to the daughter
The actual type may be bird, cat, or dog
But when it receives living thing
Until the time of calling it as living thing class
it's usually not decided until then
So we say that we decide the call function during runtime
and that's called dynamic binding, or late binding
In summary, virtual function is when
you know the address of the function when you call
And you know it by
for any objects or any classes
when you use virtual function
Where there's at least one virtual keyword
and it's implemented
The 4 byte or 8 byte at the very front of the class
The pointer forms, that points to the virtual function table
So it includes the information of what function to call and how to call
The pointer that points to the table, is formed
And through the pointer
Through the pointer of basic class
Where the function is at
Where the function pointer is at, is found
This is subtype polymorphism
It's the method of how polymorphism is formed
We need to actually look at the code in order to understand
That's how difficult it is at first
So for now
When calling with virtual function, we can know the address of the function
Static binding, is as far as you should know
So when making this virtual function
Among virtual function, there's something called pure virtual function
It's for making abstract class
To put this in simple terms
It's a virtual function with no content, no implementation
The dog runs, the bird flies
the whale swims
We say that the living things are moving
But the living thing may not even be there
The target of living thing
may not even move, is what it means
So for abstract class that contains pure virtual function
Instance is impossible
It means that we can't create that class
The concept of a living thing moving
can't be actually made
By creating a pointer called living thing
we point to the dog, point to the bird
and pointing to the whale, is possible
It's the exact same meaning as the pointer
So for pure virtual function
Or abstract virtual function, doesn't create the function implementation
It only creates the outer form of the function
and redefine them in the daughter
When we use this pure virtual function
we can't actually made the object
so we can't only make abstract classes
We can only make abstract class
And this means forcing abstract
Related to virtual function, I can explain about the virtual destructor
As I mentioned earlier, destructor is possible in virtual function
but not constructor
Why does destructor have virtual function?
Within the hierarchy
When there's the parent and daughter relationship
In order to completely destroy the daughter using the parent class
even the destroyer is implemented virtually
This is the virtual function table I mentioned earlier
It's called virtual table
There were controversies about the word virtual
The address of the function created virtually, or dynamically
the function pointer is included in this table
The compiler makes this
The implementation may be different based on how the compiler makes it
But grammatically, when virtual function is called over
The daughter is overridden
in the virtual function
If it follows the C++ standard
So the class that uses virtual function
has the virtual function table
And the pointer that points to the table
is saved in the object
I explained about abstract and polymorphism
For configuration of the C++ language
For the two main elements
Unlike C, the two main elements are
Abstract and polymorphism are one part
And then there's template called generic
It's called generic programming
It means that we can do something generally
In C++, this grammar is called template
This template grammar literally means template
It sets the form
In generic programming
it's used as a tool
Let's take a quick look at the MSDN page
As I explained in other lectures
for looking at a certain concept, the best way is
using Microsoft's
Visual Studio C++
That's why is good to use Microsoft's MSDN page
At the very top, it says 'Learn C++, C and Assembler'
And it says template C++
Normally, template grammar
only exists in C++
Template limited to C++
Template is the basis of generic programming in C++
There's the basic explanation
There's the definition and usage of template
Under here, it says C++ and there's a code
template typename T, T minimum, const T reference
Like this, template grammar
is pretty complicated for a beginner
But in the beginning
there's the template bent bracket, called angle bracket
Starting from the angle bracket
typename T, then close angle bracket
This grammar is essential for using template
It's a must-have grammar
And the next part that starts with T
The typename of template
It means that it operates on a certain T type
So the details and the form parameter
parameters without the form, specialization
Template is the grammar that takes up the other 50% of C++
So the grammar is very complicated and varies a lot
So for modern C++
After C++ emerging in 1979
it continues to improve from 2011
So it's called C++11
After 2011
a new version is released every 3 years
And the new versions are mostly
used for strengthening generic programming
So template has various grammars and is difficult
but if you want to use C++ well
you must know this grammar
So I hope you take a look at the MSDN page
Using template
is called compile type polymorphism
In English, Compile Type Polymorphism
Usually, for polymorphism
as I explained earlier, abstract that uses virtual function
It's usually called runtime polymorphism that uses virtual function
The polymorphism appears at the time of operation
But unlike that, using a template
Compile type, or the source code
If they're made into a binary code
and made possible, that's called compile
At the time of translating
the polymorphism is operated
So at the time of compiling
we said that it's a tool for making a class type
But even for the type
Even when we don't know what type will come in
We can still write the code
To put it in simple terms
I explained about the add function that adds two integers
When we make the add function
We don't know whether integer or float will come in
But we can still make it
That's the concept
of compile type polymorphism template
So on top of runtime polymorphism
Runtime polymorphism is the same thing as subtype polymorphism
The runtime polymorphism
On top of the polymorphism that uses hierarchy
It's also an important element of C++
C++ Unreal Engine Structure
So the overall form
of C++ were touched upon
Based on that
let's take a look at the hierarchy structure of Unreal C++
Visual studio 2022 community
We're going to use this version
And let's skim through the Unreal source code
We didn't learn it yet
but actor class, with A as the prefix
actor class and character class
For the hierarchy of the actor class series
as we move up from the character class
We'll take a look at the inheritance structure
If you look at the source code
There's a lot to explain
But I'll just touch upon the main parts
First, where 'sharp' is included
Syntaxes such as sharp pragma once or sharp include
are called pre-processors
Before compiling, the pre-processor
the pre-processor is commanded to change the document this way
The pragma once is declaring that this code doesn't repeat twice
And it says sharp include core minimal.h
What #include is, is that
within the source code
other source codes are included
So the core minimal. h header file
is part of the source code
That source code, in this third line
will be copied as is
Then there's include GameFramework/Character.h
Same thing for this, the source code content of character.h
will be pasted as-is here
Then there's UCLASS and parenthesis
This UCLASS doesn't exist in the C++ grammar
The implementation is done by pre-processor
but this grammar doesn't exist in C++ grammar
But it's made with pre-processor
It's a characteristic of C++ used in unreal
When this UCLASS is added, it means that we'll use it in Unreal
That's what it means
Then there's the lowercase class
LECTURE01_API is calling convention
You don't need to know about this for now
This is about how we're going to call this class
It's a code, but we'll pass for now
And the AMyCharacterL after that
This is the class type of the character
that I made in Unreal
And next to it, there's a colon
and says public Acharacter
And this is the grammar of inheritance
The name of the class
The name of the class that I made
is AMyCharacterL
And this AMyCharacterL
is the type I explained, the type of the class
And this class, public acharacter
has another class called acharacter as the parent
That's what this inheritance grammar means
As I mentioned earlier, it corresponds to the inheritance
It has the parent called acharacter
With the acharacter parent
we made the AMyCharacterL
For the generate body inside the braces
it's the code made in Unreal
I'll also explain about this later when I talk about reflection
There are codes that Unreal creates and inserts
For the existing C++
reflection function is not possible
It's not provided in the language
But to implement that, Unreal has made it
I'll explain about it later
And there's public modifier
There's the comment
and the constructor called AMyCharacterL
And after that, public void BeginPlay
When this actor begins
this is what's called
tick is called at every frame
It says it in English at the top
Called every frame
or called when the game starts or when spawned
The time that these functions will be called
are described in Unreal
For begin play, tick, setup player, and input component
they're all declared as virtual
virtual void, and void is the return type
Virtual, as I mentioned earlier
is the keyword that shows that it's a virtual function
At the back, there's the keyword override
Looking at this
this function was implemented in the parent
and was redefined in the daughter
So the very bottom part
the most daughter of the hierarchy
was this AMyCharacterL class that I created
This AMyCharacterL class inherited from
character a, the parent right above it
Right-click on it in visual studio
and go to definition
Then we can go to the definition of the object
It's the function of the visual studio itself
That's how I moved to the parent right above it
To go back, the class I made earlier
is the character that I made
And in the character I made
there will be functions exclusive to my character
And the a character above it
is a class made by Unreal Engine
Here, normally
it contains how the character is supposed to act
We can't take a look at the entire source, but
the thousands of functions
of Unreal, called api
We can't take a look at the whole Application Programming Interface
but generally, when I make a certain class
at the top
to see what code that Unreal makes
I'm looking through the sources
It says class ACharacter : public APawn
It means that it made the class, or character, by inheriting Pawn
Same thing here, in GenerateBody
codes for reflection made by Unreal were inserted
Underneath, it says DefaultUObjectConstruct
In order to Unreal to use them
the basic codes are listed here
Similarly, if we move up one step again
There class APawn
Pawn is just like the Pawn of chess
It's lower than the actor
but higher than the character
It's moveable
It doesn't have characteristics
but an object that can be moved
It will be helpful to read the top explanation
Pawn is the base class of all actors
that can be possessed by players or AI
Players or AI
The players or AI can
possess, but it basically means moveable
The objects that can be possessed and moveable
are made here, is what it's trying to say
So when you take a look at APawn class
there are actions that only Pawns can do
Under that, the purple part
the parts written in purple
are what pre-processors redefined
The purple parts
in C++ or C language itself
is not included
The pre-processor was used
to create the reflection function
that doesn't exist
Later on, when I explain reflection
GenerateBody, UProperty
and UFunction will all be explained
Anyways, that's how APawn hierarchy is made up
For Pawn, GetLifetimeReplicateProperty or PreReplication can be used
Replication literally means that the object can be replicated
It means moving the same way
That's not what I'll explain right now
This Pawn does a certain job
It has the function of being possessed and moved
is what we can know from this
When I moved back to the character, the character
wasn't that different from Pawn
When we move up to Pawn and move up one more step
We come across the Actor
Before I explain the Actor
When you take a look at Class APawn
There's colon and Public AActor
then comma, then Public INavAgentInterface
This is called multiple inheritance
There's more than one parent
It can inherit from multiple
But I won't mention this multiple inheritance for now
So it inherited two things
Interface Navigation Agent Interface
I stands for Interface
Rather than going to INavigationAgentInterface
we'll move up to Actor class
If we go to AActor class again
This becomes the parent of Pawn
It's the parent of Pawn
Same thing, there's the actor for the parent of Pawn
And how this Actor acts
The top part is similar
But there's something called InitializeDefaults
If you look at the explanation
Called from the constructor to initialize the class
to its default setting
Using the default setting
this object
will be initialized
So as you've seen so far
We can move up even more, but up until this hierarchy
Starting from AMyCharacterL
to ACharacter
APawn
AActor
Until then, there are many parent hierarchy
This is called inheritance hierarchy
We divide it into so many hierarchy because
for each hierarchy, for each parent
there are various and complex works that need to be done
That's why it's configured like this
The explanation was long
But we have one or two more steps left
Let's go to UObject, the parent of Actor
UObject is actually the parent
of almost all objects that exist in Unreal
There's one more step, but first for UObject
For UObject
it's also explained on top in English
The Base Class of all UE objects
For all the objects that appear in Unreal Engine
this is the base class
Hence the name, it's the basic class
Of course, there's another parent at the top
But this is the hierarchy for implementing functional aspects
This class, UObject, is actually
very important for Unreal C++ Programming
It's the most basic
It can act most normally
It's the class that you must know
There are lots of grammars
and I can't explain every one of them
as Unreal is the world's most
AAA class engine
The content is very complicated
and the explanation and the implementation are also complicated
But through the Pawn and Actor
moving up the hierarchy
moving up to the UObject
is what's important
Same thing, at the top
There's a slash, not two slashes, but slash asterisk
Starting from slash asterisk
the first four lines are the comment
The comment doesn't get compiled
It's an explanation, so take a look
Same thing for document.unrealengine.com
The explanation in Unreal engine
take a look at that as well
Then the things I've said so far
will be understood in detail
So if you take a look at UObject for a second
There's Virtual Constructor
probably virtual function constructor
will also be callable
Config Name, Static Config Name
We'll be able to name static names as well
The normal things that we can do
The thing that all Unreal objects can do
are what's written here
While we're on it
I said that Unreal Object is the parent
of practically all the objects that appear in Unreal
But let's go to the very top
So Unreal Object is Unreal Object Base Utility
The basic actions
The basic useful utilities are included
And when you take a look, there seems to be functions
As for MarkPendingKill
There's something called Garbage Collecting
where objects need to be removed someday
This is the hierarchy that holds those functions
You can see Clear Garbage or Mark As Garbage Clear Pending
But for this UObjectBaseUtility hierarchy
The object is created and deleted
in this class
Let's move up to UObjectBase
This is the uppermost, without a parent
hierarchy that we're at
UObject was called BaseOfU earlier because
when it operates
conceptually, the parent is UObject
But UObject can't be made at once as well
So for UObject, in Unreal
It needs a lot of functions as the parent of all objects
That's why there are two more hierarchy above it
Then at the very top
That's why the name is UObjectBase
When making Unreal object
this is the basic class
In the comment, it says Low Level Implementation of UObject
Literally translates to its meaning
So it means making the most basic, at the low level
Should not be used directly in game code
So we can't use this right away in game code
There's a keyword called Friend, and to explain this briefly
This means that rather than going to other class or qualifier
we can use it right away
Normally, to keep all Private Projected and Public
they have to pass through all capsulation
But without those, they can access each other
So they set that as Friend class or Friend Struct
Here as well, there's the basic form of constructor
And the name, the package name
It says super class, static class
So the name and where it belongs to
Only those basic things
are included here
So to recall
Using the source code
in the example of lecture 1
The character class, MyCharacter class that we made
We went through each hierarchy using the source code
Starting from Begin, Play, Tick, the overridden parts
then up to the middle hierarchy, Garbage Collecting, and handling objects
Then up to the Actor
And the Unreal Object, the parent of all, and UObject, the base of all
In order to implement the UObject
we also took a look at the two class hierarchies
Even the programmers who can handle C++ well
have a hard time looking at the Unreal class hierarchy
So when you have just learned C++
it may be difficult to read these codes
But when you do Unreal programming
you'll see them often, so I skimmed through them
The UProperty and UFunction I mentioned earlier
I'll explain about the reflection system of Unreal
Actually, this isn't the details of C++
It's not the C++ Specification
And it's not implemented in C++
It's not implemented in C++ language
so Unreal made this system
To explain this briefly
When there's a syntax that says int a = 3
In variable a
3 can be included or used
But we don't know that the name of the variable is a
When the program is executed
we can't know the name of the variable and the class
Even in the current C++2023
That's the newest version
Even in the C++ released in 2023
The name of the variable or the name of the class
cannot be brought as text
So using the existing grammar, we need to implement it somehow
To see what class I made
And what variables and functions are there
From outside the program
Being able to see those outside the source code
is called the Reflection system
In order to implement the Unreal's Reflection system
If you look at Unreal engine
When you click on Actor
You can see the inner properties of Actor
Those are possible because
And we can call a function of an Actor
We can call it from the server
And other clients can call as well because
This Reflection function that works with UProperty and UFunction
is coming into play
I skipped in the middle
but to summarize it
Many implementations are skipped, actually
UProperty is placed in front of the member variable
If we put UProperty Define before the variable
The pre-processor
Then that means that this variable will be used in the Reflection system
Same thing for UFunction, it's used in member function
Existing in C++ as well
UClass, UEnumerate
I didn't explain Enumerate, but
it's a countable form
It's put as a special reason
There's a keyword called UStruct
As I explained, since it doesn't have the Reflection function
When we make one C++ file in Unreal
when we make the class
For example, after ACharacterL
Generated.h file is formed automatically
And this becomes included in Generated Body
Unreal's Reflection system is
not something we can talk about within 5 or 10 minutes
It's made very complex
But when we actually use it
You just need to see that UProperty and UFunction are together
So within the source code that I showed you earlier
Go to AMyCharacterL
and look at the GenerateBody
or the UProperty above it
Finally, I'll show you the life cycle of an Actor
The purpose of my lecture is
being able to do Unreal Programming using C++
That's the step I wanted to reach
So opening Unreal
and changing and moving the actor
won't be done
But for Actor, Pawn, or Character
when you do Unreal programming
They're the very first object that you encounter
So we'll take a look at the Actor's life cycle
Click the Unreal Documentation Actors Life Cycle link
And this screen appears
There's a simple Breakdown
This shows what happens when the Unreal Actor is formed
You'll see this later
For Actor, there are 3 main ways
of how the life cycle works
When it's executed in Editor
or when it's loaded from data
and third is when it's created dynamically
The yellow on the left
says Play in Editor
What happens when the player button is pressed in Editor
It means copying it from Editor and running it in engine
For the second, there's a level, and the map is loaded on the level
or when it's added in the world
The actor is formed by bringing it from the disk
On the right, it starts from nothing
When an action actually happens
For example pressing a key or colliding
The Spawn, or Actor is dynamically created
The Spawn Actor is created right away
And Spawn Actor Deferred is the delayed spawn
So it's created dynamically and about what happens next
It's a step process
If you look at the bottom
It's created
It begins play through Begin Play
In the middle, where it says The application is running and the actor is ticking
This is where the game is actually running
The green part in the middle
The game ultimately goes around a tick
It's an infinite loop
So it goes around the loop infinitely and runs the game
Based on the 6 conditions on the bottom
When it ends, go to End Play
and it reaches the step of deleting the object
So for Actor mark, as I explained earlier
does Garbage Collecting and Pending Kill
and once everything is over
After the post processing
BeginDestroy, IsReadyforFinishDestroy, then FinishDestroy
It ends by destroying the object
You're going to go through these kinds of programming
And I hope the C++ programming basics today was helpful
And same thing for MSDN, Microsoft Development Network, but
when you study Unreal
The Epic Games that made Unreal
Their Documentation page will be the most helpful
I'll end the lecture here, thank you
Understanding inheritance, polymorphism, and template Inheritance Inherit the parent object for the common parts, can use the data and functions of the parent
Need inheritance to reuse the repeated parts Redefining function is needed to implement the inheritance hierarchy
Polymorphism Executing different actions with the same commands, based on different object Subtype polymorphism: applied on daughter type that comes from the parent type
Compile-Time Polymorphism: uses template
Template Name of the C++ normalization programming Essential grammar template for using template 〈typename T〉
C++ Unreal Engine Structure Unreal C++ Inheritance Hierarchy
AMyCharacterL 〈 ACharacter 〈 APawn 〈 AActor 〈 UObject 〈 UObjectBaseUtility 〈 UObjectBase
Unreal's Reflection System Made by Unreal because there's no reflection function in C++ Can view the variables and functions within the class from outside the source code
Can call over the properties or functions of an Actor UPROPERTY in front of member variable UFUNCTION in front of member function
The End