Casting - C++ syntax and UE syntax

What is the difference between those 2 casting options?

ABuildingsMain* obj = (ABuildingsMain*)AVikingKingPlayerController::mouseHit.GetActor();

ABuildingsMain* obj = Cast<ABuildingsMain>(AVikingKingPlayerController::mouseHit.GetActor());

Both work, So why do I have to use UE syntax and not the regular C++ syntax?

First one is C syntax the 2nd is C++ syntax actully (which actully do the same thing in this case), C++ got more casting methods thats why its done this way

Theres no such thing as UE4 syntax, macros used in UE4 are valid C++ syntax

There is no such thing as UE syntax, UE4 macros is valid C++ syntax. This probably have to do with C++ casters this might picks the right one:

Thank you :slight_smile:

I ask you to watch from the first steps style of registration of the programs. Do spaces, write comments, use intelligent names of variables and functions, separate logical parts of the program from each other in the empty lines. Remember that “excess” gaps and empty lines in Xi are admissible everywhere, except images of constants and names. (As on FORTRAN-e) it is very heavy to read and understand the programs on Xi filled in one column. Because of it it is difficult to find the lost bracket{s}, the lost symbols’;’ and other mistakes.
There are some “schools” of registration of programs - get accustomed to examples in this book and in other sources - and choose any! Anything terrible if you mix these styles. But - FAR AWAY OT FORTRAN - and!!!
The program can be formatted automatically to an “initial” look by means of, for example, cb program, but it is better to make out at once the program correctly.
Allocate logically independent (“closed”) parts of the program in function (even if they will be caused the only time). Functions - means to avoid repetition of the same operators in the text of the program, but also the means of structuring process of programming doing the program more clear isn’t simple. First, you can in other program use the text already written by you before function instead of writing it anew. Secondly, the operation issued in the form of function can be considered as an indivisible primitive (from quite simple on sense, like strcmp, strcpy, to the quite difficult - qsort, malloc, gets) and to forget about its internal device (it well - should be remembered less).
You don’t pursue brevity to the detriment of clarity. Xi allows to write sometimes such expressions over which it is possible to puzzle half an hour. If to write down them less subtly, but is slightly longer - they are axiomatic (and are more protected by it from mistakes).
In UNIX system you can look at the description of any team of system or the Xi function, having made team
(man - from the word manual, “management”).
One more parting word: learn English! Practically all programming languages use English words (as keywords, terms, names of variables and functions). Therefore it is better to understand value of these words (though their perception as simply certain symbols too has certain advantages). Back - programming on Xi will help you to learn English. All information to study of program language on

You don’t have to use the UE4 templated cast function, but it is much safer.

This is all about up-casting vs down-casting and type safety. There is some type safety stuff that happens with up-casting because it can be automatic. You can always cast a AActor to a UObject automatically but you obviously can’t automatically cast a UObject to a UComponent because it could actually be an AActor or even a UMaterial!.

Because down casting isn’t so easy, you use explicit casting like Cast<UBaseclass>() or the C raw pointer cast (UBaseclass*). The issue with the normal C cast is that it does not perform any run-time type checking, it simply tells the compiler that you want the pointer to be treated as a pointer of some other type. This works fine if you know the pointer is always going to be pointing at an object of the type you are expecting, but what happens if it’s not?

In comes the Cast template function. How this works is that it uses the UE4 reflection information to verify that the type you want to cast to is in fact something that is allowed. If it is not, it simply returns nullptr.

An example of where this is handy is in the AActor::RecieveHit(). Say the actor is a energy bolt cast from a spell. Now this spell only deals damage to Trolls so in RecieveHit() on your spell bolt you can write something like this:

ATroll* troll = Cast<ATroll>(Other);
if (troll != nullptr)

If you were to use the normal C cast like this.

ATroll* troll = (ATroll*)Other;

And the actor you hit was a wall. The troll pointer would not be null and then when you call KillTroll(), the game would probably crash.

There is also a variation of CastChecked() which will assert if the type cannot be cast to. It is useful for making sure that higher level code is providing you with the correct inputs.


Can someone please give an example of when to use Cast and when to use CastChecked? I’ve read that CastChecked is faster but I’m not sure when should I use it and how. For example, do I still need to verify for nullptr if using CastChecked like we need to do for Cast?

CastChecked will assert (break to the debugger, or crash the program) if the cast is unsuccessful, so you just use it in cases where the object must be of the type you are casting to, and if it’s not then it implies a programmer error which needs resolving. It can’t return nullptr, so there is no point in null checking.

Thank you!

I’d like to add something to the topic.

So UE Cast can only be used on UObjects which means some classes with F prefix do **not **fit.

Here’s an example where you can only use C++ syntax but static_cast would still be preferred than the raw pointer cast.

IAssetEditorInstance* AssetEditor = FAssetEditorManager::Get().FindEditorForAsset(Blueprint, true);
if (AssetEditor)
    FBlueprintEditor* BlueprintEditor = static_cast<FBlueprintEditor*>(AssetEditor);

You can find more examples in the engine source code like the one above ^.

Regarding the previous post … don’t use static_cast for downcasting lightly (see…atic-cast-in-c). Use dynamic_cast instead.