Casting Syntax: Cast<CLASSNAME>(OBJECT TYPE)
Tweak Object Ptr Syntax (MUST be in the protected section of the header file):
TweakObjectPtr<Class CLASSNAME> VAR_NAME
Tweak Object Ptr Syntax (.cpp)
Casts cannot be put in the constructor. It will crash Unreal Engine.
I need to convert Blueprint Cast to C++ Cast
First, we need to understand the syntax. In Unreal Engine, you could use their version of Cast or the C++ Cast. It doesn’t matter.
As so eloquently described by Alen Loeb in this video. The syntax is:
It compares to the blueprint equivalent like this:
I wanted my cast to AVrCharacter to get the camera component and save it as a UCameraComponent Reference.
If you come from blueprints you’d logically think you’d have to do this:
But that’s wrong. GetPlayerCharacter() is included in “Classes/Kismet/GameplayStatics.h” and has two parameters you need to put into it.
So, GetPlayerCharacter(FIND THE PLAYER, PLAYER INSTANCE))
*Do not do*
If you are trying to access the Player Character you need to do: UGameplayStatics::GetPlayerCharacter(GetWorld(),0) instead
Just find the right component using FindComponentByClass<>();
If you’re saving this to a reference/pointer variable, there’s a better way to do this.
You would have to use TweakObjectPtr<>
The syntax goes as follows:
As close as I can figure, this is Variable type. As you’ll have this in your .cpp/.h:
Why is this so useful?
It allows you to do this: “Camera->”!
You can set his variable type to a cast, as I did.
You can put this into your header file for convenience.
In your UCLASS, under the “protected:” use the following syntax to create the member variable.
TWeakObjectPtr<class INSERT_NAME_OF_CLASS> VARIABLE_NAME;
TWeakObjectPtr<class UCameraComponent> Camera;