Switch to VR at runtime

hi, I am trying to switch to VR and Non VR at runtime, but it is not working, Please help

void AMyPlayerController::ChangeApplicationMode(EApplicationMode::Type NewMode)
{
	APawn* PrvPawn = m_pPawn;
				
	FActorSpawnParameters SpawnParams;
	SpawnParams.Owner = this;
	SpawnParams.Instigator = NULL;
	SpawnParams.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
	
	APawn* NewPawn = nullptr;
	
	if (NewMode == EApplicationMode::VirtualReality)
	{
		if (UHeadMountedDisplayFunctionLibrary::EnableHMD(true))
		{
			NewPawn = GetWorld()->SpawnActor<APawn>(VrPawnBlueprint, SpawnParams);
		}
	}
	else
	{
		if (UHeadMountedDisplayFunctionLibrary::EnableHMD(false))
		{
			NewPawn = GetWorld()->SpawnActor<APawn>(DesktopPawnBlueprint, SpawnParams);
		}
	}
	
	if (NewPawn != nullptr)
	{
		UnPossess();
		Possess(NewPawn);

		PrvPawn->Destroy();
	}
}
  1. You don’t handle all the outcomes in the nested ‘if’ statement. For example, what if the application mode is VirtualReality but the HMD cannot be enabled for some reason yet? If you don’t need to do anything, then it’s ok though.

  2. I suppose it is better to move the enabling of HMD out of this method. Trying to enable the HMD and then, based on the outcome, switching to VR is tidier as you would have two methods: one for checking the mode and another for spawning a pawn, which is easier to test and maintain.

  3. Is it a multiplayer game? If it is, then you are trying to enable HMD on the server, not on the client and you would need to enable HMD on the client side, then send the result to the server and spawn the appropriate pawn.

hi, It is not a multiplayer game, It is a architectural rendering application where I need to switch between VR/NonVR mode.

Do you need to continuously switch between VR and non VR mode or just check it one time on startup whether VR is present or not?

Hi, Yes I need to continuously switch between VR and Non VR. I have a checkbox in my UI, Whenever user checks the checkbox it will enable VR and all the UI will convert to VR mode, and when user unckecks it it will convert back to Non VR mode.

It was working in editor If I start in VR mode. But in packaged build it is not working.

Do you test it in the editor or in the packaged version?