Basic Server Multiplayer Functions (Kick/Ban/Etc)

Hi all, in first i would say that i’m not using Sessions.

So, what i already know is that some of the server code can be implemented in the GameMode class. But when i need to kick or ban player from server, how i can do that?

Thanks

There are already Methods for Banning/Kicking Players :wink:

Old thread, but this still ranks high on google searches. That page is great and all, looks like some fine functions. But there is little in the way of instruction on how to get your game session and cast to it so that you can use those functions.

The GameSession is available from the GameMode.

I’m sorry, but that’s a really simplistic and not terribly helpful answer. Game session isn’t gotten and casted to the same way many of the other core parts like game state, game mode, etc are. There isn’t much information on this, but I managed to work it out last night. This can only be accessed in a meaningful manner via C++. While you can “get game session class” in blueprints you can’t do anything with it.

You need to do this to cast to the game session properly so that you can use it.


CurrentGameSessionRef = GetGameSessionClass();
CurrentGameSession = Cast<AGameSession>(CurrentGameSessionRef->GetDefaultObject());


if you want to kick, especially via blueprints create a header like this:


UFUNCTION(BlueprintCallable, meta = (DisplayName = "KickPlayer"))
bool BP_Kickplayer(APlayerController * KickedPlayer, const FText & KickReason);

with the following code in CPP:


 if (CurrentGameSession)
{
return CurrentGameSession->KickPlayer(KickedPlayer, KickReason);
}
else
{
return false;
}

Be aware that using ban player currently is pointless since all ban player does is call kick directly. Despite the description on the unreal documents ban player does not in any way ban the player from the server permanently. You need you to create a way to reliably identify the player and check if they’re banned during pre-login on the game mode to prevent them from rejoining the server.

2 Likes

…however the GameSession is available from the GameMode like suggested.


GetWorld()->GetAuthGameMode()->GameSession

And my question was:

Not about where it was but how to get it and cast to it, which is different from every other core piece like the game mode, game instance, game state, etc. Simply stating “You can get it from the game mode” is not a helpful answer to that question.

This is how you get it and you don’t have to Cast it to call KickPlayer since it is already a GameSession Type.

That’s why I was asking for specifics, that answer doesn’t come up on google searches for “get game session” and the only results I was getting were threads like this with vague references and no specifics. Thank you for that, that is a lot easier than my solution.

You won’t find a lot of decent multiplayer tutorials on the web at large unfortunately, a few of us have even complained in the past that Epic’s official MP tutorials don’t cover the best practices. It’s a complicated topic and the best documentation is the source code itself, along with Epic’s ShooterGame example project - which is extremely comprehensive.

The GameSession is just an actor spawned by the Server - I figured my comment would be enough for you to go looking and find what you need. As has been said you can get the spawned GameSession from there. You will most likely need to subclass it however and add your own code, because the default engine implementation is pretty barebones at best. Again, ShooterGame is the place to look for info.

IIRC there is no “Ban” functionality implemented by default, it just routes straight to kick. That’s something you have to add yourself since the concept of banning is usually unique and bespoke for every game.

Thanks, it worked.

My Final setup:

.h:

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "KickPlayer.generated.h"

/**
 * 
 */
UCLASS()
class MYPROJECT_API UKickPlayer : public UBlueprintFunctionLibrary
{
	GENERATED_BODY()

		UFUNCTION(BlueprintCallable, Category = "KickPlayer")
			static bool Kick(APlayerController* KickedPlayer, const FText & KickReason, AGameModeBase* CurrentGameMode);
};

.cpp

// Fill out your copyright notice in the Description page of Project Settings.

#include "KickPlayer.h"
#include "GameFramework/GameSession.h"
#include "GameFramework/GameModeBase.h"
#include "Runtime/Engine/Public/TimerManager.h"
#include "Kismet/GameplayStatics.h"

bool UKickPlayer::Kick(APlayerController* KickedPlayer, const FText & KickReason, AGameModeBase* CurrentGameMode)
{
	AGameSession *Sessao=CurrentGameMode->GetWorld()->GetAuthGameMode()->GameSession;

	if (Sessao)
	{
		return Sessao->KickPlayer(KickedPlayer, KickReason);
	}
	else
	{
		return false;
	}
}
1 Like