Download

Possess instead of Default Spawn

How do I stop the default spawn of a character by the game mode and instead possess a pawn of my choosing? I made my own AGameModeBase and I’ve set DefaultPawnClass = nullptr in the constructor. That puts the camera at (0, 0, 0), but when I Possess a Pawn with an APlayerController I’ve made, it doesn’t seem to do anything? This is the same APlayerController that works as expected when the game mode is what instantiates it. What gives?

It works if I possess like this:

UGameplayStatics::GetPlayerController(world, 0)->Possess(Cast<APawn>(actor));

It does not work if I possess like this:

AController* controller = world->SpawnActor<AController>(AMyPlayerController::StaticClass(), actor->GetActorLocation(), actor->GetActorRotation(), SpawnInfo);
controller->Possess(Cast<APawn>(actor));

So I have to assume the issue is that the World maintains a list of player controllers, and simply spawning one isn’t enough to tell it to be active.

… you’re spawning a controller, but the player is already controlling a controller at this point. You need to possess the pawn using the controller they already have.

I feel like you’re confusing the pawn with the controller here, you want to spawn a pawn and possess it with the controller, not the other way around.

Also, in general, chaining things like that can often introduce lots of crashes – you should check that your Spawn was successful before calling a function on the returned item.