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

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

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

Using Slate In-Game

Networking and Multiplayer

Production Pipeline

XR

Developer Blogs


Send me a message.


Related Posts: