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. The problem is that I have a OnlinePawn Blueprint that inherits from a C++ base class AOnlinePawn. 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.
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. 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);
}