C++ How I made timers work when game paused

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!

#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);
}
1 Like