Unreal Engine C++ Developer #1
This is the course notes
Lecture Notes on Unreal Engine C++ Developer #1
1.What is include
?
#include
is nothing else but just copy and paste what is inside to here. It is a sort of analogy to using
in C#(not entirely). You can specify which library you are using with #include
The static library and "official" libraries, let's say, are using <>
. While the latter ""
focusing on header file or libraries we built casually.
2.What is header file
?
A little bit similar to interface
in C#? Normally, we will declare the name and return type in header
file just like in interface
.
Following is a file called FBullCowGame.h
class FBullCowGame
{
public:
int GetMaxTries() const;
int GetCurrentTry() const;
bool IsGameWon() const;
void Reset();
bool CheckGuessValidity();
private:
int MyCurrentTry = 3;
int MyMaxTries;
};
Just like we define the function in interface
, header
file does something similar.
Then we complement the function in FBullCowGame.cpp
xxxxxxxxxx
void FBullCowGame::Reset()
{
return;
}
int FBullCowGame::GetMaxTries() const
{
return 0;
}
int FBullCowGame::GetCurrentTry() const
{
return MyCurrentTry;
}
bool FBullCowGame::IsGameWon() const
{
return false;
}
bool FBullCowGame::CheckGuessValidity()
{
return false;
}
Therefore, that's what I mentioned header
file is just about copy and paste
? In FBullCowGame.cpp
, the code in FBullCowGame.h
was copied and pasted here.
3.What is #prama once
?
It literally means runs once
.
4.Avoid using using namespace
in header
file even in .cpp
files
using namespace
is different from using xxx
in C#. It literally means take a namespace from the included library. The simple case is the std
library.
❌Not recommend:
x
using namespace std
int main()
{
cout << "Hello world!" << endl;
}
✔️Recommend:
x
int main()
{
std::cout << "Hello world!" << std::endl;
}
With your programming system growing, the using
could be really to debug.
5.What is std::getline()
?
It is a function taking user's input while it is different from std::cin
.
std::getline()
takes whatever user typed as a string.
x
std::string name;
std::cout << "Please, enter your full name: ";
std::getline (std::cin,name);
If the user type Norman Foster
, then the value of name
is Norman Foster
.
std::cin
takes only one word.
x
std::string name;
std::cout << "Please, enter your full name: ";
std::cin >> name;
If the user type Norman Foster
, then the value of name
is Norman
. The Foster
will be left in the memory and it will continue to feed the next string.
6.What is constexpr
and const
?
The naming of constexpr
and const
is very confusing in C++. I have a trick to remember these 2 guys.
constexpr
literally means constant you want to define which can hardly be changed. Like PI
:
xxxxxxxxxx
constexpr float PI = 3.141596;
const
literally means read-only. Since there is no such concept of Property
in C++ like in C#. Therefore you cannot define a read-only property like in C#:
x
public double PT{get => 3.141596;}
C++ has a more complicated way of doing that:
xxxxxxxxxx
float GetPI() const
{
return 3.141596;
}
The const
ban any assignment operation or value modification operation inside the function!
7.Shortcut for QuickActions and Refactoring
- Ctrl + .
This is very very very handy!! Highly recommend! The shortcut is Ctrl + .
For example, you want to auto generate the function you declare in the .h
. You can double click the function name and Ctrl + .
Then Visual Studio IDE will automatically generate the block of code for you.
8.Shortcut to Rename some variable or function - Ctrl+R
Double click the variable or function, Ctrl+R
Links
C++
Book: Beginning C++ Through Game Programming
Website: https://www.learncpp.com/
Unreal Coding Standard: https://docs.unrealengine.com/4.26/en-US/ProductionPipelines/DevelopmentSetup/CodingStandard/