Course contentsShow
← Unreal Engine Game Development
Lesson 156 of 8802. The Gameplay FrameworkFree lesson

Handling Global State Changes Across Level Loads

You will develop robust event-driven systems that update the GameInstance and UI components immediately following asynchronous level loading processes.

In this lesson, you will learn how to bridge the gap between persistent data in your GameInstance and the reactive needs of your UI immediately after a level transition.

The Challenge of the Transition

When you load a new level, the world is destroyed and rebuilt. While the GameInstance survives this process, your UI widgets and active actors do not. If you have global state—like a player’s current currency or quest progress—you need a way to tell the new, freshly spawned HUD that the world has changed, or to update the GameInstance as soon as the level settles.

The Event-Driven Solution

Rather than having your UI poll the GameInstance every frame (which is inefficient), you should treat the initialization of the new level as an event. The best place to trigger this is in the PlayerController or the level's GameMode using the BeginPlay sequence.

By defining a Multicast Delegate inside your GameInstance, you create a global broadcast channel that survives the transition. When the new level begins, the PlayerController can "listen" for these changes or notify the GameInstance that it is ready to receive data.

Consider this setup in your PlayerController:

void AMyPlayerController::BeginPlay()
{
    Super::BeginPlay();

    // The HUD initializes and asks the GameInstance for the latest data
    UMyGameInstance* GI = Cast<UMyGameInstance>(GetGameInstance());
    if (GI)
    {
        // Broadcast that we are ready to receive state updates
        GI->OnLevelInitialized.Broadcast(GI->CurrentPlayerStats);
    }
}

Decoupling Logic

By using this event-driven approach, the GameInstance doesn’t need to know which specific widget is currently active. It simply broadcasts the state. Any UI component that cares about that data simply binds to that delegate during its own Construct phase. This ensures that even though the world was destroyed, your UI re-syncs with the "source of truth" in the GameInstance the millisecond the level becomes playable. This pattern prevents "stale data" bugs where your HUD displays default values before snapping to the correct state.

Key Takeaway: Always use an event-driven broadcast from the level's startup logic to sync your persistent GameInstance data with newly instantiated UI components.