How to properly detect if VR is enabled in PIE?

I have code right now in C++ that detects if the VR HMD is available and if it is, and my game is configured to be in VR mode, it’ll enable the VR Codepath. Otherwise, it’ll act like a traditional FPS.

void ARDBasePlayerController::RefreshVr_Implementation()
{
	const bool bPreviousVrHeadsetEnabledNow = bVrHeadsetEnabledNow;
	const TSet<FName> PreviousVrMotionControllerEnabledNow = VrMotionControllerEnabledNow;
	
    //Try detecting the VR headset
    if (bVrHeadsetEnabled && GEngine->XRSystem.IsValid())
    {
        GEngine->XRSystem->GetHMDDevice()->EnableHMD(true);
        bVrHeadsetEnabledNow = GEngine->XRSystem->GetHMDDevice()->IsHMDEnabled();
    }
    else
    {
        bVrHeadsetEnabledNow = false;
    }

However, in the Unreal Editor, if I play using the normal PIE mode without VR preview enabled, my code detects that HMD is available and sets itself up for VR, though I meant to play in non-VR mode. Is there another layer of detecting that in PIE I pressed the preview game mode without VR needing to be enabled?

Right now I always have to hardcode some C++ to either set bVrHeadsetEnabledNow to true or false depending on what mode I want to test in.

Normally bVrHeadsetEnabled is a Config UPROPERTY that players would set on/off in the options menu so the game can be in VR or not VR. But in PIE, if I play without VR Preview on but bVrHeadsetEnabled is true, my game is in a VR state, but not really. And I want to detect that I launched PIE without VR preview on so my game stays in non-vr mode correctly. I’d be OK with adding some precompiler macros too if needed, like #if EDITOR.

BTW, I even noticed that the OpenXR implementation is blank and always returns true:

void FOpenXRHMD::EnableHMD(bool enable)
{
}
bool FOpenXRHMD::IsHMDEnabled() const
{
	return true;
}

This was found by using “Step Into” when debugging so I know for sure these are the actual implementations Unreal Engine 5 is using when I use the OpenXR plugin. So no matter what, it looks like Unreal 5 always thinks it’s in VR mode whether any devices are attached or not.

You can check for if you’re playing in VR Preview using this snippet

if(GEditor && GEditor->IsVRPreviewActive())
{

}