简体   繁体   English

虚幻引擎4 —类型转换

[英]Unreal Engine 4 — Typecasting

I am currently developing my first classes in Unreal Engine 4. Coming from using UScript extensively, I'm getting a little bit confused by how typecasting works in pure C++. 我目前正在使用Unreal Engine 4开发我的第一堂课。由于广泛使用UScript,我对纯C ++中类型转换的工作方式有些困惑。 More specifically, class/object casting. 更具体地说,类/对象转换。

I am currently putting together a switch statement in MyCustomGameMode which calls upon MyCustomPlayerController for MyCustomPlayerControllerVariable. 我目前正在MyCustomGameMode中组合一个switch语句,该语句调用MyCustomPlayerControllerVariable的MyCustomPlayerController。

The function in question that I am overriding is this one: virtual UClass* GetDefaultPawnClassForController(AController* InController); 我要覆盖的有问题的函数是这个函数: virtual UClass* GetDefaultPawnClassForController(AController* InController);

Currently I'm trying to call the variable with the following line of code, which I know is incorrect, but I'm not sure why: 当前,我正在尝试使用以下代码行调用变量,我知道这是不正确的,但是我不确定为什么:

Cast<MyCustomPlayerController>(InController).MyCustomPlayerControllerVariable

I am interested in casting the "InController" to MyCustomPlayerController but Cast<MyCustomPlayerController>(InController) doesn't seem to work, what am I doing wrong here? 我有兴趣将“ InController”强制转换为MyCustomPlayerController,但Cast<MyCustomPlayerController>(InController)似乎不起作用,我在这里做什么错?

The cast will return a pointer to your player controller, so you'd need to use -> to dereference it. 强制转换将返回一个指向您的播放器控制器的指针,因此您需要使用->取消引用它。

const MyCustomPlayerController* MyController = Cast<MyCustomPlayerController>(InController);
check(MyCustomPlayerController);  // asserts that the cast succeeded
const float MyVariable = MyCustomPlayerController->ControllerVariable;

` `

When you cast, It always returns a pointer. 强制转换时,它总是返回一个指针。 So make sure you check if the cast is successful before you access variables from a pointer. 因此,请确保在从指针访问变量之前检查强制转换是否成功。

auto MyPC = Cast<MyCustomPlayerController>(InController);
if(MyPC)
{
    MyPC->MyVariable;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM