Skip to main content
Unreal Engine 5 – Complete Beginner to Advanced Guide
CHAPTER 06 Intermediate

C++ Programming in Unreal Engine 5

Updated: May 16, 2026
35 min read

# CHAPTER 6

C++ Programming in Unreal Engine 5

1. Introduction

Blueprints are fantastic for prototyping and fast iteration, but when building AAA games with massive performance requirements, C++ is king. The entire core of Unreal Engine 5 is written in C++. By dropping into the code layer, you gain absolute control over memory management, execution speed, and complex math calculations that would be impossibly messy as a giant web of Blueprint nodes. In this chapter, we will bridge the gap between visual scripting and raw code. We will learn the UE5 C++ architecture, how to expose C++ variables to the Editor using macros, and how to write a simple gameplay mechanic.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Understand the relationship between C++ and Blueprints (The Base Class concept).
  • Create a new C++ Actor class in the UE5 editor.
  • Understand the .h (Header) and .cpp (Source) file structure.
  • Utilize the UPROPERTY and UFUNCTION macros to expose code to Blueprints.
  • Write basic gameplay logic inside the Tick function.

3. The Blueprint-C++ Relationship

In professional UE5 development, you do not choose *between* C++ and Blueprints; you use them together.
  • The Golden Rule: C++ handles the heavy logic and math (The Brains). Blueprints handle the visual setup, components, and art references (The Body).
  • The Workflow: A programmer writes a C++ class called AWeapon. The designer creates a Blueprint child of AWeapon, assigns the 3D model of a shotgun, and sets the "Damage" variable to 50 in the editor.

4. Anatomy of an Unreal C++ Class

When you create a C++ Actor in Unreal, it generates two files:
  1. 1. The Header File (.h): The "Table of Contents." This is where you declare your variables and functions.
  1. 2. The Source File (.cpp): The "Implementation." This is where you write the actual logic (what the functions do).

5. Unreal Macros: UPROPERTY and UFUNCTION

Standard C++ code is invisible to the Unreal Engine Editor. If you want a designer to see your C++ variable in the Details Panel, you must "tag" it using an Unreal Macro.
  • UPROPERTY: Placed above a variable in the .h file.
  • UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="Stats")
  • *Result:* The variable can now be edited in the Details panel and used in Blueprint graphs.
  • UFUNCTION: Placed above a function.
  • UFUNCTION(BlueprintCallable, Category="Actions")
  • *Result:* Designers can now call this C++ function as a visual node in Blueprints!

6. Code and Automation Examples

Example: A Simple C++ Actor Header (.h)
cpp
1234567891011121314151617181920212223
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "MyFloatingActor.generated.h"

UCLASS()
class MYGAME_API AMyFloatingActor : public AActor
{
    GENERATED_BODY()
    
public: 
    AMyFloatingActor(); // Constructor

    // Expose this variable to the UE5 Editor
    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="Float Settings")
    float FloatSpeed;

protected:
    virtual void BeginPlay() override;

public: 
    virtual void Tick(float DeltaTime) override;
};

Example: The Source Logic (.cpp)

cpp
12345678910111213141516171819202122
#include "MyFloatingActor.h"

AMyFloatingActor::AMyFloatingActor()
{
    PrimaryActorTick.bCanEverTick = true; // Allow the Tick function to run
    FloatSpeed = 20.0f; // Default value
}

void AMyFloatingActor::BeginPlay()
{
    Super::BeginPlay();
}

void AMyFloatingActor::Tick(float DeltaTime)
{
    Super::Tick(DeltaTime);
    
    // Move the actor upwards every frame
    FVector NewLocation = GetActorLocation();
    NewLocation.Z += FloatSpeed * DeltaTime; 
    SetActorLocation(NewLocation);
}

7. Visual Learning: The C++ to Blueprint Pipeline

txt
123456789
[ C++ Header (.h) ] ---> Declares: UPROPERTY(EditAnywhere) float Health;
        |
     (Compiles)
        v
[ Unreal Editor ]   ---> 'Health' appears in the Details Panel!
        |
  (Designer Edits)
        v
[ C++ Source (.cpp) ] -> Uses the designer's new Health value in combat logic.

8. Best Practices

  • Use DeltaTime: If you move an object in the Tick() function by adding 1 unit of distance every frame, a player running the game at 120 FPS will move twice as fast as a player running at 60 FPS. Always multiply movements by DeltaTime (the time passed since the last frame) to ensure consistent speeds across all hardware.

9. Common Mistakes

  • Forgetting to Compile: If you change C++ code in Visual Studio/Rider and just press 'Play' in Unreal, nothing will happen. You must compile the code (using the Live Coding icon in UE5 or hitting Ctrl+Alt+F11) before the engine registers the changes.

10. Mini Project: Expose a Variable to the Editor

Objective: Prove you can bridge the gap between C++ and the UE5 UI.
  1. 1. In the Content Browser, right-click -> New C++ Class. Select Actor. Name it ACubeSpinner.
  1. 2. Open the .h file. Under public:, type:
UPROPERTY(EditAnywhere, Category="Rotation") float SpinSpeed;
  1. 3. Save, go back to Unreal, and hit the "Compile" (Live Coding) button.
  1. 4. Drag your new ACubeSpinner from the C++ Classes folder into the Viewport.
  1. 5. Look at the Details Panel. You will see a new category called "Rotation" with your SpinSpeed variable ready to be edited!

11. Practice Exercises

  1. 1. Define the difference between the .h file and the .cpp file in Unreal Engine programming.
  1. 2. Why is the UPROPERTY(EditAnywhere) macro considered a crucial bridge between Programmers and Level Designers?

12. MCQs with Answers

Question 1

A programmer writes a variable int AmmoCount; in C++, but the Level Designer complains they cannot see or edit the Ammo Count inside the Unreal Engine Editor. What did the programmer forget to do?

Question 2

To ensure that an object moves at the exact same speed regardless of whether a player's computer runs the game at 30 FPS or 144 FPS, what must you multiply the movement speed by in the Tick function?

13. Interview Questions

  • Q: Explain the ideal architectural relationship between C++ and Blueprints in a AAA game studio. Which system handles what tasks?
  • Q: Walk me through the syntax and purpose of UPROPERTY(EditAnywhere, BlueprintReadWrite). What does each parameter inside the parenthesis actually do?
  • Q: What is the purpose of GENERATED_BODY() at the top of an Unreal C++ class?

14. FAQs

Q: Do I need to learn C++ to use Unreal Engine? A: If you are a solo indie developer or an artist, no. You can build entire games in Blueprints. If you want to be hired as a "Gameplay Programmer" at a major AAA studio, yes, advanced C++ is absolutely mandatory.

15. Summary

In Chapter 6, we unlocked the raw power of the engine. We learned that C++ in Unreal Engine is heavily customized with macros like UPROPERTY and UFUNCTION, which act as magical bridges exposing raw code directly to the visual editor. We understood the division of labor: C++ classes act as the foundational "Brains" providing fast, optimized logic, while Blueprints act as child classes that allow artists and designers to safely tweak visual variables.

16. Next Chapter Recommendation

We can write code, but our player is stuck standing completely still. Proceed to Chapter 7: Player Movement and Input Systems.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·