# C++ How I made timers work when game paused

**URL:** <https://forums.unrealengine.com/t/c-how-i-made-timers-work-when-game-paused/496292>\
**Category:** C++\
**Tags:** tick, timer, unreal-engine, UE4, CPP, Blueprint\
**Created:** [February 9, 2022, 8:17pm UTC](https://forums.unrealengine.com/t/c-how-i-made-timers-work-when-game-paused/496292 "2022-02-09T20:17:39Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![MaxKapustin](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@MaxKapustin](https://forums.unrealengine.com/u/MaxKapustin)\
**Post date:** [February 9, 2022, 8:17pm UTC](https://forums.unrealengine.com/t/c-how-i-made-timers-work-when-game-paused/496292/1 "2022-02-09T20:17:39Z")

</div>

Hello everyone

What are you thoughts about how I made timets work even when game is paused?  
Here is the code

```auto
UCLASS()
class APausedTimerManagerTicker : public AActor
{
	GENERATED_BODY()
	APausedTimerManagerTicker();
	virtual void Tick(float DeltaSeconds) override;
};

inline APausedTimerManagerTicker::APausedTimerManagerTicker()
{
	PrimaryActorTick.bCanEverTick = true;
	PrimaryActorTick.bTickEvenWhenPaused = true;
}

inline void APausedTimerManagerTicker::Tick(float DeltaSeconds)
{
	Super::Tick(DeltaSeconds);
	if(GetWorld() && GetWorld()->IsPaused())
	{
		GetWorld()->GetTimerManager().Tick(DeltaSeconds);
	}
}

```

---

<div class="post-metadata">

**Author:** ![KelbyVP](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/kelbyvp/32/261024_2.png) [@KelbyVP](https://forums.unrealengine.com/u/KelbyVP)\
**Post date:** [February 9, 2022, 9:36pm UTC](https://forums.unrealengine.com/t/c-how-i-made-timers-work-when-game-paused/496292/2 "2022-02-09T21:36:15Z")

</div>

That seems like a clever solution. Does it work?

---

<div class="post-metadata">

**Author:** ![MaxKapustin](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@MaxKapustin](https://forums.unrealengine.com/u/MaxKapustin)\
**Post date:** [February 10, 2022, 11:34am UTC](https://forums.unrealengine.com/t/c-how-i-made-timers-work-when-game-paused/496292/3 "2022-02-10T11:34:34Z")

</div>

Yes it does work, but . . .

In my game I have resources like metal, wood, that are being collected every second. And they work using timers, and it’s obvious that they should be paused when game is paused. But when this class is used they collect resources even when game is paused, which is wrong.

Now I am thinking about writing own TimerManager that will allow to mark any single Timer as TickWhenPause = true, so I could controll which timers I want to tick when paused and which ones I dont want to.

Yes I am a newbie in UE4 and maybe there exist more correct solution, but I am not aware of it right now(((

---

<div class="post-metadata">

**Author:** ![Natalo77](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Natalo77](https://forums.unrealengine.com/u/Natalo77)\
**Post date:** [February 10, 2022, 12:30pm UTC](https://forums.unrealengine.com/t/c-how-i-made-timers-work-when-game-paused/496292/4 "2022-02-10T12:30:00Z")

</div>

I honestly think you’d be well placed to turn this into a pull request for the engine on github - This seems like a feature that a lot of people would find very useful

---

<div class="post-metadata">

**Author:** ![MaxKapustin](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@MaxKapustin](https://forums.unrealengine.com/u/MaxKapustin)\
**Post date:** [February 10, 2022, 2:00pm UTC](https://forums.unrealengine.com/t/c-how-i-made-timers-work-when-game-paused/496292/5 "2022-02-10T14:00:25Z")

</div>

Really? I think you overestimating me)

---

<div class="post-metadata">

**Author:** ![AnkaPhantom](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/ankaphantom/32/578286_2.png) [@AnkaPhantom](https://forums.unrealengine.com/u/AnkaPhantom)\
**Post date:** [October 17, 2022, 7:40am UTC](https://forums.unrealengine.com/t/c-how-i-made-timers-work-when-game-paused/496292/6 "2022-10-17T07:40:18Z")

</div>

Hi, about writing your own TimerManager that will allow any single Timer works when game pause, do you have any follow-up solution? I would like to know, thanks!

---

<div class="post-metadata">

**Author:** ![eblade](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@eblade](https://forums.unrealengine.com/u/eblade)\
**Post date:** [October 17, 2022, 8:28am UTC](https://forums.unrealengine.com/t/c-how-i-made-timers-work-when-game-paused/496292/7 "2022-10-17T08:28:59Z")

</div>

Unless you’re going to override TimerManager and build it o ut to handle that sort of thing, because you need it a lot for some reason, then it’s easiest just to do a Tick in the object that needs it

---

<div class="post-metadata">

**Author:** ![DarioGangi](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/dariogangi/32/1215012_2.png) [@DarioGangi](https://forums.unrealengine.com/u/DarioGangi)\
**Post date:** [February 28, 2023, 10:53pm UTC](https://forums.unrealengine.com/t/c-how-i-made-timers-work-when-game-paused/496292/8 "2023-02-28T22:53:45Z")

</div>

This is a huge workaround and it could cause a lot of issues. I don’t think a pull request like this will be accepted.

---

<div class="post-metadata">

**Author:** ![Sterik\_GS](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Sterik\_GS](https://forums.unrealengine.com/u/Sterik_GS)\
**Post date:** [March 23, 2023, 2:06am UTC](https://forums.unrealengine.com/t/c-how-i-made-timers-work-when-game-paused/496292/9 "2023-03-23T02:06:42Z")

</div>

I modified your system by adding a timer call function on the real-time actor itself. By adding a link to it to your subsystem. Now I can run timers through it using real time. Maybe it will help others!

```auto
#include "RealTimeActor.h"

#include "Kismet/GameplayStatics.h"
#include "GameSystem.h"

// Sets default values
ARealTimeActor::ARealTimeActor()
{
 	// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;
	PrimaryActorTick.bTickEvenWhenPaused = true;
}

// Called when the game starts or when spawned
void ARealTimeActor::BeginPlay()
{
	Super::BeginPlay();
	
	UGameInstance* GameInstance = Cast<UGameInstance>(UGameplayStatics::GetGameInstance(GetWorld()));
	UGameSystem* GameSystem = GameInstance->GetSubsystem<UGameSystem>();
	GameSystem->RealTimeActor = this;
}

// Called every frame
void ARealTimeActor::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
}

void ARealTimeActor::StartRealTimer(FTimerHandle TimerHandle, FTimerDelegate TimerDelegate, float time, bool bLoop)
{
	GetWorldTimerManager().SetTimer(TimerHandle, TimerDelegate, time, bLoop);
}

```
