A compilation of references I use for Unreal Engine C++ code. Makes it easier to copy-paste functionalities that are used quite often.
Developer References
This section compiles resources I’ve gathered from various forums posts, websites, blogs, and videos.
UnrealCPP.com
- Create static mesh
- Add mesh from file
- Add component
- Debug logging
- Draw debug helpers
- Find player position
- Hide and disable actor in game
- Get number of pawns
- Set actor location and rotation
- Rotating actor
- Set view target
- Set view target with blend
- Timer actor
- Register component hit
- Sweep multiple line trace
- Actor line trace
- Add radial impulse to actor
- Character overlap events
- Trigger box
- Trigger box with specific actor
- Trigger volume
- Trigger volume with specific actor
- Light switch overlap trigger
- Bind button to character
- Light switch on button press
- Rotate angle axis
- Rotate around vector
- Rotate actor around player with rotation
- Rotate actor around player
- Floating actor
- Unreal Engine 4 camera director tutorial
- Receive player input
- Colliding pawn UE4 tutorial
- Line trace on tick
- Line trace on fire
- Add force to mesh
- Destroy actor on overlap
- Change material mesh
- Open swing door
- Open door with lerp and overlap
- Open door with timeline and curve float
- Pickup, rotate, and throw object like gone home
- Camera shake
- Health bar and UI HUD
Debug Logging
// Debug Logging from unrealcommunity.wiki
UE_LOG(LogTemp, Warning, TEXT("Hello"));
UE_LOG(LogTemp, Warning, TEXT("The Actor's name is %s"), *YourActor->GetName());
UE_LOG(LogTemp, Warning, TEXT("The boolean value is %s"), ( bYourBool ? TEXT("true") : TEXT("false") ));
UE_LOG(LogTemp, Warning, TEXT("The integer value is: %d"), YourInteger);
UE_LOG(LogTemp, Warning, TEXT("The float value is: %f"), YourFloat);
UE_LOG(LogTemp, Warning, TEXT("The vector value is: %s"), *YourVector.ToString());
UE_LOG(LogTemp, Warning, TEXT("Current values are: vector %s, float %f, and integer %d"), *YourVector.ToString(), YourFloat, YourInteger);
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::White, TEXT("This message will appear on the screen!"));
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("Some variable values: x = %f, y = %f"), x, y));
// DrawDebugString
#include DrawDebugHelpers.h
DrawDebugString(GetWorld(), GetActorLocation(), "PLAYER SPOTTED", nullptr, FColor::White, 4.0f, true);
Spawn an Actor
FVector Location(0.0f, 0.0f, 0.0f);
FRotator Rotation(0.0f, 0.0f, 0.0f);
FActorSpawnParameters SpawnInfo;
GetWorld()->SpawnActor<AActor>(Location, Rotation, SpawnInfo);
Send HTTP GET Request
This example is taken from this video by Flopperam. Check out VaRest plugin for blueprint HTTP requests.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/GameModeBase.h"
#include "Http.h"
#include "HttpTestGameModeBase.generated.h"
UCLASS()
class HttpTest_API AHttpTestGameModeBase : public AGameModeBase
{
GENERATED_BODY()
public:
virtual void StartPlay() override;
private:
void OnResponseReceived(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bConnectedSuccessfully);
};
#include "HttpTestGameModeBase.h"
void AHttpTestGameModeBase::StartPlay()
{
Super::StartPlay();
FHttpRequestRef Request = FHttpModule::Get().CreateRequest();
Request->OnProcessRequestComplete().BindUObject(this, &AHttpTestGameModeBase::OnResponseReceived);
Request->SetURL("https://jsonplaceholder.typicode.com/posts/1");
Request->SetVerb("GET");
Request->ProcessRequest();
}
void AHttpTestGameModeBase::OnResponseReceived(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bConnectedSuccessfully)
{
UE_LOG(LogTemp, Display, TEXT("Response %s"), *Response->GetContentAsString());
}
Parse JSON Data
The example above retrieves JSON data. We can parse this data using the code changes below.
#include "HttpTestGameModeBase.h"
#include "Json.h"
void AHttpTestGameModeBase::StartPlay()
{
Super::StartPlay();
FHttpRequestRef Request = FHttpModule::Get().CreateRequest();
Request->OnProcessRequestComplete().BindUObject(this, &AHttpTestGameModeBase::OnResponseReceived);
Request->SetURL("https://jsonplaceholder.typicode.com/posts/1");
Request->SetVerb("GET");
Request->ProcessRequest();
}
void AHttpTestGameModeBase::OnResponseReceived(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bConnectedSuccessfully)
{
TSharedPtr<FJsonObject> ResponseObj;
TSharedRef<TJsonReader<>> Reader = TJsonReaderFactory<>::Create(Response->GetContentAsString());
FJsonSerializer::Deserialize(Reader, ResponseObj);
UE_LOG(LogTemp, Display, TEXT("Response %s"), *Response->GetContentAsString());
UE_LOG(LogTemp, Display, TEXT("Title: %s"), *ResponseObj->GetStringField("title"));
}
HTTP POST Request
#include "HttpTestGameModeBase.h"
#include "Json.h"
void AHttpTestGameModeBase::StartPlay()
{
Super::StartPlay();
FHttpRequestRef Request = FHttpModule::Get().CreateRequest();
TSharedRef<FJsonObject> RequestObj = MakeShared<FJsonObject>();
RequestObj->SetStringField("title", "foo");
FString RequestBody;
TSharedRef<TJsonWriter<>> Writer = TJsonWriterFactory<>::Create(&RequestBody);
FJsonSerializer::Serialize(RequestObj, Writer);
Request->OnProcessRequestComplete().BindUObject(this, &AHttpTestGameModeBase::OnResponseReceived);
Request->SetURL("https://jsonplaceholder.typicode.com/posts/1");
Request->SetVerb("POST");
Request->SetHeader("Content-Type", "application/json");
Request->SetContentAsString(RequestBody);
Request->ProcessRequest();
}
void AHttpTestGameModeBase::OnResponseReceived(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bConnectedSuccessfully)
{
TSharedPtr<FJsonObject> ResponseObj;
TSharedRef<TJsonReader<>> Reader = TJsonReaderFactory<>::Create(Response->GetContentAsString());
FJsonSerializer::Deserialize(Reader, ResponseObj);
UE_LOG(LogTemp, Display, TEXT("Response %s"), *Response->GetContentAsString());
UE_LOG(LogTemp, Display, TEXT("Title: %s"), *ResponseObj->GetStringField("title"));
}
Actor Lifecycle
StartPlay() vs BeginPlay()
BeginPlay is an event that gets called at the beginning of a level being opened. Start Play is a function you can call with GameModes, from what it says, “Trasitions to WaitingToStart and calls BeginPlay on actors” I would say it does probably just that. Hope this helps [Link]
As i noticed while doing this tutorial, the “StartPlay” is only replacing inside a GameModeClass. The other classes, like character etc. are using the “BeginPlay”. Cause Character has the BeginPlay Method, your “StartPlay” can’t override. Just try to only use “StartPlay” in the GameModeClass. [Link]
Owner or Instigator
“So short version, Owner is the Actor responsible for spawning another Actor, while Instigator is a Controller class who started the series of events which will eventually result in a given actor going something (e.g. a bullet killing an enemy).”
“A player controls a tank to fire a projectile, the player is the instigator, the tank is the owner, that simple ! get it?”
[Link]
Events vs Functions
Primary differences taken from here and here:
- Function can return value(s). Event cannot have any output.
- Event can use Delay node or timelines, functions cannot. Functions are guaranteed to execute
and return immediately by limiting what kinds of nodes can be placed in a
function (latent actions, timelines, etc… are all prohibited). - Function can use local variables(s).
Managing complexity in Blueprints (2014) by Michael Noland.
Interfaces vs Casting performance
“There are also performance benefits when Interfaces over casting, as loading an Actor that casts to another Actor will subsequently load that Actor to memory as well. If not treated carefully, this could result in a cascading loading effect where loading a single Actor results in several other Actors being loaded into memory as well.” [Link]
Georgy Treshchev
- How to use delays in C++ in Unreal Engine
- Reducing build size of Android or iOS game in Unreal Engine
- How to integrate third-party library into Unreal Engine
Constructor with or without FObjectInitializer (link)
Multiplayer / Networking
Property/Variable Replication
#include "Net/UnrealNetwork.h"
class ENGINE_API AActor : public UObject
{
UPROPERTY(Replicated)
float Health;
AActor();
};
AActor::AActor()
{
bReplicates = true;
// SetIsReplicatedByDefault(true); if component
}
void AActor::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
{
Super::GetLifetimeReplicatedProps(OutLifetimeProps);
DOREPLIFETIME(AActor, Health);
}
Unreal Engine C++ Documentation
Several years ago, the UE C++ documentation was not great. It barely had any sample code for basic actions such as doing a line trace, changing mesh material, triggering an event on overlap, etc. You had to scour through the UE forums to find the examples you need. Throughout the years more sample code was added, but some of the C++ topics are scattered in different places or buried in the sample projects. This prompted me to make a list of the most-used links.
Programming Basics
- Blueprint Function Libraries
- Game-Controlled Cameras
- Components and Collision
- Player-Controlled Cameras
- Player Input and Pawns
- User Interface with UMG
- Variables, Timers, and Events
- Casting Quick Start Guide
- Direct Actor Communication Quick Start Guide
- Event Dispatchers / Delegates Quick Start Guide
- Spawning and Destroying an Actor
- Using Timers
- String Handling
Using Slate In-Game
Networking and Multiplayer
- Networking and Multiplayer
- Setting Up Dedicated Servers
- Multiplayer Programming Quick Start
- Actor Replication
- Character Movement Component
- Online Subsystem
Production Pipeline
XR
Developer Blogs
- Tom Looman –
- Mr Robin’s Guide to Unreal Engine C++
- Unreal Engine Community Wiki –