[英]Unreal Engine 4 — Typecasting
我目前正在使用Unreal Engine 4開發我的第一堂課。由於廣泛使用UScript,我對純C ++中類型轉換的工作方式有些困惑。 更具體地說,類/對象轉換。
我目前正在MyCustomGameMode中組合一個switch語句,該語句調用MyCustomPlayerControllerVariable的MyCustomPlayerController。
我要覆蓋的有問題的函數是這個函數: virtual UClass* GetDefaultPawnClassForController(AController* InController);
當前,我正在嘗試使用以下代碼行調用變量,我知道這是不正確的,但是我不確定為什么:
Cast<MyCustomPlayerController>(InController).MyCustomPlayerControllerVariable
我有興趣將“ InController”強制轉換為MyCustomPlayerController,但Cast<MyCustomPlayerController>(InController)
似乎不起作用,我在這里做什么錯?
強制轉換將返回一個指向您的播放器控制器的指針,因此您需要使用->取消引用它。
const MyCustomPlayerController* MyController = Cast<MyCustomPlayerController>(InController);
check(MyCustomPlayerController); // asserts that the cast succeeded
const float MyVariable = MyCustomPlayerController->ControllerVariable;
`
強制轉換時,它總是返回一個指針。 因此,請確保在從指針訪問變量之前檢查強制轉換是否成功。
auto MyPC = Cast<MyCustomPlayerController>(InController);
if(MyPC)
{
MyPC->MyVariable;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.