Initialize character with settings in multiplayer

Hello everyone,

I’ve been reading up on this subject for a while and maybe I’m just asking the wrong questions, and the answer is something very simple.

What I want to achieve is a simple game where people can join a session with their own character setup and choice. So when joining a server I want them to be able to say join team Empire using my character custom skin color and hair style.
But I hit a brick wall in that I simply cannot send these choices to the server at any point.
In OnJoinSessionComplete(), before ClientTravel() the connection is not up yet, RPCs are still run by the local machine, and during ClientTravel() the server takes everything over and everything is reset to defaults (as I understand seamless travel is only possible once you travelled to the server once).

So the question is this: How do I communicate my character setup to the server/host so I am joining the correct team, and my custom character is preserved?

Thanks for your answers in advance!

Don’t know if it’s the cleanest method but I did it by storing the data I need on the GameInstance, and recovering it then via an RPC call to the client.
As long as the data I want is not returned, I hold off on the spawning of the character, and the RPC response triggers the rest. (as a fallback if too long time elapses, I initialize the client with fallback settings).

Store data during Joining:

void AMainMenuPlayerController::JoinSession(int32 SessionIndex, ERoles PlayerRole)
{
	FPlayerData PlayerData = FPlayerData(PlayerRole, EGenders::Male);
	Cast<UCustomGameInstance>(GetGameInstance())->SetStoredPlayerData(PlayerData);
	if (!SessionInterface.IsValid() || SessionIndex >= FoundSessions.Num())
	{
		return;
	}
	...
}

Recover data:

void ALobbyPlayerController::Client_RecoverPlayerData_Implementation()
{
	if (UCustomGameInstance* GameInstance = GetGameInstance<UCustomGameInstance>())
	{
		FPlayerData RetrievedData = GameInstance->GetStoredPlayerData();
		Server_SetPlayerData(RetrievedData.Role, RetrievedData.Gender);
	}
}

Store data and spawn player:

void ALobbyPlayerController::Server_SetPlayerData_Implementation(ERoles PlayerRole, EGenders Gender)
{
	if (ACustomPlayerStateBase* PlayerStateReference = GetPlayerState<ACustomPlayerStateBase>())
	{
		PlayerStateReference->SetPlayerData(FPlayerData(PlayerRole, Gender));
		UGameplayStatics::GetGameMode(GetWorld())->RestartPlayer(this);
	}
}

Like I said, not sure it is the cleanest approach, but gets the job done. Hopefully it helps someone.

Hey @JadenKorr !

Yes that’s actually works great!
You may also wants to check the subsystem if you want to encapsule more that code.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.