World is Null

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:

Try using a WorldContext argument

UFUNCTION(BlueprintCallable, Meta=(WorldContext="WorldContext"))
bool PlayerSaveGame(UObject* WorldContext, FString SaveSlotName, int32 UserIndex);

bool UCG_PlayerSaveGame::PlayerSaveGame(UObject* WorldContext, FString SaveSlotName, int32 UserIndex)
{
    if (UWorld* World = GEngine->GetWorldFromContextObject(WorldContext, EGetWorldErrorMode::LogAndReturnNull))
    {
        //...
    }
}

I appreciate the input. Unfortunately it still returns that that world is null.

In completely shocking news, I removed the function entirely (not just commented out) and figured I would start from scratch. In some cached way, the unreal engine is still “seeing” the function and trying to run it. This leads me to believe, even if I found the solution, it would not have displayed/updated because somewhere/somehow unreal engine 5.1 is keeping a phantom function. I was tipped off because the log was still showing it was ‘World is null’ after I removed that check from the function entirely as I tried to get a game reference instead.

I ended up clearing it all and building from scratch. I am not sure what happened between Visual Studio and Unreal, but the issue I had no longer exists.