简体   繁体   English

虚幻引擎C ++ SpawnActor Cast

[英]Unreal Engine C++ SpawnActor Cast

I'm coding a multiplayer game in Unreal Engine 4.15.0 and it's almost working except for a freaking line of code that I can't figure out it's behavior. 我正在用Unreal Engine 4.15.0编写一个多人游戏,并且几乎可以正常工作,只是我无法弄清它的行为异常。 The problem is that I have a OnlinePawn Blueprint that inherits from a C++ base class AOnlinePawn. 问题是我有一个从C ++基类AOnlinePawn继承的OnlinePawn蓝图。 Whenever a player joins the match I spawn this Blueprint and cast it to an array that I update constantly receiving data. 每当有玩家加入比赛时,我都会生成此蓝图并将其投射到我不断更新以接收数据的数组中。

The problem is that the following code that tries to casts AActor* returned by the function to AOnlinePawn* fails and returns null, it's a cast issue as it is indeed spawned. 问题在于,以下尝试将函数返回的AActor *强制转换为AOnlinePawn *的代码失败,并返回null,这确实是一个强制转换问题。

The cast: 演员:

playersInstances[i] = Cast<AOnlinePawn>(GetWorld()->SpawnActor(OnlinePlayer->GeneratedClass, &position, &rotator));

The blueprint reference: 蓝图参考:

static ConstructorHelpers::FObjectFinder<UBlueprint> OnlinePlayerBP(TEXT("Blueprint'/Game/Blueprints/OnlinePawn.OnlinePawn'"));
OnlinePlayer = OnlinePlayerBP.Object;

The Blueprint description, stating the parent class: 蓝图描述,说明父类:

蓝图描述,说明父类。

So, what's the problem here? 那么,这是什么问题呢? There's anyway I can improve my workflow? 无论如何,我可以改善我的工作流程吗? There's a workaround? 有解决方法吗?

I think your cast line is not correct. 我认为您的演员表不正确。 SpawnActor is a template function, you are not supposed to cast it manually like that. SpawnActor是一个模板函数,您不应像这样手动进行转换。 Instead, you should simply give it the type you want to use. 相反,您应该简单地为其指定要使用的类型。 Try it like this: 像这样尝试:

AOnlinePawn* NewActor = GetWorld()->SpawnActor<AOnlinePawn>(GetClass(),  &position, &rotator); 

EDIT: 编辑:

You're welcome. 别客气。 So if you actually want To reference a blueprint in your code you can either use the constructor helpers or the class finder. 因此,如果您实际上想在代码中引用蓝图,则可以使用构造函数帮助器或类查找器。

static ConstructorHelpers::FObjectFinder<UBlueprint> onlineSpawnBP(TEXT("Blueprint'/Game/FirstPersonCPP/Blueprints/OnlineSpawn'")); //replace with the right path to your blueprint in your project
     if (onlineSpawnBP)
     {
         AOnlinePawn* NewActor = GetWorld()->SpawnActor<AOnlinePawn>(GetClass(),  &position, &rotator); 
     }

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

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