Hello everybody,
I’m coding a game mode for a game, and I have a small issue when overriding PreLogin.
Here is my code :
void ASurvivalGameMode::PreLogin(const FString& Options, const FString& Address, const FUniqueNetIdRepl& UniqueId, FString& ErrorMessage) {
//Super::PreLogin(Options, Address, UniqueId, ErrorMessage); Might not be useful, will try that out later
if (CurrentPlayers >= (PlayersPerTeam*NumberOfTeams)) {
ErrorMessage = "Server is already full. Please try again later.";
}
if (IsGameStarted) {
ErrorMessage = "Game has already begun. Please try again later.";
}
Super::PreLogin(Options, Address, UniqueId, ErrorMessage);
}
The function is actually called, no issue about that. The ErrorMessage is set. But it doesn’t prompt the client to disconnect or anything of the sort. Is there something I didn’t understand about this in the docs ?
Thanks in advance for your help
Well, I managed to figure out why it didn’t work, so I’m going to post this here. The documentation is actually partially inaccurate about the possibility of doing what I thought was possible. Here is the code for the same function in. AGameModeBase :
void AGameModeBase::PreLogin(const FString& Options, const FString& Address, const FUniqueNetIdRepl& UniqueId, FString& ErrorMessage)
{
// Login unique id must match server expected unique id type OR No unique id could mean game doesn't use them
const bool bUniqueIdCheckOk = (!UniqueId.IsValid() || (UniqueId.GetType() == UOnlineEngineInterface::Get()->GetDefaultOnlineSubsystemName()));
if (bUniqueIdCheckOk)
{
ErrorMessage = GameSession->ApproveLogin(Options);
}
else
{
ErrorMessage = TEXT("incompatible_unique_net_id");
}
FGameModeEvents::GameModePreLoginEvent.Broadcast(this, UniqueId, ErrorMessage);
}
A few things about this code : It doesn’t seem like just modifying the string works, as the function is calling an event after it’s done modifying the string, which isn’t mentioned anywhere in the doc. So, if you want to override it, you have to call FGameModeEvents::GameModePreLoginEvent.Broadcast(this, UniqueId, ErrorMessage); at the end of your method in order for it to work. And if you’re naive like me and call the super function, it’s not gonna work either because GameSession->ApproveLogin typically returns a null string if it succeeds, erasing your previous modification.