Timer crashing UE4 C++

Hello everyone,

I making a lantern class that slowly uses fuel over time so I tried to setup a timer to handle fuel consumption. The Drain function decreases the fuel level of the Lantern. I am trying to make it so that the Drain function is called every 1 second. Thank you for your help, below is the code I am using to handle the timer. Any advice is appreciated!

ALantern::ALantern()
{
	PrimaryActorTick.bCanEverTick = true;
	Fuel_Level = 10000.0f;
	FTimerHandle Timer;
	GetWorldTimerManager().SetTimer(Timer, this, &ALantern::Drain, 1.0f, true, 3.0f);
}

Hello AndrewPrograhams,

It would be best if you were to set your timer elsewhere. Setting your timer in your constructor is a bad idea as it immediately starts the timer and calls any function it is set to call. This is what is causing the crash. Setting it in BeginPlay would be a good idea.

1 Like

Thank you so much!

thankyou so much I was freaking out my ue4 kept crashing on build