Daily Unreal Column #17 - CastChecked
Writing code in Unreal Engine without using `Cast<>` function is close to impossible. Did you know that there is another version of that function called CastChecked?
CastChecked
has two parameters, not one (although second parameter has default value). The second parameter is of type ECastCheckedType
and can have two values:
- NullAllowed
- NullChecked
(default value)
When NullChecked
is used, the behavior is the same as explained above. When NullAllowed
is used, the function is NOT going to halt the program if we pass a nullptr
as the first parameter. For example:
AActor* MyActor = nullptr;
APawn* MyPawn = CastChecked<APawn>(MyActor, ECastCheckedType::NullAllowed);
Now, without using the NullAllowed
, the above code would halt the programs execution once it tries to run the above lines. However, with the NullAllowed
it is simply going to return a nullptr
and move on.