Load world state on server start
This commit is contained in:
@@ -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())
|
||||
|
||||
@@ -26,7 +26,12 @@ public:
|
||||
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Agrarian|Persistence", meta = (ClampMin = "0"))
|
||||
float ServerAutoSaveIntervalSeconds = 300.0f;
|
||||
|
||||
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Agrarian|Persistence")
|
||||
bool bLoadWorldOnServerStart = true;
|
||||
|
||||
protected:
|
||||
void RegisterPersistentActorClasses(UAgrarianPersistenceSubsystem* Persistence) const;
|
||||
void LoadWorldOnServerStart();
|
||||
void RunServerAutoSave();
|
||||
|
||||
FTimerHandle ServerAutoSaveTimerHandle;
|
||||
|
||||
Reference in New Issue
Block a user