Hello, this is Younghoon Lee
This time, in order to produce a shooting game that's based on C++
We're going to learn how to produce the project using C++
and learn the basic content production methods
In this unit, we're going to create a project based off of C++
and compose the basic environments
Then in C++, we're going to process user input
and control the player
Basic Project Settings
This time, we're going to produce a shooting game based on C++
We're first going to create the project
Select the game tab
We're going to select the blank template
And now select C++
And then, use the starter content as-is
And then type the project name
Like last time, I'm going to use the Cpp_ prefix
I'll put shooting
Create it like this
Once the project is made
We first need to do some environment settings
First, set the layout to how you're used to
I'm going to do it like this
It becomes the same structure as when we were on blueprint
Then, the very first thing you need to do is making a level
Go to file, new level
Same thing, we'll use empty level
Create, then save
Create a folder
New Folder, and we'll name it Maps
Here, I'll save the level in the name of GameMap
Even though we're working with C++
we still use blueprint
So we're going to create a blueprint folder as well
Create the folder
Like this
First, we need to make the game mode base
And we also need to make the player, Pawn
After making both in C++ class
we're going to inherit them with blueprint
That will be our work process
Should we first make the game mode?
In order to make the game mode class with C++
If you look at the top, there's Tools
If you open this, there's the new C++ class menu
Click this, and you'll see this pop-up window
Scroll down a little
and you'll see game mode base
Select this, and click next
And if you look here, you can work as-is
but in class type, there's public and private
I'm going to click public
What this does, is that
In the case of Header file
it will fall under the public folder
Then for the source file
it will fall under the private folder
Do it like this
Since the name is shooting
I'm going to name it shooting game mode base
Create
Starting from Unreal 5
They added a live coding function
I'll use this function while working
I can turn live coding on or off
If you look here, there's this menu on the bottom right
If you click these dots
You can activate or deactivate it
That's all
Once you click this
You can see that it compiles right away
For Unreal versions before 5
We would compile in visual studio
and watch that after executing Unreal
But that had many cons
So the solution they provided was live coding
Of course, there are some imperfect parts
But after I used it
it seems to have more stability
So I'm going to use this to work
Once a class is made
Like this, under the C++ class folder
This project's name is there
And under the public folder
There's the header file exposed
We can't see the source file from here
We can only see the header
Now we double click this
Then visual studio is already opened
To take a look at the folder structure first
Let's look at the project
In Content, select Show in explorer
Then we can see the folders
This is the project job folders
If you take a look, there are the essential folders
Config, Content, and Source
These are where the original files are saved
They must never be deleted
Then on the very bottom, uproject
The project file, with the extension name uproject
This file must also never be deleted
So these 3 folders and one file
These should never be deleted
Even if others are deleted
Once we execute Unreal, it will automatically be made again
And when we compile, they're also made
Binary is the case for that
When you back-up this project later
You need to back-up these four
Let's proceed
When we create the code with C++
it will be created within the source file folder
It's created here, in the public and private folders
The content there
In visual studio, looks like this
Of course, just because there's the file in the folder
doesn't mean it will all be exposed here
For visual studio, we have to input it in order to show here
So even if there are multiple files here
Over here, they only show the inputted files
We first made the game mode
Should we make the player too?
Select New C++ class
Our player is going to be Pawn
Select Pawn
It's says it in English here
Pawn is an actor
that can be possessed
Click next
I'll name it PlayerPawn
Create it
Among the extension programs, I'm using visual assist
so my colors may look different
Keep that in mind while watching this
After making this
We should set this up first
If you look at the C++ folder, you'll see two classes
We'll make this in blueprint
First, select game mode base and right-click
And there's Create Blueprint Class Base
Click this
Then the blueprint class that inherits this class can be made
Select the blueprint folder
For prefix, I'll put BP_
Create the game mode like this
We'll make the player the same way
Like this, blueprint
Then we'll save all of these
Then the structure will be like this
The class made in C++
is inherited by the blueprint class
The parent class is the player, Pawn
And for the game mode as well
It inherited the C++ class
Go to Edit, Project Settings, then Maps & Modes
We'll set it up here
Since we made the map
We'll set the game map to start up map and default map
And for default mode, we'll leave it as-is
On the world setting of this level
I'll designate this
Over here, go to Settings in the top right corner
They also have World Settings here
We'll make those settings here
And the player is in default right now
Let's set this to BP_PlayerPawn that we made
When this is edited
It says that the blueprint class here was changed
Let's save all of this
Then now the player has been set
Here, we need the camera and lights
We'll do it the same way
Here, there's Light
Add Directional Light
This is the correct direction
Rotate it so that the X points to the inside of the screen
And let's arrange the camera
We'll place the camera actor in the screen
And we'll set the camera the same way
Reset rotation
and push it back 10 meters
Then change Perspective to Orthographic
to do orthogonal projection
And we'll set the width to 1920
Then the player should use this camera
So go to Auto Player Activation, and set it to Player 0
And the player will appear on the screen
Let's take a look
For the player Pawn
Let's first put a cube
When we execute it, it appears on the screen
But it's at the wrong spot
That's because we didn't set the player's exact location
So it was placed randomly
So in BASIC, there's Player Start
We'll use this to place it
For player start
First put it in the center
then we'll lower it a little
Execute it, and now it looks good
Arrange it like that
Then since we're working with C++
although some are done with blueprint
The basic core logics should all be done using C++
We're going to work on it ourselves in the source code
So we're going to do the coding with C++
Here, I put the cube as the appearance
but we're going to delete this
For the component that will be inserted here
let's create it in the code
Then we first need to go to the header file
Compile first
Then on the top right hand corner
Click the Player Pawn, the parent class
and it will go over to this window
You can open it
Or over here, it's like this
You can do that here
Go to the header file
Open public, and there's the header file
We don't need to expand this
Scroll down
Put public
What we're going to do here, is
Collider, a collider component
We want to create the component and place it as Root
I said this before
I don't know if you'll remember
For Unreal, if there's an Actor
There's the actor component
The actor basically has a root component
It has a root component
And under that, other components come in
It can branch out like this
Or it may branch out from the root
It has a hierarchy like this
Usually, the component with the collider
gets inserted into the Root
So what I'm going to do is
The player is going to have this structure
For the player, I'm going to put the box component in the Root
Then here, I'm going to put Mesh
And here, I can put something like Arrow
That's the hierarchy I'm going to make
We'll do the arrow later, and we'll first work on this
That's what I wrote, and then
Create the component in charge of the outer appearance
and place it in Root
Then Root, ultimately, becomes the collider
So now
we're going to declare in the header first
In Unreal, we use prefixes
Prefixes are very important
If you look here, it says Pawn
When you ctrl+click on it
You can see its parent
It's inheriting the actor like this, right?
So the guys that inherit an actor all use the prefix, A
That's why prefixes are important
Above the actor, it's inheriting from UObject
It says U, right?
Remember those prefixes
Normally, Actors, Pawn, Characters start with A
And the components start with U
And all the others usually start with F
There's also S but
those are very little
Most of them use A, U, or F
often as their prefixes
So we're going to make a component
All components start with U
So write U first
Type UBox, and there's auto fill
Create something called UBox component
We're going to make this as a pointer
So I'll write boxComp
And for C++, we need to declare header
To use this
The header of boxComp
needs to be included here to use it
But if we keep including in header file
there may be problems later when we're compiling
It will become very slow
So in Unreal
Or for all C++ based contents
They all use "include what you use"
When you include header
include it at the implementing place
That's the guideline
So we're going to use that method as well
Then we used that here
But we didn't actually implement it because it's a pointer
We're going to implement it now in source file
So we need to do a forward declaration
If we do it the proper way
we should do the forward declaration at the top, on top of the class
Then we can use this
But forward declaration is this
"We're going to use this"
is what it's saying beforehand
But if we do this every time, we need to scroll up and down
So this is the formal way
But the shortcut is
Writing the class here right away
Then we're doing forward declaration in the abbreviated way
That's the forward declaration
And this Ucomponent
is a component provided by Unreal engine
So when we use this
We're going to use this in engine
In Unreal engine, we're going to use that
But even if we write it in C++ code
the engine can't read it
So when we produce the Unreal engine
there's the Unreal header tool and the Unreal build tool
Those are the two tools
Those tools were used to implement reflection
What reflection is, is that
The content worked on here will also be seen in the engine
It's just like the name, reflecting
That's the concept
For recent languages such as
Python, C#, Java all these provide the reflection function
So using that
we can see the content of the corresponding class
But C++ was released a while ago
That's why it didn't have those functions
But while producing the Unreal engine
In order to make those, they implemented it themselves
There's a rule
In the header, in order to do reflection
the header tool reads the header file
and uses that to create a class
That's the structure
So at the very bottom
There's the Generated header file
It says under the "include"
This must be there
This class, the part without the A
The same name as B
The actual file name is this too, right
That's the structure
Then for including the header file
It has to be at the very bottom of all the includes
For example, it can't be switched like this
There will be errors
The rule is that it must be like this
In order to do reflection
if all of them are the target of reflection
then it will be inconvenient
The file size will be big and there will be unnecessary information
In order to make that precise
Here, in the UProperty
literally means the properties of Unreal
We use macro like these
to designate that this variable is
the target of reflection
And here, there's a designator goes in
For the main designators, in Unreal
Here, I'll make the cube that I made earlier
When I click this, there are the details, right?
In order to see these, we need to set the designator
That this component will be seen like that
To see in details
There's the Edit or Visible concept
Use one of these prefixes
Then plus
Plus means profit
Then Edit
anywhere
We'll have to write it here
Here, there's EditAnywhere
And there's Anywhere
Then there's EditDefaultsOnly
Like that, and then
There's InstanceOnly
We can do this with visible as well
There are three in visible
It's put together like this
EditAnywhere, EditDefaultsOnly, EditInstanceOnly
Like this, or visible
Edit means editable
Visible is read only
So are we going to read only
or will we be able to insert something, that's the concept
What that means is
When we select this
We can insert a value here
Like this
This is Edit
That's the meaning
And for visible, we can only see this and not edit
These here for example
Of course, we can check this off to edit, then back to visible
That's the concept
It can control these
Then for DefaultsOnly and InstanceOnly, it means where
This is Anywhere, so it means both
And this is only in Default, this is only in Instance
So in class
If you look here, this is our Edit window
Then we can do it in the Editor window
And when we take this out
Now it's in the state of being at the outliner of the level, Instance
At the instance state, if we still want to expose it here and edit it
Then that's InstanceOnly
Anywhere means both are possible
So whether we'll edit only in blueprint class
or whether we'll only edit in instance
or do it in both, that's what it means
So at first this may be hard
So what I recommend is
Rather than memorizing all of these
First, EditAnywhere means we can literally edit anywhere
So work with this first
And you'll know later
that something shouldn't be there
That you shouldn't edit something
You'll have those
For example, that you should only do it in EditDefaultsOnly
So once you start seeing those
Then you'll have to write this exactly
In this lecture for now, I'll use EditAnywhere
Player Input Process
Next, we're going to write the component in charge of the outer appearance
Back to here, the cube
This is a Static Mesh component
There are two main components for outer appearance in Unreal
There's Static Mesh and there's Skeletal Mesh
Static Mesh means literally static
It's a fixed mesh shape
For Skeletal Mesh, it has the shape of a skeleton
It has a skeleton
and it can be animated
So right now, we can go with Static Mesh
Let's create a Static Mesh component
Delete this
So same thing, let's copy UProperty
Then for the outer appearance
put class UStaticMeshComponent
We'll put MeshComponent
Should we do it in caps
Let's put it in capital letters
Then we'll have to make this
Making is done here, in the constructor
The constructor in Unreal is a little special
The role of the constructor is
Normally, when we make an object, the constructor of that object is called over
Then there, we reset and do all those
For constructor in Unreal, the basics are like that
but this is different
First, the engine is activated
Or the app is executed
At the developing step, it will be engine activation
And when it's made as a build file and executed
then it becomes the app execution step
At this step, you know the things we wrote beforehand
The actors that inherited UObject
We create the Actors beforehand
Create them first
And how it's made is
Like this
Let's bring the picture
Here, there's the header tool
When the Unreal header tool runs
it creates the Actor beforehand
It doesn't create one, but 2 instances
It creates 2 classes
It creates the UClass
and the Unreal Object Class
And using those, it makes objects
It makes instances
The UClass instance and
the CDO instance
The UClass instance is used in reflection
So when there's an object
When they're comparing the objects
Or in engine or blueprint
casting to see if the object is there
Those using reflection
Then they process by using UClass
Then we create components
When we create them
Let's say for example, there's a class
If there's class A
Inside, there are variables int A, int B
Let's say that there are about 100 variables
Then when we create object A
In constructor, this has to be set up one by one
We need to copy all of them
We need to insert them through substitution
But the inserting process itself takes a long time
Inserting like this
So instead, we create this beforehand
We create object A
Then when we create objects after that
When we create this Actor
Then we copy the object that we pre-made
We do memory copy
Then memory copy is much faster than inputting one-by-one
For that use, we create CDO
This stands for
Class Default Object
That's how we call it
So we make these two instances
So the role of the constructor in Unreal is
When we make an object, rather than calling the constructor then
and creating it like this every time
If there's nothing, make one
and for all the ones coming after
Memory-copy the existing ones
That's the structure
So we need to write that here
How that will work is
I explain more because it's the beginning
Later, I'll just continue quickly
The name of the collider component is BoxComp
There's the CreateDefaultSubobject function
Using that
We create this
Like this
This is in the template form
So here, put UBoxComponent
And after that, we put the name
The name earlier, in Unreal engine
This name here, that's the name
So there's a rule
We can't have the same name in the class
So I recommend using the variable name as-is
You don't have to do it this way
If you can distinguish it surely, then you can use it
But if it's hard to name
use the same name as the variable
Then we'll place this in Root
And there's the SetRootComponent function
It's a function
Here, we put the new component
We'll put it like this
And to use this
We need to do add header here
When we don't know what header this is
Then in Google, let's write it like this
Search it like this, and it will come up
There's a document
Click on it, and it looks like this
Here, they say include this for header
Then we include this
And Unreal basically
is composed of combinations of modules
It's a class included in the module, engine
So copy this
and add it like this
For those of you using visual assist or ReSharper like me
There's a shortcut here
You can do Shift-Alt-Q to include
You can do it like that as well
Here, the header is like this
This is what's written here
You can write it like this, or like this
Then let's make the component for the outer appearance the same way
This is the mesh component
CreateDefaultSubobject, then text
Write it the same way
UStaticMeshComponent
We'll name it MeshComponent
And then
Should we make the Arrow along with it
It's the player, so the Arrow for shooting later
Let's make it here first
class UArrowComponent
Should we put Arrow
Same thing, we need to add this
Here, in the constructor as well, the Arrow
CreateDefaultSubobject
Write it
UArrowComponent, and this also has to be added to headers
StaticMeshComponent won't necessarily need it
It's probably already included somewhere
You can use it as-is
Like this
Then all of these need to be added to Root as well
MeshComponent in Root
SetupAttachment
And we'll write RootComponent
Same thing for Arrow
Write it like this
We'll have to change the location for the Arrow
Let's try this later, when we're shooting
Let's arrange it like this for now
Gun
Then should we compile it now?
Delete the cube
Save it, then we'll compile it
It worked well
Double-click on it
And you can see that it changed like this
The components that we added in C++ are shown here
Then here, on the mesh in charge of the outer appearance
Should we put the StaticMesh in here?
Search for cube here
It sometimes doesn't appear for some of you
Then open this, and it's like this
If show engine content
is not checked, then you won't be able to see them
So you have to click that in order to see these
Like this, cube
Then here, there's Approach Size
Right now, it's in 100x100x100
There's a same cube, but the size is different
It's in 256x256x256
I'll use the 100x100x100 one, or 1 m
You have to watch for it
It's placed in viewport now
Compile, then
We can also insert the Material under that
You can use it as-is
But if you put basicshapematerial
Then the material that we used first is this guy
We'll use this
Should we move the arrow up a little
Rotate it a little
After setting it like this
we can continue the work in C++ constructor
Normally when we work on this
at the prototype step, we use the blueprint often
And then after that
we take these content back to the source code
and fill in the values
That's how we process it
Player Movement Process
Then after that, we'll try moving this
When we execute it, the player will appear
Now it just has to move
To move, same thing here
Use the four types of input
to move it
Let's fill it in, in input
Make 2 access mapping
One is horizontal, one is vertical
Horizontal and Vertical
Insert it like this
For horizontal, we'll put A, D, W, S
A is negative, and S is negative
And now we're going to work on that here
Basically here
There's this function
SetPlayerInputComponent
Here, I'm going to receive the key input
But when we work on it here, the structure is
We need to create a function that will be called when the key is pressed
So we connect that function to here
This is called bind
It's literally binding
It would mean linking them together
So we process this through binding
Then
In order to bind, we need 2 functions first
Go to the header file
Under that, here
In response to the input
The horizontal and vertical inputs
will be received by a function
Let's write it like that
So it's a void type
OnHorizontal... I'll use the prefix like this
OnAxisHorizontal
We're going to receive it as float value
Then void again, OnAxisVertical
Right now, we don't need it but
I mentioned UProperty earlier
But this is about the variable
For function as well, in order to do reflection
There's the UFunction macro
This can be used to do reflection
But I don't think we need that at the moment
I'll put it like this for now
We can use the UFunction and UProperty macro
to do reflection processing
Of course, it doesn't just have the reflection function
Using this, we can do additional jobs as well
We're going to implement this now
We can implement it as-is, but
Press Alt+Enter, and this will show
I'm using extension now, but let me turn it off for a second
Do Alt+Enter, and it will appear like this
It's the same
You can use this, or the one at the top
Press Enter, and the function is implemented automatically
The function that I'm talking about is method
The member function. So going to the player Pawn
You can see that the two functions are implemented on the bottom
We'll have to implement them now by actually linking them
We'll link them here
Here, going to the parameters
There's something called Player Input Component
We can bind using this guy
There's action and axis, but we're using axis
Put axis
And then here
The name that we wrote here
Use the name that we wrote in mapping
We made Horizontal and Vertical
Write it like this
To be safe, put TEXT
Then put a comma
Second, we put the user class
This is talking about the class that will actually receive the input
then we'll put ourselves
It's object, not class
Since it's a pointer, we're putting this pointer
Then we link the functions
Right now, it's Horizontal, so this one, AxisHorizontal
Then the full name is this
Starting from the class name, write everything
Write it like this
In the function, we need to add ptr, or the pointer
So put & in the front
And through the veterans
we'll hand over the address of this function
Use the same method to make Vertical
Vertical
We linked the two like this
Then the value will be handed over
Then we'll remember this value and move in Tic
Since we need to use this value in another location
We'll have to save this
So in header, we'll make two variables to save these values
Make variables h and b, and we'll save them to use
Here in h, put value
And here in b, put value like this
We're almost done
Go to Tic
Now we move in Tic
Recall the motion equation
p = p0 + vt, that's the equation that we're going to use
The delta time comes over
In blueprint, it was delta second
Here, it says time
Since delta time comes over, we'll use that
Then the final location is, after making a vector at the current location
It becomes velocity multiplied by delta time
Let's first make velocity
It starts with f, vector
We'll put velocity
Velocity needs direction
We need to make the direction
If we're doing absolute direction, put FVector
Then this is x, y, z
x, y, z
Then the left right corresponds to h, or y
In the coordinate, the left and right direction is y
and up and down is z
So in y, put h, and in z, put v
Then
It will be 0, h, v
Now that we made direction, we also need speed
We'll create a speed variable
Like this, put multiply speed
Then now it's the velocity
We'll have to check it in header
Put UPROPERTY
Then we'll make a variable, float speed
Like this
Then we need Normalize
So here
Normally we can use the Normalize function
but the return value doesn't return vector
It returns success or fail
So instead of that, put safe
There's SafeNormal
Use this
to obtain Normalize
Here, make elaborated vector
Receive the Normalize vector, and there
Multiply speed to make velocity
And then
We also need p0
p0 is GetActorLocation
Let's still make it
We'll put p0
GetActorLocation
Then finally, we just need to insert it
SetActorLocation, p0 + velocity
times t, DeltaTime
We wrote it like this
We'll have to compile first
Compile like this
Then execute it
It should move, but it's not moving
Let's check again
Ah we didn't give a value for speed
I didn't put anything for speed
In Unreal, the constructor is used
to reset this value
Then it becomes reset to 0
So we'll put 500
Come back here, and compile again
Go to Player Pawn, go to class default
It says 500
Like this
Execute it, and it works well
Let's summarize the content learned in this lecture
Basic Project Settings Creating project based on C++ and the basic setting methods Role of the constructor in Unreal and CDO UPROPERTY, UFUNCTION macro
Player input process Creating and binding function for user input process in C++
Player movement process Creating the component in the constructor function Using the input value to move based on the user's input
The End