Download

While Loop Problem

Hey,

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.

^ This ^

interesting thought experiment and you could bump up max iteration steps to see if it runs. But without some kind of delay in there (even zero to delay by a frame), its probably always going to register as infinite (as if you’ve looped a white wire back on itself). Couple of other things to try. Dump the while loop and just loop a white wire back on itself after inserting a GATE. When the Timer is killed off close the Gate. Still think a delay will be needed though. The alternative is to create sub-actors to run the actual code and drive Set-Tick-Interval for each actor off the Timer. That should work as you’re not trying to create your own Tick, you’re just using the built-in system to tweak it. But this is more work and loses the flexibility you were after to make this all worthwhile I suppose… :thinking:

1 Like

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.

^ This ^

Quick test… But calling Get-Timer-Elapsed-Time-By-Function-Name seems to return ZERO when the function name is bogus or doesn’t exist. So infinite loop! But the reset doesn’t do anything anyway to ensure the While condition halts. So there’s probably other edge cases where the code will break. Set Actor/Component Tick Interval would be far safer.:fearful: :cold_sweat: :cold_face:

1 Like

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:

Normally Gates are used to Tick sequences where pausing or disabled sequences are needed right-next to actively ticked sequences. Timers are normally used to ‘Tick’ sequences that don’t require as high a frequency. If you need finer granularity, then the big book says the design should be more OOP anyway, and functionality divided up into sub-components / child actors or objects that you control by setting their individual Tick properties. Overall, I get why you want this flexibility inside a generic macro. But this is one of those areas when it may burn you badly. BP is meant to be the servant of UE, not an attempt to usurp the kernel and create your own Tick. Even that Timer macro may be a btch to debug later when there’s so many things going on and much more complexity. :fire: :firecracker:

1 Like

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?

C++ would be the natural place to explore this as opposed to fighting BP. But ofc then the simplicity of the macro is gone… And you’re into threads and gotchas like this. So you’ll have to decide have much you want this. Time spent in C++ means time lost on gameplay.

1 Like

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):
Macro:


Event graph:

Result:


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.

Macro:

Event graph:

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

1 Like