How to possess a pawn after spawning from spawnvolume

Hi,

I am new to the Engine and could not find resources / tutorials on the following:

I created a spawn volume (similar to the C++ Battery Collector video tutorial) that spawns an instance of my own pawn subclass. Now I want to “possess” that pawn and redirect input to it using either the default player controller that is instantiated on startup or my own one if necessary. How would I do this in C++? What are some best practices to implement this regarding where (in which class) to put this code? I currently have basically three classes: the pawn subclass, a spawnvolume class and the game mode class. Somehow the game mode should be notified when a pawn is spawned by the spawnvolume and then let the current controller posess that pawn, right?

I would think that the logic could work like this:

  • The SpawnVolume spawns the Pawn
  • The SpawnVolume notifies notify the
    GameMode that it has spawned a Pawn
  • The GameMode finds the local
    PlayerController and instructs it to
    possess the Pawn

E.g.

SpawnVolume:

// You'll have to find some entry point after spawning the pawn, I'm just making up this function
void AYourSpawnVolume::SpawnPawn()
{
	// Spawn pawn
	APawn* pawn = GetWorld()->SpawnActor<AYourPawn>(AYourPawn::StaticClass());

	// Notify GameMode we've spawned the pawn
	AYourGameMode* gameMode = Cast<AYourGameMode>(world->GetAuthGameMode());
	gameMode->NotifyPawnSpawned(pawn);
	
}

GameMode:

void AYourGameMode::NotifyPawnSpawned(AYourPawn* pawn)
{
	// Get local controller
	APlayerController* pc = UGameplayStatics::GetPlayerController()
	
	// Possess
	pc->Possess(pawn);	
}

Thank you very much for pointing me in the right direction. I did not know where to look for methods like UGameplayStatistics::GetPlayerController() or world->GetAuthGameMode(). Helped me a lot!