top of page

Breaking Down Unreal

Casting Basics

  • Feb 5, 2022
  • 3 min read

Chances are that if you're here, you're curious about casting in some form or the other. This post will be very Blueprint oriented, as C++ users can translate the same use cases to C++.


Lets start with a basic statement. Casting is not a terrible thing as some tutorials or guides will have you believe. This page is simply about pointers and conversion, if you're more interested in the real issues of casting, see my "Understanding Pointers and Casting and Memory Management" posts!


Note, this is not a guide on how to get a valid pointer, but what casting actually is!




What do I need to know to understand casting?

In short, to understand casting you will first need a basic understanding of the following:


Classes and their hierarchies.

What is a Pointer? See my Understanding Pointers post.




Casting a pointer

So you understand what a pointer is, that it is nothing but a number pointing to a memory location of an instantiated object. And you also understand that classes are made up of a hierarchy?


Good, then casting is simple to understand. Lets do some demonstrations.














In the above image, I have called GetGameMode. This returns a pointer to the current GameMode at a hierarchy level of GameModeBase.


If you're unfamiliar with GameMode's hierarchy it's pretty simple.

UObject - Most basic object in Unreal Engine

AActor - Actor class, used to wrap around components and do networking

AInfo - Invisible, non scene component Actor type, used as a pure code class

AGameModeBase - The core of GameMode


So now we have a little number that points to the location in memory where the current level's GameMode is. We have this object, we know where it is, so why cast? Because chances are you have created functions here that you need to access. Maybe you call a game ending function which does some data work, checks for best players, gives out awards and then saves some data to disk.


You've written this in your own personal GameMode class like say my ArcaneVillageGameModeBP. Lets show it's hierarchy.


UObject - Most basic object in Unreal Engine

AActor - Actor class, used to wrap around components and do networking

AInfo - Invisible, non scene component Actor type, used as a pure code class

AGameModeBase - The core of GameMode

AGameMode - Match based GameMode, has some extra functions over GameModeBase

AArcaneVillageGameModeBP - My GameMode where I write functions.


Note that when I call GetGameMode, it's returning an object that IS A ArcaneVillageGameModeBP type actor, but as a pointer type of AGameModeBase. This means I can only access stuff on my ArcaneVillageGameModeBP at a level up to AGameModeBase with this pointer. To gain access to GameMode, or even my personal functions in ArcaneVillageGameModeBP I simply cast this pointer to ArcaneVillageGameModeBP type.


This keeps the same number, points to the same exact memory location, but it changes the pointer type to allow different memory access.


Now that you understand how to cast a simple GameMode pointer.


You cannot cast a pointer to a type that it is not. If you try to take a GameMode pointer, that points to a GameMode object, and cast it to a GameState or Character, it will fail and return an invalid pointer that cannot be used for anything. Casting only works by trying to convert a pointer to a different type that exists in it's object's direct hierarchy.




Thank you for reading!

For a deeper understanding see Understanding Pointers.

 
 
 

Recent Posts

See All
UObject::Serialize

The Serialize function from the UObject class is a key feature in the engine that surprisingly few people know about. It isn't talked...

 
 
 
Saving Games Smartly

A cornerstone to games is the ability to save and load them. This is true whether you use a simple checkpoint system or a full blown full...

 
 
 

Comments


bottom of page