What is the correct procedure for multiplayer spawning and possessing pawns / huds in c++?

So the game i am working on is multiplayer and has two teams, each with a different character and hud, which are assigned to the clients while they are on the respective teams. at what point after the client joins the server are the pawns spawned and the player controller told to take ownership of the spawned actor? i have overridden AGameMode::ChooseTeam(args) to assign the teams in the player states and tested it, so i know the teams are assigned correctly, but now i’m not sure where in the code i need to assign the hud class, spawn the correct pawn/character and tell the player’s playercontroller to take ownership based on which team they were assigned to. i assume i will be overriding one of the superclass functions to do this, but i’m not clear on the order of when they are executed after the client joins the server. what functions spawn the GameMode’s defaultpawnclass and defaulthudclass?

any help would be greatly appreciated.

Why don’t you check in engine source code yourself? :slight_smile:

https://github.com/EpicGames/UnrealEngine/blob/35d8b371a10392565bfc68ddb6f9b52aa3ba4bbf/Engine/Source/Runtime/Engine/Private/GameMode.cpp

just remember to tie your github account with Unreal accoun, in Unreal profile page so you gain access to source

Quick search with Ctrl+F suggest that DefaultPawnClass spawn is handled in

void AGameMode::SpawnDefaultPawnFor(AController* NewPlayer, AActor* StartSpot)

and assigning hud in

void AGameMode::StartNewPlayer(APlayerController* NewPlayer)
void AGameMode::HandleSeamlessTravelPlayer(AController*& C)

oh yeah! i forgot we can get into the source now, that will be a LOT more helpful than trying to figure out this stuff only through the headers. thank you for providing the function names anyway, that will save me some searching time and provide a good start point for what i need to do <3

If anyone comes across this question, I ended up solving my problem by making a base class for my game HUD that is a subclass of AHUD and handles all of the common ground between the two types of pawns i am using (such as the in-game menu/chat) and then reparented both of the other huds to my gameHUD. since “StartNewPlayer” is called after i have assigned the teams, i use that to assign the respective huds based on the teams as follows.

//set appropriate hud initial
void AMYGameMode::StartNewPlayer(APlayerController* NewPlayer)
{
	Super::StartNewPlayer(NewPlayer);

	AMYPlayerController* PCOwner = Cast<AMYPlayerController>(NewPlayer);
	if (PCOwner)
	{
		AMYPlayerState* PState = Cast<AMYPlayerState>(PCOwner->PlayerState);
		if (PState)
		{
			if (PState->GetTeamNum() == 1)// set team 1 hud
			{
				NewPlayer->ClientSetHUD(Team1Hud);
			}
			else if (PState->GetTeamNum() == 2)//set team 2 hud
			{
				NewPlayer->ClientSetHUD(Team2Hud);
			}
		}
	}
}

I also overrode the RestartPlayer function, as my game mode is match based and it appears that StartNewPlayer is only called upon joining the server in PostLogin.

/**
 * Override for resetting HUDs
*/
void AMYGameMode::RestartPlayer(AController* NewPlayer)
{
	Super::RestartPlayer(NewPlayer);

	AMYPlayerController* PCOwner = Cast<AMYPlayerController>(NewPlayer);
	if (PCOwner)
	{
		AMYPlayerState* PState = Cast<AMYPlayerState>(PCOwner->PlayerState);
		if (PState)
		{
			if (PState->GetTeamNum() == 1)// set team 1 hud
			{
				PCOwner->ClientSetHUD(Team1Hud);
			}
			else if (PState->GetTeamNum() == 2)//set team 2 hud
			{
				PCOwner->ClientSetHUD(Team2Hud);
			}
		}
	}


}

where Team1Hud and Team2Hud are AMYGameMode class variables initialized in the constructor to Team1HudClass::StaticClass() and Team2HudClass::StaticClass() respectively. Hope this helps somebody.