While Loop Problem


So I made this reusable timer macro that works fine in itself. The problem arose when I came up with the idea to add a while loop to the timer so I can basically run a tick only when the timer is active… seems like it would be extremely useful.

Here is the timer macro:

I think this should work but whenever I activate the timer, the while loop complains about an “infinite loop”. This makes no sense to me because when the timer is not active or when it is cleared, the elapsed time is -1, and since I have the “>=” condition, it should not be an infinite while loop??? Help! :slight_smile:

I don’t think it’s possible to implement your own tick using a While loop.

Is it possible that Get Timer Elapsed Time by Function Name never returns a negative number? That would prevent this from ever existing. You could try changing it to while (A > 0.f) rather then while (A >= 0.f).

Also just wondering where this would be useful. I’m still new and have not run into a place in which I would use something like this.

Yep, and simpler:

I put a print string right after the “Get elapsed time” node and it does return -1 when the timer isn’t active or when it is reset. And if I put it as “>” instead of “>=” it would never activate the while loop because the execution to it gets ran when the elapsed time is at 0.

1 Like

I feel like the while loops in Blueprint are pretty much useless :joy: I have coded some text based games for myself in pure C++ Visual Studio Code and in that while loops are very useful and make sense.

So there is no way to do it with purely this while loop method then, sucks… :frowning: I guess I could give the gate a go at some point. Thanks for replying anyways :slight_smile:

EDIT: Yeah so I tried the gate and it’s the same thing unfortunately. Thanks for the suggestion tho.

Hmm, but what if I need a constant tick running as well? I assume then pausing the tick will pause it for everything in that Blueprint and there is no easy way to have two ticks essentially with one running and one paused?

So I ended up doing a workaround where I run the normal tick from the Blueprint through the timer macro. This way I can split it into other places with a sequence node and get the tick from the timer when it is running. Not ideal but I guess it’ll do…

Thanks for all the replies :slight_smile:

Yeah, I’m kinda new to Blueprint so I don’t get these things since I’m used to working in C and C++. Btw do you think something like my original plan could be possible to do in C++ script, or is it gonna be the same story since it is still used inside UE4? :thinking: And would it even be a good idea to try it or do you think it will be too risky?

Alright man, thanks for the tips. I think I’ll stick to my workaround for now since it does the job and isn’t too sketchy :slight_smile: I have it in my head tho that at some point I do want to switch over to using C++ scripts too since they are much faster than BP.

Just so you know, you DEFINITELY won’t be able to use the while loop as a custom tick in C++, but you could register a custom tick function.

Hmm, good to know. I will surely look more into all of this when I decide to delve into C++ scripts. I’ll be sure to keep custom tick functions in my mind :slight_smile:

Simplified your setup (more of an alternative):

Event graph:


Also, notice they’re all in sync even when pausing. If you want to allow them to go out of sync, you would add a branch before the delay node.

1 Like

Forget my last one; I went with your method and improved it a lot imo. Using the “Set Timer By Function Name” makes it much simpler because the function/event doesn’t need to be connected to the macro. I added functionality so that if the function name changes, it clears the last timer before setting the new one.


Event graph:

First press starts “Hello” tick, second press clears “Hello” tick and starts “World” tick, and third press clears “World” tick.

1 Like