Load world state on server start

This commit is contained in:
2026-05-18 19:32:25 -07:00
parent a7ca8d10f8
commit 9aa2f8bee3
5 changed files with 114 additions and 1 deletions
@@ -3,8 +3,10 @@
#include "AgrarianGameGameMode.h"
#include "AgrarianGameCharacter.h"
#include "AgrarianDebugHUD.h"
#include "AgrarianCampfire.h"
#include "AgrarianGameState.h"
#include "AgrarianPersistenceSubsystem.h"
#include "AgrarianShelterActor.h"
#include "TimerManager.h"
AAgrarianGameGameMode::AAgrarianGameGameMode()
@@ -17,6 +19,11 @@ void AAgrarianGameGameMode::BeginPlay()
{
Super::BeginPlay();
if (HasAuthority())
{
LoadWorldOnServerStart();
}
if (HasAuthority() && ServerAutoSaveIntervalSeconds > 0.0f)
{
GetWorldTimerManager().SetTimer(
@@ -53,6 +60,51 @@ void AAgrarianGameGameMode::Logout(AController* Exiting)
Super::Logout(Exiting);
}
void AAgrarianGameGameMode::RegisterPersistentActorClasses(UAgrarianPersistenceSubsystem* Persistence) const
{
if (!Persistence)
{
return;
}
Persistence->RegisterWorldActorClass(TEXT("primitive_shelter"), AAgrarianShelterActor::StaticClass());
Persistence->RegisterWorldActorClass(TEXT("campfire"), AAgrarianCampfire::StaticClass());
}
void AAgrarianGameGameMode::LoadWorldOnServerStart()
{
if (!HasAuthority() || !bLoadWorldOnServerStart)
{
return;
}
UAgrarianPersistenceSubsystem* Persistence = GetGameInstance() ? GetGameInstance()->GetSubsystem<UAgrarianPersistenceSubsystem>() : nullptr;
if (!Persistence)
{
UE_LOG(LogTemp, Warning, TEXT("Agrarian startup load skipped: persistence subsystem unavailable."));
return;
}
RegisterPersistentActorClasses(Persistence);
if (!Persistence->DoesSaveExist())
{
UE_LOG(LogTemp, Log, TEXT("Agrarian startup load skipped: no save exists."));
return;
}
int32 RestoredPlayerCount = 0;
int32 RestoredActorCount = 0;
constexpr bool bClearExistingActors = false;
const bool bLoaded = Persistence->LoadCurrentWorld(RestoredPlayerCount, RestoredActorCount, bClearExistingActors);
UE_LOG(
LogTemp,
Log,
TEXT("Agrarian startup load %s. Restored players: %d. Restored actors: %d."),
bLoaded ? TEXT("completed") : TEXT("completed with world-state warning"),
RestoredPlayerCount,
RestoredActorCount);
}
void AAgrarianGameGameMode::RunServerAutoSave()
{
if (!HasAuthority())