Unreal Engine C++ const, pointers and references
in ,

Mastering const, Pointers, References, and Passing by Value in Unreal Engine C++

If you’re diving into Unreal Engine C++ (or even if you’ve been paddling around for a while), you’ve probably run into const, pointers (*), references (&), and the whole “pass-by-value vs pass-by-reference” thing.
It can feel like C++ is trying to trip you up on purpose. (Spoiler: It is.)

But once you really get how these work, your Unreal projects will level up big time — cleaner code, fewer bugs, better performance.

Let’s break it all down, GameDevVault style: no fluff, just clear, practical info you can use today.


const — Your Code’s Bodyguard

const means “this thing shouldn’t change.”
In Unreal (and C++ in general), you slap const on variables, pointers, references, and even functions to lock them down.

Example:

void PrintName(const FString& Name);

Here, Name is a reference (&), but const says, “Hey, inside PrintName, you are not allowed to modify Name.”

Why bother?
Because const helps:

  • Prevent bugs (accidentally changing stuff you shouldn’t).
  • Let the compiler optimize more aggressively.
  • Tell other devs (and your future self), “this is safe to use.”

Use const whenever you don’t need to change a parameter or a function’s internal data. Your future self will thank you.


Pointers (*) — The Treasure Maps of C++

A pointer is just a memory address. It’s like a treasure map that says, “The real data is over there.”

In Unreal, you’ll often see raw pointers like this:

AActor* TargetActor;

TargetActor isn’t the actor itself — it’s a map leading to it.

Important:
A pointer can be:

  • Valid (pointing to something real).
  • Null (nullptr) — meaning it points to nothing.

Always check your pointers before using them:

if (TargetActor)
{
    TargetActor->Destroy();
}

Unreal Engine loves pointers because game objects (like AActor, UObject, etc.) are often dynamically created and managed behind the scenes.


But beware: raw pointers don’t own memory. They’re just “borrowing” the address. That’s why Unreal uses smart pointers (TWeakObjectPtr, TSharedPtr, etc.) to avoid memory leaks — but that’s another blog post for another day.


References (&) — Fancy Aliases

A reference is a nickname for something. It’s basically a pointer you can’t make null or point elsewhere.

Example:

void TakeDamage(float& Health);

Health here is passed by reference. If TakeDamage changes it, the original variable changes too.

void TakeDamage(float& Health)
{
    Health -= 10.0f;
}

Using references makes your functions more efficient (no copying big structs or classes) and safe (since you can trust the reference is valid).

Pro Tip:
Use const references (const Something&) if you don’t need to modify the thing you’re referencing — again, safer and faster.


Pass-by-Value vs Pass-by-Reference — Choose Wisely

When you pass-by-value, you’re making a copy.

void UpdatePosition(FVector Position);

Here, Unreal will copy the entire FVector. If you change Position inside UpdatePosition, the original stays the same.

Pass-by-value is fine for small types (like int, float, etc.), but for bigger structs (FVector, FTransform, custom structs), it gets expensive fast.


When you pass-by-reference, you’re passing the original:

void UpdatePosition(FVector& Position);

Now you’re working on the real thing, no copy involved.
And if you don’t need to modify it? Pass a const reference:

void UpdatePosition(const FVector& Position);

Summary:

  • Small types? Pass-by-value is fine.
  • Big types? Prefer pass-by-const-reference unless you need to change them.

Real-World Example — Making It Click

Imagine a function that moves an enemy:

void MoveEnemy(AEnemyCharacter* Enemy, const FVector& TargetLocation);
  • Enemy is a pointer because it could be nullptr if the enemy is already dead or invalid.
  • TargetLocation is a const reference because it’s probably a big struct (3 floats), and we don’t want to accidentally modify it inside MoveEnemy.

Inside the function:

if (!Enemy)
{
    return;
}

Enemy->SetActorLocation(TargetLocation);

Clean, fast, and safe. Just how we like it.


🔥 Golden Rules Cheat Sheet for Unreal Engine C++

If parameter is:Then use:
Small types (int32, float, bool, FVector)Pass by value
Complex types (FYourStruct, TArray, TMap, TSet)Pass by const ref (const TArray<>&)
Function doesn’t modify object stateMark the function itself as const (Type Function() const)
If you modify input parameter inside functionStill pass by value or non-const ref if needed

Wrapping Up

Getting comfortable with const, pointers, references, and passing-by-reference vs passing-by-value is one of the most important C++ skills you can build — especially for Unreal Engine development.

It’s not just about being “correct” — it’s about writing faster, safer, better code that your future self (and your teammates) will actually want to work with.

Key takeaways:

  • Use const everywhere you can.
  • Understand when you’re working with a real object, a pointer, or a reference.
  • Avoid unnecessary copies.
  • Always check your pointers before using them.

Master these basics, and your Unreal projects will run smoother than ever. 🚀

Want more Unreal C++ deep dives? Let me know what topics you’re stuck on — I’m always down to tackle more vault-worthy tutorials.

Happy coding, devs!

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

GIPHY App Key not set. Please check settings

Loading…

10 Must-Read Books for Game Developers 📚🎮