Inside Unreal Engine C++ Programming
Inside Unreal Engine C++ Programming
First Part : Unreal Engine C++ Basic
Chapter 1: Five Basic Classes
1.1 UObject
The base class of all UE objects.
#include "UObject/Object.h"
1.1.1 Functionality Provided by UObject
- Garbage collection
- Reference updating
- Reflection
- Serialization
- Automatic updating of default property changes
- Automatic property initialization
- Type information available at runtime
- Network replication
UObjects should never use the new operator
1.1.2 Garbage collection
- using
UPROPERTY
- using smart pointer - non-uobject
1.1.3 Reflect
- Get Object Info at runtime
1.1.4 Serialization
- seri - des
1.1.5 Type information available at runtime
- /GR-, so cann’t use dynamic_cast
- using
Cast<>
instead - About
/GR-
: https://learn.microsoft.com/en-us/cpp/build/reference/gr-enable-run-time-type-information?view=msvc-170
1.2 Actor Class
Having the ability to handle Component
Actor is the base class for an Object that can be placed or spawned in a level. Actors may contain a collection of ActorComponents
, which can be used to control how actors move, how they are rendered, etc.
class ENGINE_API AActor : public UObject
1.3 Pawn/Character/Controlller
1.3.1 Pawn
Pawn is the base class of all actors that can be possessed by players or AI.
class ENGINE_API APawn : public AActor, public INavAgentInterface
1.3.2 Character
Characters are Pawns that have a mesh, collision, and built-in movement logic.
class ENGINE_API ACharacter : public APawn
1.3.3 Controller
Controllers are non-physical actors that can possess a Pawn to control its actions.
PlayerControllers
are used by human players to control pawnsAIControllers
implement the artificial intelligence for the pawns they control.
class ENGINE_API AController : public AActor, public INavAgentInterface
Chapter 2: New Object
2.1 Create Object
- Pure C++ Class(prefix
F
), usingnew
operator - Derived from
UObject
, but notAActor
, usingNewObject
AActor
and subclass, usingSpawnActor
2.2 Get Object
TActorIterator
// Get All Actors in this Level
for(TActorIterator <AActor> Iterator(GetWorld());Iterator;++Iterator)
{
//do something
}
2.3 Destory Object
- Pure C++ Class, using new -> delete
UObject
GCActor
Destory function
Chapter 3: C++ to Blueprint
3.1 UPROPERTY
UPROPERTY(BlueprintReadWrite,VisibleAnywhere,Category="Object")
3.2 UFUNCTION
UFUNCTION(BlueprintCallable,Category="Test")
Chapter 4: Gameplay
4.1 behavior tree
Behavior Trees assets in Unreal Engine 5 (Unreal Engine) can be used to create artificial intelligence (AI) for non-player characters in your projects. While the Behavior Tree asset is used to execute branches containing logic, to determine which branches should be executed, the Behavior Tree relies on another asset called a Blackboard which serves as the “brain” for a Behavior Tree.
The Blackboard contains several user-defined Keys that hold information used by the Behavior Tree to make decisions.
- Root -> Selector -> Keys
- Root -> Sequence -> Keys
Selector : If encounter ture
, then return true
Sequence: If encounter false
, then return false
4.2 Network Architecture
4.2.1 sync
- Action:
Player move 3 steps
- State:
Player Health
Client send Action
to server, after server
update all states
, it will sending state
to all clients.
cons: low latency
4.2.2 Client - Server Model
Replication is the name for the process of synchronizing data and procedure calls between clients and servers.
Chapter 5: Utils
5.1 Regex
#include "Regex.h"
FString TextStr("ABCDEFGHIJKLMN");
FRegexPattern TestPattern(TEXT("C.+H"));
FRegexMatcher TestMatcher(TestPattern, TextStr);
if (TestMatcher.FindNext()) {
UE_LOG(MyLog, Warning, TEXT("Found %d -%d"),
TestMatcher.GetMatchBeginning(),
TestMatcher.GetMatchEnding());
}
5.2 FPaths
FPaths::GameDir()
-> Get Game Root DirectoryFPaths::FileExists()
-> File Exists or notFPaths::ConvertRelativePathToFull()
5.3 JSON
#include "Json.h"
FString JsonStr = "[{\"author\":\"Tim\"},{\"age\":\"100\"}]";
TArray<TSharedPtr<FJsonValue>> JsonParsed;
TSharedRef< TJsonReader<TCHAR> > JsonReader = TJsonReaderFactory<TCHAR>::Cr
bool BFlag = FJsonSerializer::Deserialize(JsonReader, JsonParsed);
{
UE_LOG(LogSimpleApp, Warning, TEXT("Successed"));
FString FStringAuthor = JsonParsed[0]->AsObject()->GetStringField("auth
UE_LOG(LogSimpleApp, Warning, TEXT("author = %s"),*FStringAuthor);
}
5.4 FPlatformFileManager
File Read and Write
#include "PlatformFilemanager.h"
FPlatformFileManager::Get()->GetPlatformFile();
5.5 GConfi
GConfig->SetString(
TEXT("MySection"),
TEXT("Name"),
TEXT("Seccy"),
FPaths::GameDir()/ "MyConfig.ini");
// Read
FString Result;
GConfig->GetString(
TEXT("MySection"),
TEXT("Name"),
Result,
FPaths::GameDir() / "MyConfig.ini");
GConfig->Flush();
5.6 UE_LOG
UE_LOG(LogTemp, Warning, TEXT("Hell World"));
Custom Log Category:
DEFINE_LOG_CATEGORY_STATIC(LogMyCategory,Warning,All);
5.7 Strings
- FName
ReadOnly, case-sensitive,
- FText
display and localization
- FString
dynamic memory
5.8 deprecated
5.9 Images
ImageWrapper
- CompressedData
- RawData
Second Part : Inside Unreal Engine
Chapter 6 : Module
6.1 UnrealBuildTool(UBT)
- Public
- Private
- .build.cs
XXXX_API
expose to other class.
6.2 Create Module
6.3 UBT and UHT
- UHT -> Reflection
Output:
.generated.h
.generated.cpp
static variables init before main function
Chapter 7: Core
7.1 Memory
FMalloc* FWindowsPlatformMemory::BaseAllocator()
- Malloc(ANSI)
- Intel (TBB)
- Binned
7.2 Init
- Preinit
- init
FEngineLoop
- MainLoop
!GIsRequestingExit
7.3 Test
/Private
—A.cpp
—/Tests
—–ATest.cpp
#include "PrivatePCH.h"
#include "..."
DEFINE_LOG_CATEGORY_STATIC(TestLog, Log, All);
IMPLEMENT_SIMPLE_AUTOMATION_TEST(FMultiThreadTest, "MyTest.PublicTest
.MultiThreadTest", EAutomationTestFlags::EditorContext |
EAutomationTestFlags::EngineFilter)
bool FMultiThreadTest::RunTest(const FString& Parameters)
{
UE_LOG(TestLog, Log, TEXT("Hello"));
return true;
}
Window->Develop Tools->Session Frontend->Start Tests
7.3.1 Thread
FRunnable
TGraphTask
7.3.2 sync
Async
7.4 GenericPlatformProcess
CreateProc
Chapter 8: Object Moduel [Unfinished]
8.1 UObject
8.2 Actor
Chapter 9: Render System
9.1 Render Thread
- Render Thread
- Game Thread
In FEngineLoop::PreInit
, to init Render Thread
.
9.2 Render Architecture
-
Deferred Rendering(Default method of rendering)
-
Front Rendering
https://docs.unrealengine.com/5.0/en-US/forward-shading-renderer-in-unreal-engine/
pros and cons
Third Part : Unreal Engine Extension
###