Engine cast returns null

I am trying to use the following code as a callback lambda:

auto notificationClicked = [](const FGeometry&, const FPointerEvent&) -> FReply
	{
		GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, TEXT("Notification was clicked"));
		// we restore the game window no matter where it is
		UGameEngine* engine = Cast<UGameEngine>(GEngine);
		if (engine != nullptr)
		{
			TSharedPtr<SWindow> windowRef = engine->GameViewportWindow.Pin();
			if (windowRef.IsValid())
			{
				SWindow* window = windowRef.Get();
				if (window != nullptr)
				{
					if (window->IsWindowMinimized())
					{
						window->Restore();
						window->ShowWindow();
						GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, TEXT("Game was minimised"));
					}

					window->BringToFront(true);
					window->HACK_ForceToFront();
				} else {
					GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, TEXT("Window was nullptr"));
				}
			} else {
				GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, TEXT("Window Ref was not valid"));
			}
		} else {
			GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, TEXT("Engine was nullptr"));
		}

		return FReply::Handled();
	};

However I am getting the message “Engine was nullptr”. Is there any reason why the engine cast would return nullptr?

I am including “Runtime/Engine/Classes/Engine/GameEngine.h”

Were you able to resolve this? I too am facing this problem.

I am also stuck on this same problem.
Cast<UGameEngine>(GEngine) cast fails and returns a nullptr
Has anyone figured this out?

Hi,

I suppose you’re testing it in PIE mode. Try testing in Standalone mode and see if it works.

I’m not very familiarized with GEngine, but as I recall, it is a UEditorEngine in PIE and UGameEngine in Standalone.

Try this code in PIE, New PIE window and Standalone modes

if (const UGameEngine* GameEngine{ Cast<UGameEngine>(GEngine) })
			GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, TEXT("Game Engine"));
		else if (const UEditorEngine* EditorEngine{ Cast<UEditorEngine>(GEngine) })
			GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, TEXT("Editor Engine"));
		else
			GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, TEXT("nullptr"));
1 Like