I am really pulling my hair out here. I don’t understand why my world is coming up as null. Visually, I am in the world, can see everything, my other functions are working but this one is failing. Please let me know what I have missed. I did put a delay between the save and actually exiting just in case that was the issue. No differences.
Here is my PlayerSaveGame function:
bool UCG_PlayerSaveGame::PlayerSaveGame(FString SaveSlotName, int32 UserIndex)
{
UWorld* World = GEngine->GetWorldContexts()[0].World();
if (!World)
{
UE_LOG(LogTemp, Error, TEXT("World is null"));
return false;
}
// Get a reference to the game mode
ACG_v002GameMode* GameMode = Cast<ACG_v002GameMode>(World->GetAuthGameMode());
if (!GameMode)
{
UE_LOG(LogTemp, Error, TEXT("ACG_v002GameMode is invalid or not properly initialized."));
return false;
}
// Create a new instance of your save game class
UCG_PlayerSaveGame* SaveGameInstance = Cast<UCG_PlayerSaveGame>(UGameplayStatics::CreateSaveGameObject(UCG_PlayerSaveGame::StaticClass()));
// Set the member variables on save game instance
SaveGameInstance->Players = Players;
SaveGameInstance->Commanders = Commanders;
SaveGameInstance->Planets = Planets;
SaveGameInstance->Cities = Cities;
SaveGameInstance->Improvements = Improvements;
SaveGameInstance->Captains = Captains;
SaveGameInstance->Missions = Missions;
SaveGameInstance->UniverseDate = UniverseDate; // Set the UniverseDate member variable
// Save the game to disk
bool bSuccess = UGameplayStatics::SaveGameToSlot(SaveGameInstance, SaveSlotName, UserIndex);
if (bSuccess)
{
UE_LOG(LogTemp, Display, TEXT("Game saved to slot: %s"), *SaveSlotName);
GameMode->CurrentSaveSlotName = SaveSlotName;
UE_LOG(LogTemp, Display, TEXT("Saved game slot name: %s"), *GameMode->CurrentSaveSlotName);
}
else
{
UE_LOG(LogTemp, Warning, TEXT("Failed to save game to slot: %s"), *SaveSlotName);
}
return bSuccess;
}
Here is me calling it is being used in my UMG Widget Blueprint: