Give to choose the monitor in the settings menu?

Hi, I want to give to choose the monitor in the settings menu, but I am not able to find any tutorial that talks about how to set this. I imagine that first the game has to detect how many monitors there are and then give to choose in which one the user wants to play the game.

Best regards and thank you very much.

This is the answer from GPT; I wouldn’t know how to implement this or if it really works, could someone confirm this?:

Creating a settings menu that allows players to select their preferred monitor is a great idea for improving player flexibility, especially in a multi-monitor setup. Unreal Engine 5 doesn’t directly expose monitor selection as a straightforward setting, but with some Blueprint scripting and C++ functionality, you can implement this feature. Here’s a step-by-step guide to accomplish this:

Steps to Implement Monitor Selection in Unreal Engine 5:

1. Detect Available Monitors (C++ Needed)

Unreal Engine’s Blueprint system alone doesn’t currently support detailed access to monitor information (like detecting multiple monitors and changing the display). For this, you’ll need to write a custom C++ function to detect available monitors. Here’s how you can do it:

  • In your project’s source code, add a new C++ function to detect monitors.
  • Use platform-specific API calls to get the list of monitors:
    • Windows: Use the EnumDisplayMonitors function from Windows.h.
    • Linux & MacOS: You may need to use specific libraries (such as SDL or GLFW) to query monitor information.

Sample C++ Code (for Windows)

Here’s an example of a basic C++ function to enumerate monitors on Windows:

cpp

Copiar código

#include "YourProjectName.h"
#include "Windows/AllowWindowsPlatformTypes.h"
#include "Windows/WindowsHWrapper.h"
#include "Windows/HideWindowsPlatformTypes.h"

// Struct to store monitor info
struct MonitorInfo {
    FString Name;
    int Width;
    int Height;
};

TArray<MonitorInfo> GetMonitors() {
    TArray<MonitorInfo> MonitorList;
    
    EnumDisplayMonitors(nullptr, nullptr, [](HMONITOR hMonitor, HDC, LPRECT, LPARAM pData) -> BOOL {
        MONITORINFOEX MonitorInfoEx;
        MonitorInfoEx.cbSize = sizeof(MonitorInfoEx);
        if (GetMonitorInfo(hMonitor, &MonitorInfoEx)) {
            MonitorInfo info;
            info.Name = FString(MonitorInfoEx.szDevice);
            info.Width = MonitorInfoEx.rcMonitor.right - MonitorInfoEx.rcMonitor.left;
            info.Height = MonitorInfoEx.rcMonitor.bottom - MonitorInfoEx.rcMonitor.top;
            reinterpret_cast<TArray<MonitorInfo>*>(pData)->Add(info);
        }
        return TRUE;
    }, reinterpret_cast<LPARAM>(&MonitorList));

    return MonitorList;
}
  • Explanation: This function uses EnumDisplayMonitors to gather information about each monitor. It stores each monitor’s name, width, and height, which you can later pass to the Unreal Engine UI.
  • Expose to Blueprints: Wrap this function in a UFUNCTION(BlueprintCallable) in a UBlueprintFunctionLibrary so it can be called directly in Blueprints. This way, you can list available monitors in the UI.

2. Expose Monitor List to UI (Blueprint)

  • In Unreal Engine, create a Blueprint UI Widget (e.g., WBP_SettingsMenu) where you’ll allow users to select the monitor.
  • Add a ComboBox or Dropdown UI element to list available monitors.
  • Blueprint Setup:
    • Call the custom C++ function from your Blueprint to populate the ComboBox with monitor names.
    • You may use an OnInitialized or Construct event in the Widget to call this function and populate the list as soon as the menu opens.

3. Set Selected Monitor

Once the player selects a monitor, you’ll need to switch the display to that monitor.

  • Set Desired Monitor: You can switch the display using the monitor name or index returned from your list.
  • For this, you can use Unreal’s UGameViewportClient::GetGameViewport() and custom platform-specific code to force the game to render on the chosen monitor. However, this part is complex and may require external libraries or more in-depth OS-level API handling.

4. Apply Monitor Setting and Restart Game (if needed)

  • Once the player selects the monitor, you might need to restart the game or reinitialize the viewport to apply the change. Here’s how:
    • Option 1: Set the game to restart or reload the level when a new monitor is chosen.
    • Option 2: Use the custom C++ code to dynamically reinitialize the game on the new monitor, though this approach is less common due to complexities.

Putting It All Together

  • Use the C++ function to retrieve available monitors and expose this to Blueprints.
  • Populate a ComboBox in the settings menu UI with the monitor names.
  • When the player selects a monitor, store this preference (in SaveGame data if persistent settings are desired).
  • Restart or apply settings as necessary to switch to the new monitor.

Additional Tips

  • Testing on Multiple Setups: Test this thoroughly, especially if deploying cross-platform, as monitor handling can vary.
  • Fallbacks: If the selected monitor is disconnected or unavailable, set the game to fallback to the primary monitor.