Editor crash when using UHeadMountedDisplayFunctionLibrary in constructor

GEngine->HMDDevice hasn’t been initialized when the constructor is called. While the code could check for that and not crash, it wouldn’t be able to actually set the tracking origin as at that time the HMDDevice is never going to be valid.

Move the call to BeginPlay. At that point GEngine->HMDDevice is correctly initialized so if running on a HMD you will set the tracking origin correctly.

This bug also affected UE 4.16.

Reproduction:

  1. New Project > TestProject > C++ > First Person
  2. Open Visual Studio 2017.
  3. In the constructor for the character (TestProjectCharacter.cpp), add at the end:
    UHeadMountedDisplayFunctionLibrary::SetTrackingOrigin(EHMDTrackingOrigin::Floor);
  4. Build the project.
  5. Save the level.
  6. Restart the editor.

The editor will now always crash on load.

I used an Oculus Rift HMD for this.