While loop to infinity and beyond

Hello the community.

I get a feel of why it doesn’t work, but i can’t see how to make it work.
So I want to send the player’s character back to a starting position, using a while loop to fire the Add input movement until the character is within range of the starting point.

The loop being “infinite” crash the game.
What I guess is that the body of the loop (Add input movement) doesn’t really have time to move the character a notch, as a result the condition for the while loop is always False … Is it right ?!

Work fine with a Tick event and a switch, but i feel should have a way to achieve the result with a loop.

Joining two screen, The problematic loop and the tick event test.

Any advise will be much appreciated.

Want to add that making my own “while loop” using a short delay do work, looping on itself.
I could use that to make the trick. But It’s still bugs me to not understand why the normal loop doesn’t work.

Games as a whole are a loop.

If you do a loop (for example here a while loop) it will execute during the same frame .

This means your code might as well get the distance to your target point, remove 20 units (for your nearly equal) or just use the exact location and set your actor location to that. It would have the same effect but significantly easier code. Assuming movement input is processed each time you add movement input that is.

The movement component acts as abstraction layer. For multiple reasons you usually do not want to directly influence velocity on input. On one hand because of strafing (meaning an increased speed if you move forward and sideways at the same time), on the other because adding limited acceleration becomes an issue. As well as coupling the movement rate to real time instead of fps.

Basically. You can do more in a better and easier way if you store all movement commands and execute them just before the next frame. Rather than when a movement command is issued.

So what you do in this case is tell the movement component to move in direction X by Y. And you do so infinitely. Because the character doesn’t move. You just tell the movement component of your intention to move.

But more importantly. There is no time in between the while iterations. I mean there is. But we’re talking nano seconds here. Pure execution time. Not game time. So for the sake of argument. Let’s just assume no time has passed between the first and the second execution.

Everything that should happen over a timespan has to be done via event tick, a timeline or a similar construct (like you tried with “Delay”).

Thanks you.

I kinda have a hard time to completely understand, but I got the big lines.
I might try to dig into timeline for the first time then, can’t hurt to try. If anything, I might stay on the delay-loop-to-branch way. Work well and so far it doesn’t seems to impact the performance.

If it truly should run every frame I would highly suggest using Event Tick.

The delay is arbitrary. If you use a small enough number it will just wait till the next frame. If it’s too large it will skip frames, etc.

Event tick will run (by default) every single frame and is therefore (most likely) the better alternative.

You can still use that separate function. Just call it on event tick and get rid of the loop :wink:

Noted ! Timeline to try, might be a good exercise to get me started on it. Otherwise i would indeed add that function to my tick “list”
Thanks again :wink: