|
// Copyright Illia Komsa 2022. All rights reserved. |
|
#include "GameFramework/GameModeBase.h" |
|
#include "GameFramework/PlayerState.h" |
|
#include "Kismet/GameplayStatics.h" |
|
#include "GameLiftGameModeExample.generated.h" |
|
|
|
/** |
|
* An example PlayerSession class, that adds property to store PlayerSessionId |
|
*/ |
|
UCLASS(Abstract) |
|
class AGameLiftPlayerStateExample : public APlayerState |
|
{ |
|
GENERATED_BODY() |
|
public: |
|
#if WITH_GAMELIFT |
|
UPROPERTY() |
|
FString PlayerSessionId; |
|
#endif |
|
}; |
|
|
|
/** |
|
* An example GameMode class to show how you should integrate the GameLiftGameModeComponent to your GameMode |
|
*/ |
|
UCLASS(Abstract) |
|
class AGameLiftGameModeExample : public AGameModeBase |
|
{ |
|
GENERATED_BODY() |
|
public: |
|
AGameLiftGameModeExample(const FObjectInitializer& ObjectInitializer = FObjectInitializer::Get()) |
|
: AGameModeBase(ObjectInitializer) |
|
{ |
|
MyGameLiftComponent = ObjectInitializer.CreateDefaultSubobject<UGameLiftGameModeComponent>(this, TEXT("GameLiftComponent")); |
|
} |
|
|
|
virtual void PreLogin(const FString& Options, const FString& Address, const FUniqueNetIdRepl& UniqueId, FString& ErrorMessage) override; |
|
virtual void Logout(AController* Exiting); |
|
virtual FString InitNewPlayer(APlayerController* NewPlayerController, const FUniqueNetIdRepl& UniqueId, const FString& Options, const FString& Portal); |
|
private: |
|
UGameLiftGameModeComponent* MyGameLiftComponent; |
|
}; |
|
|
|
// Before player is logged in to server, we need to check whether they are present in the GameLift GameSession, otherwise fail login |
|
void AGameLiftGameModeExample::PreLogin(const FString& Options, const FString& Address, const FUniqueNetIdRepl& UniqueId, FString& ErrorMessage) |
|
{ |
|
Super::PreLogin(Options, Address, UniqueId, ErrorMessage); |
|
|
|
#if WITH_GAMELIFT |
|
const FString& PlayerSessionId = UGameplayStatics::ParseOption(Options, "PlayerSessionId"); |
|
const FString& PlayerId = UGameplayStatics::ParseOption(Options, "PlayerId"); |
|
|
|
MyGameLiftComponent->AcceptPlayerSessionIfAuthorized(PlayerSessionId, PlayerId, ErrorMessage); |
|
#endif |
|
} |
|
|
|
// Before player logs out, we need to remove them from GameLift GameSession, so they can join another. |
|
// We might not need it if a player disconnects before match end and can reconnect later |
|
void AGameLiftGameModeExample::Logout(AController* Exiting) |
|
{ |
|
#if WITH_GAMELIFT |
|
if (AGameLiftPlayerStateExample* PlayerState = Cast<AGameLiftPlayerStateExample>(NewPlayerController->PlayerState)) |
|
{ |
|
FString PlayerSessionId = PlayerState->PlayerSessionId; |
|
MyGameLiftComponent->RemovePlayerSession(PlayerSessionId); |
|
} |
|
#endif |
|
|
|
Super::Logout(Exiting); |
|
} |
|
|
|
// After successful login we should store player's PlayerSessionId in their PlayerState, so we can use it later for removing the PlayerSession |
|
FString AGameLiftGameModeExample::InitNewPlayer(APlayerController* NewPlayerController, const FUniqueNetIdRepl& UniqueId, const FString& Options, const FString& Portal) |
|
{ |
|
FString ErrorMessage = Super::InitNewPlayer(NewPlayerController, UniqueId, Options, Portal); |
|
|
|
#if WITH_GAMELIFT |
|
const FString& PlayerSessionId = UGameplayStatics::ParseOption(Options, "PlayerSessionId"); |
|
|
|
if (AGameLiftPlayerStateExample* PlayerState = Cast<AGameLiftPlayerStateExample>(NewPlayerController->PlayerState)) |
|
{ |
|
PlayerState->PlayerSessionId = *PlayerSessionId; |
|
} |
|
#endif |
|
|
|
return ErrorMessage; |
|
} |