C++ check is game in HMD (VR) mode

How do I check, in C++ code, whether the game is in VR mode?

I tried the following but it doesn’t work:

IHeadMountedDisplay *pHmd = nullptr;
if (GEngine) {
	pHmd = GEngine->XRSystem->GetHMDDevice();
}
if (pHmd->IsHMDEnabled()) {
	// we are in VR mode?
}

The above code thinks we’re in VR even when we are not.

To answer my own question, the following seems to work for 4.19 in editor mode (VR Preview) vs (New Editor Window):

IHeadMountedDisplay *pHmd = nullptr;
TSharedPtr<IStereoRendering, ESPMode::ThreadSafe> pStereo = nullptr;
if (GEngine) {
	pHmd = GEngine->XRSystem->GetHMDDevice();
	pStereo = GEngine->XRSystem->GetStereoRenderingDevice();
}
if (pHmd->IsHMDEnabled() && pHmd->IsHMDConnected() && pStereo->IsStereoEnabled()) {
	// in VR mode
}

Though now I wonder if this is the correct/official way to do it?

From source code of node IsHeadMountedDisplayConnected

return GEngine->XRSystem.IsValid() && GEngine->XRSystem->GetHMDDevice() && GEngine->XRSystem->GetHMDDevice()->IsHMDConnected();

So your code is correct

Note this is only sufficient to check if the HMD is connected and ready, not to tell if the game is running in VR. You need IsHMDEnabled() and/or IsStereoEnabled() for that .