# Engine cast returns null

**URL:** <https://forums.unrealengine.com/t/engine-cast-returns-null/439125>\
**Category:** C++\
**Tags:** engine, unreal-engine, cast, CPP, question\
**Created:** [December 8, 2018, 7:59pm UTC](https://forums.unrealengine.com/t/engine-cast-returns-null/439125 "2018-12-08T19:59:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mastercookerYT](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@mastercookerYT](https://forums.unrealengine.com/u/mastercookerYT)\
**Post date:** [December 8, 2018, 7:59pm UTC](https://forums.unrealengine.com/t/engine-cast-returns-null/439125/1 "2018-12-08T19:59:28Z")

</div>

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”

---

<div class="post-metadata">

**Author:** ![meetakshay99](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@meetakshay99](https://forums.unrealengine.com/u/meetakshay99)\
**Post date:** [February 26, 2021, 6:13am UTC](https://forums.unrealengine.com/t/engine-cast-returns-null/439125/2 "2021-02-26T06:13:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Sim\_Dev](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Sim\_Dev](https://forums.unrealengine.com/u/Sim_Dev)\
**Post date:** [April 27, 2022, 5:47pm UTC](https://forums.unrealengine.com/t/engine-cast-returns-null/439125/3 "2022-04-27T17:47:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![BrunoCaxito](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/brunocaxito/32/706161_2.png) [@BrunoCaxito](https://forums.unrealengine.com/u/BrunoCaxito)\
**Post date:** [April 27, 2022, 6:37pm UTC](https://forums.unrealengine.com/t/engine-cast-returns-null/439125/4 "2022-04-27T18:37:43Z")

</div>

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

```cpp
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"));

```
