open a widget via c++ code example

Example: ue4 c++ how to open a blueprint widget

// Where menu might be your widget.
    Menu->AddToViewport();

    // Step 0 get player controller.
    auto PlayerController = GetFirstLocalPlayerController();
    if (!ensure(PlayerController != nullptr)) return;

    // Step 1 setup an input mode. There are multiple such as game only or game and UI as well.
    FInputModeUIOnly InputModeData;
    // Step 2 config is specific to the type
    InputModeData.SetLockMouseToViewport(false);
    InputModeData.SetWidgetToFocus(Menu->TakeWidget()); //Because UMG wraps Slate

    // Step 3 set the mode for the player controller
    PlayerController->SetInputMode(InputModeData);

    // Step 4 enable cursor so you know what to click on:
    PlayerController->bShowMouseCursor = true;

Tags:

Cpp Example