73 lines
2.2 KiB
C++
73 lines
2.2 KiB
C++
// Copyright Epic Games, Inc. All Rights Reserved.
|
|
|
|
#include "AgrarianGameGameMode.h"
|
|
#include "AgrarianGameCharacter.h"
|
|
#include "AgrarianDebugHUD.h"
|
|
#include "AgrarianGameState.h"
|
|
#include "AgrarianPersistenceSubsystem.h"
|
|
#include "TimerManager.h"
|
|
|
|
AAgrarianGameGameMode::AAgrarianGameGameMode()
|
|
{
|
|
GameStateClass = AAgrarianGameState::StaticClass();
|
|
HUDClass = AAgrarianDebugHUD::StaticClass();
|
|
}
|
|
|
|
void AAgrarianGameGameMode::BeginPlay()
|
|
{
|
|
Super::BeginPlay();
|
|
|
|
if (HasAuthority() && ServerAutoSaveIntervalSeconds > 0.0f)
|
|
{
|
|
GetWorldTimerManager().SetTimer(
|
|
ServerAutoSaveTimerHandle,
|
|
this,
|
|
&AAgrarianGameGameMode::RunServerAutoSave,
|
|
ServerAutoSaveIntervalSeconds,
|
|
true,
|
|
ServerAutoSaveIntervalSeconds);
|
|
}
|
|
}
|
|
|
|
void AAgrarianGameGameMode::RestartPlayer(AController* NewPlayer)
|
|
{
|
|
Super::RestartPlayer(NewPlayer);
|
|
|
|
AAgrarianGameCharacter* AgrarianCharacter = NewPlayer ? Cast<AAgrarianGameCharacter>(NewPlayer->GetPawn()) : nullptr;
|
|
UAgrarianPersistenceSubsystem* Persistence = GetGameInstance() ? GetGameInstance()->GetSubsystem<UAgrarianPersistenceSubsystem>() : nullptr;
|
|
if (AgrarianCharacter && Persistence && Persistence->RestorePlayerSnapshot(AgrarianCharacter))
|
|
{
|
|
UE_LOG(LogTemp, Log, TEXT("Agrarian restored reconnect snapshot for %s."), *AgrarianCharacter->GetName());
|
|
}
|
|
}
|
|
|
|
void AAgrarianGameGameMode::Logout(AController* Exiting)
|
|
{
|
|
AAgrarianGameCharacter* AgrarianCharacter = Exiting ? Cast<AAgrarianGameCharacter>(Exiting->GetPawn()) : nullptr;
|
|
UAgrarianPersistenceSubsystem* Persistence = GetGameInstance() ? GetGameInstance()->GetSubsystem<UAgrarianPersistenceSubsystem>() : nullptr;
|
|
if (AgrarianCharacter && Persistence)
|
|
{
|
|
Persistence->SavePlayerSnapshot(AgrarianCharacter);
|
|
}
|
|
|
|
Super::Logout(Exiting);
|
|
}
|
|
|
|
void AAgrarianGameGameMode::RunServerAutoSave()
|
|
{
|
|
if (!HasAuthority())
|
|
{
|
|
return;
|
|
}
|
|
|
|
UAgrarianPersistenceSubsystem* Persistence = GetGameInstance() ? GetGameInstance()->GetSubsystem<UAgrarianPersistenceSubsystem>() : nullptr;
|
|
if (!Persistence)
|
|
{
|
|
UE_LOG(LogTemp, Warning, TEXT("Agrarian server autosave skipped: persistence subsystem unavailable."));
|
|
return;
|
|
}
|
|
|
|
const bool bSaved = Persistence->SaveCurrentWorld();
|
|
UE_LOG(LogTemp, Log, TEXT("Agrarian server autosave %s."), bSaved ? TEXT("completed") : TEXT("failed"));
|
|
}
|