Using the Cast template function
Unreal Engine is designed as a universal graphics development tool. It provides base classes that can be inherited for developing specific games. That means the standard functions can only return the base type results, and game developers are responsible for casting the results to the specific types defined in their games.
In Unreal, Cast
is a safe conversion function used for casting data types. Here is the syntax:
ToType* Cast<ToType>(FromType* theObjectPointerOfFromType)
Let’s break the syntax down:
ToType
represents the target type that you want to get after the conversionFromType
represents the original type that you want to convert from- The angular brackets (
<
and>
) are C++ template expressions - The function returns
nullptr
if the casting operation failed
In C++, a function can be declared to be generic with a template expression, which means that the function can accept and process different...