So it’s probably just my inexperience, but every now and then I run into API or C++ (or both) issues that make me scratch my head. Like in this case where I’m trying to implement the most simple thing in C++ that took all but 10 secs in Blueprint.
What this intricate algorithm does is, well, reset the VR view. You put that helmet on, sit up straight and relaxed and reset the view to match your viewing position. Let’s implement this in C++ just for the heck of it. So let’s start by looking behind the curtain of that node.
Runtime\Engine\Classes\Kismet\HeadMountedDisplayFunctionLibrary.h
UFUNCTION(BlueprintCallable, Category="Input|HeadMountedDisplay")
static void ResetOrientationAndPosition(float Yaw = 0.f);
Runtime\Engine\Private\HeadMountedDisplayFunctionLibrary.cpp
void UHeadMountedDisplayFunctionLibrary::ResetOrientationAndPosition(float Yaw)
{
if (GEngine->HMDDevice.IsValid() && GEngine->IsStereoscopic3D())
{
GEngine->HMDDevice->ResetOrientationAndPosition(Yaw);
}
}
Alright, that seems straight forward. Of course that stuff doesn’t get exported so I can’t use that function outright? And also I probably shouldn’t because they were meant for Blueprints. That’s what the annoying guy in the back of my head says anyway. So let’s just use it in my Pawn like this:
void AIkoPawn::ResetVRView()
{
if (GEngine->HMDDevice.IsValid() && GEngine->IsStereoscopic3D())
{
GEngine->HMDDevice->ResetOrientationAndPosition(0.f);
}
}
Compiler: Ay, ay, ay, no me gusta!
Error 1 error C2027: use of undefined type 'IHeadMountedDisplay'
Error 2 error C2039: 'ResetOrientationAndPosition' : is not a member of 'TSharedPtr<IHeadMountedDisplay,0>'
Hmm… let’s try to find something to include! That’s always fun, bit like easter.
#include "Runtime/HeadMountedDisplay/Public/IHeadMountedDisplay.h"
Compiler: Pfft, not even gonna honor that with a response…
Me: Sorry! You never know!
Compiler: …
Error 1 error C2027: use of undefined type 'IHeadMountedDisplay'
Error 2 error C2039: 'ResetOrientationAndPosition' : is not a member of 'TSharedPtr<IHeadMountedDisplay,0>'
So lets try to outsmart UE4. It’s as easy as beating Kasparov in chess. What’s in that interface?
virtual void ResetOrientationAndPosition(float Yaw = 0.f) = 0;
And who dares implement that?
Plugins\Runtime\OculusRift\Source\OculusRift\Private\OculusRiftHMD.h
virtual void ResetOrientationAndPosition(float yaw = 0.f) override;
Plugins\Experimental\SteamVR\Source\SteamVR\Private\SteamVRHMD.h
virtual void ResetOrientationAndPosition(float yaw = 0.f) override;
Can’t very well include a plugin header file can I. It couldn’t possibly make sense to include those 2 implementations anyway. In the background a compiler giggles faintly. Oh, I know!
void AIkoPawn::ResetVRView()
{
if (GEngine->HMDDevice.IsValid() && GEngine->IsStereoscopic3D())
{
FOculusRiftHMD::ResetOrientationAndPosition(0.f);
}
}
Compiler: HA HA HA!! You god damned fool don’t even know what a static method is! Hint: this isn’t one!
Me: Oh shut it.
Compiler: And you’re calling code in a PLUGIN! Keep 'em coming, you’re a ******* comedian!
Ok maybe a little more systematic. Concentrate, think. After some testing the error actually happens here:
GEngine->HMDDevice**->**ResetOrientationAndPosition(0.f);
Intellisense says “pointer to incomplete class type is not allowed”, and of course that error from earlier, ‘ResetOrientationAndPosition’ : is not a member of ‘TSharedPtr<IHeadMountedDisplay,0>’.
So let’s look at this TSharedPtr. Aha, oho, interesting. Perhaps I need to do something really intelligent.
void AIkoPawn::ResetVRView()
{
if (GEngine->HMDDevice.IsValid() && GEngine->IsStereoscopic3D())
{
TSharedPtr<IHeadMountedDisplay> Dudley;
Dudley = GEngine->HMDDevice;
FOculusRiftHMD Dudders = Cast<FOculusRiftHMD>(*dudley);
dudders->ResetOrientationAndPosition(0.f);
}
}
Squiggly hell. Maybe a little more Rama.
[FONT=Comic Sans MS]void AIkoPawn::ResetVRView()
{
if (GEngine->HMDDevice.IsValid() && GEngine->IsStereoscopic3D())
{
[COLOR="#FFD700"]♥TSharedPtr<IHeadMountedDisplay> ♥Dudley;
Dudley = GEngine->HMDDevice;
♥♥♥♥♥♥
FOculusRiftHMD Dudders = Cast<FOculusRiftHMD>(*dudley);
dudders->ResetOrientationAndPosition(0.f);♥
}[/COLOR]
}
Compiler (giving Gollum impression): My eyses, they burn!!!
Me: You know what, I’ll do it in Blueprint.
Compiler: Owned.