Using a timer messes up my function (works with blueprint delays, but not C++ or blueprint timers)

Hello everyone,

I’m having a strange issue working with timers. I have a function set up which when called on its own, works perfectly. I can even call it using delays from within blueprint and it also works fine. As soon as I use a timer, whether it be in C++ or Blueprints, however, my function becomes buggy. The function itself does nothing special, but it does have a timer within it, which calls another function that also has a timer within it. Both functions are using the same ***FTimerHandle ***(declared in the .h), and I have them set up as follows:

First function (MyFunction_1):

GetWorld()->GetTimerManager().SetTimer(MyHandle, this, &ABaseCharacter::MyFunction_2, 0.1f, false);

Second function (MyFunction_2):

GetWorld()->GetTimerManager().SetTimer(MyHandle, this, &ABaseCharacter::MyFunction_3, 0.2f, false);

There’s some more code in it and if you think it’s useful I’ll share it, but all it does is change some settings to the character mesh and play an animation. What I mean by “buggy” is that when I use a timer to call the first function, the mesh rotates weirdly in one direction and then snaps back a couple of frames later. I am 100% sure there’s something wrong with how I’m using timers and it’s not the other code in the function. It happens if I use a timer in blueprint as well, and all I did in blueprint was use the “set timer” node attached to a custom event which just calls the first function. The weird part is that the function works perfectly fine if I call it without a timer, or if I use a delay instead. Unfortunately there aren’t delays in C++ so that’s not really an option. Since the issue is related to timers I thought it had to do with the TimerManager not being cleared so I called the clear function on it pretty much everywhere in my code to see if it changed anything but it did not. Please note that every timer *is being called; *I had it print a message during each part of the function and it succeeded, so they are definitely being called. They are just causing the function to behave strangely for some reason. Any suggestions?

Thank you for your time!

  • J

Perhaps you need to lose the timers and keep state instead and call these other functions from within Tick()?

So you need an instance variable to keep the “state” (i.e. “about to call MyFunction_2”, “about to call MyFunction_3”). The elapsed time since the last call and a switch statement.

You’re using the same timer handle for both timers, that’s why. Each Timer Handle needs to be unique. The same is true for Blueprint.


And if your timers are set multiple times dynamically then you can create your handles before setting the timers and add them to an array. You can later clear the timers using the handles in the array.

Thank you for the responses!

I thought it was that as well, but even if I create different timer handles in C++ the same issue still happens. Regardless, if I call ***MyFunction_1() ***directly in C++/blueprint, everything runs correctly. It’s when I use a timer to call ***MyFunction_1() ***that it has issues. But I tried again like this and it still didn’t work:

First Function (MyFunction_1):

FTimerHandle Function1Handle;
GetWorld()->GetTimerManager().SetTimer(Function1Handle, this, &ABaseCharacter::MyFunction_2, 0.1f, false);

Second Function (MyFunction_1):

FTimerHandle Function2Handle;
GetWorld()->GetTimerManager().SetTimer(Function2Handle, this, &ABaseCharacter::MyFunction_3, 0.1f, false);

I thought maybe it had to do with re-declaring the handles every time I used them or something like that so I moved both handles to the .h and called them directly. Still the same issue. I also tried clearing them right before I set them in each function, but it didn’t change anything.

Also, why can’t I use the same timer handle? If I clear it, why wouldn’t it work? Sorry for the noobish questions, timers have always confused me for some reason…

That sounds like a workaround if I can’t figure it out, but timers should work regardless, right?

The timer Handle is used for identifying the timer in the timer manager. As soon as you use it for a different function, the old timer will be removed.

FTimerHandle is just a wrapper for a uint64 btw :slight_smile:

So you are calling function 1 manually ONCE, then after 0.1 seconds function 2 gets called, 0.2 seconds later function 3 should get called. If you use a timer handle declare in your .h then it will prevent you from starting multiple timers at once, which I assume is what you would want. So just make sure that you call function 1 just once and let every function output a debug log, which contains the function name and current time.
For example:
UE_LOG(LogTemp, Warning, TEXT(“Function 1 executed. TimeSeconds: %f”, GetWorld()->TimeSeconds);

Again, thank you all for the help!

That’s how I thought it worked, but then why is my function being weird? Doesn’t using it for a different function remove it?

Okay that actually helps me understand it better, thanks. I should really take some time to read the source code.

I tried what you suggested, the output is basically the same (disregard the different starting times):
Calling function 1 directly:;base64

Calling function 1 with a timer (timer is in blueprint, but same result in C++):;base64

The only difference is that the first one yields no issues, and the second one is buggy.

Also, I am not trying to call multiple timers at once. I want a timer right after each function is executed. Wouldn’t the timer have already been exhausted by then? Since the function had been executed already? Each timer is defined in each function, I am not calling all the timers in the same one. In function 1 I set the timer for function 2, and in function 2 the one for function 3.

Hm it’s hard to figure out how to fix this because I don’t really know what you are trying to achieve and even what is going wrong. From the output I can tell that the timers are working just fine, so the problem is rather in the actual gameplay code in your functions, could rely on data that changed by the time you call function 1 with a timer for example.

I thought it could be something like that, but then why do delays work fine and timers don’t? I’ll post my source code if you think that helps, it’s nothing too complicated. But if the timers are set up fine then yeah it’s project specific haha. That was basically my question, if the timers are setup correctly then that’s it.