How to Override AGameMode::PreLogin?

Hi,

I’ve created a new C++ class in my project based on the default AGameMode.

I’d like to override the default PreLogin event so I could use it to check if a connecting player is in my ban list and deny their connection.

The trouble is, I’m new to C++ and I have no idea how to do it. :smiling_face:

I wrote this in the .cpp file, but it doesn’t work. Do I also have to write something in the .h file?

void MyGameMode::PreLogin(const FString& Options, const FString& Address, const FUniqueNetIdRepl& UniqueId, FString& ErrorMessage)
 {
	UE_LOG(LogTemp, Display, TEXT("Hi, I work!"))
 }

Thanks :wink:

You need to add Super::PreLogin first, it won’t ApproveLogin without Super.

So, to override Event PreLogin, you have to do this:

In MyGameMode.h add:

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/GameMode.h"
#include "MyGameMode.generated.h"

UCLASS()
class MyProjectName_API AMyGameMode : public AGameMode
{
	GENERATED_BODY()

public:

	AMyGameMode();
	virtual ~AMyGameMode() = default;

	virtual void PreLogin(const FString& Options, const FString& Address, const FUniqueNetIdRepl& UniqueId, FString& ErrorMessage) override;
};

In MyGameMode.cpp add:

#include "MyGameMode.h"

AMyGameMode::AMyGameMode()

void AMyGameMode::PreLogin(const FString& Options, const FString& Address, const FUniqueNetIdRepl& UniqueId, FString& ErrorMessage)
{

// Example code

// Convert the player's Unique Net ID (UniqueId) to a string. 
FString UniqueIDString = UniqueId.ToString();

//If UniqueIDString (FUniqueNetIdRepl& UniqueId) equals "001"
if (UniqueIDString == "001")
{
	//Let the player join
	ErrorMessage = "";
}
else
{
	// Don't let player join
	ErrorMessage = "Error joining";
}
	FGameModeEvents::GameModePreLoginEvent.Broadcast(this, UniqueId, ErrorMessage);
}

Replace MyprojectName with your project name, MyGameMode, with your game mode name, and AMygameModeName with your game mode name, and leave the A in front of it.

If ErrorMessage = “”; is returned as not empty, the player will not be able to join.