# Event on Close

**URL:** <https://forums.unrealengine.com/t/event-on-close/298087>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine\
**Created:** [November 4, 2014, 4:13am UTC](https://forums.unrealengine.com/t/event-on-close/298087 "2014-11-04T04:13:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![AaronShea](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@AaronShea](https://forums.unrealengine.com/u/AaronShea)\
**Post date:** [November 4, 2014, 4:13am UTC](https://forums.unrealengine.com/t/event-on-close/298087/1 "2014-11-04T04:13:29Z")

</div>

I’m looking for an overridable function or event to listen for the window of the game being closed. I need to do some cleanup operations before the window can be allowed to close up.

I’ve tried Destroyed() and EndPlay() in the AGameMode class without any luck

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_957a4452](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_957a4452](https://forums.unrealengine.com/u/anonymous_user_957a4452)\
**Post date:** [November 5, 2014, 12:48pm UTC](https://forums.unrealengine.com/t/event-on-close/298087/2 "2014-11-05T12:48:05Z")

</div>

Hi Aaron,

I think the best way to do this. It is create custom game engine class and implement events you want.

You should add to DefaultEngine.ini

```
[/Script/Engine.Engine]
GameEngine=/Script/QAGame.MyCustomGameEngine

```

MyCustomGameEngine.h

```
#pragma once

#include "Engine/GameEngine.h"
#include "MyCustomGameEngine.generated.h"

UCLASS()
class UMyCustomGameEngine : public UGameEngine
{
	GENERATED_UCLASS_BODY()

	// When engine starts
	void Init(class IEngineLoop* InEngineLoop);
	// Close engine
	void PreExit();
};

```

MyCustomGameEngine.cpp

```
#include "QAGame.h"
#include "MyCustomGameEngine.h"

UMyCustomGameEngine::UMyCustomGameEngine(const FPostConstructInitializeProperties& PCIP)
: Super(PCIP)
{}

void UMyCustomGameEngine::Init(class IEngineLoop* InEngineLoop)
{
	Super::Init(InEngineLoop);
}

void UMyCustomGameEngine::PreExit()
{
	Super::PreExit();
}

```

Best regards,

---

<div class="post-metadata">

**Author:** ![AaronShea](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@AaronShea](https://forums.unrealengine.com/u/AaronShea)\
**Post date:** [November 5, 2014, 12:57pm UTC](https://forums.unrealengine.com/t/event-on-close/298087/3 "2014-11-05T12:57:47Z")

</div>

Exactly what I was looking for

---

<div class="post-metadata">

**Author:** ![tueurdudimanche](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@tueurdudimanche](https://forums.unrealengine.com/u/tueurdudimanche)\
**Post date:** [February 9, 2016, 8:38pm UTC](https://forums.unrealengine.com/t/event-on-close/298087/4 "2016-02-09T20:38:27Z")

</div>

I’ve got syntax errors when trying to recompile my project ☹ Can you help me?

---

<div class="post-metadata">

**Author:** ![zikdoz](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@zikdoz](https://forums.unrealengine.com/u/zikdoz)\
**Post date:** [July 23, 2016, 10:38pm UTC](https://forums.unrealengine.com/t/event-on-close/298087/5 "2016-07-23T22:38:15Z")

</div>

There’s a GameInstance to handle this.

Init: [UGameInstance::Init | Unreal Engine Documentation](https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/Engine/UGameInstance/Init/index.html)

Shutdown: [UGameInstance::Shutdown | Unreal Engine Documentation](https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/Engine/UGameInstance/Shutdown/index.html)
