Unreal engine exit from vr preview

I wrote C++ class.
I want to exit when some conditions are true.

“exit(0);” terminates ue4 editor together with VR preview

how to exit from VR preview only?
how to end the game in c++ class?

I’m not sure if this is the best way, but you can use the exit console command, i.e.

UWorld* world = GetWorld();

if (GEngine && world)
{		
		GEngine->Exec(world, TEXT("exit"));	
}

If you are already including Kismet/KismetSystemLibrary, Try:

UKismetSystemLibrary:QuitGame(GetWorld(), GetController(), EQuitPreference::Quit)

If not, you can plop this anywhere:

FGenericPlatformMisc::RequestExit(true)

The first one allows you to specify a controller and to chose whether to actually quit or minimize the window
The second one just sets the game loop boolean to false, and is more portable since you don’t need includes.

exit(0) terminates the whole process, including the editor. You should be using the built in functions above instead

Sources:

https://forums.unrealengine.com/development-discussion/c-gameplay-programming/52562-c-quit-game