Why is this loop infinate?

I am trying to do Walk / Jog / Sprint and that much is fine however i want to prevent backwards sprinting when the player is running backwards. If the player presses the sprint action key and runs forward but then presses backwards having never let go of sprint then they are sprinting backwards (not what i want!)

To overcome this i thought of adding a while loop that will check if they are moving backward or forward however i get the “Infinate loop detected”

While my code may not be the ideal way this is done its all part of my learning the engine and if you have a better way i would love to hear it, however i would still hope somone can explain to me why this is an infinate loop when i have tested the DOT value and it is indeed changing between 1 and -1

Here are some screenies!

Thanks in advace :wink:

The Start Sprint function:

Where / How the function is called:

The Result:

I also tried it this way so that the loop was not dependant on the DOT and still it detects infinate loop and i have no idea why
Correct me if i am wrong but the only infinate thing about this is if the player keeps his finger down!!

The first pic loops because nothing sets the Sprinting to false in the loop body.

Last pic loops because nothing in the loop body changes the actor forward vector or velocity vector so that the condition would be set to false.

While loops are very rare in Blueprint since they are normally only used in calculations and calculations are often done in C++ instead.

Tick never triggers in a while loop so any value that gets updated on tick can’t be used for the condition in a while loop which is pretty much everything.

Instead you could just use booleans and Branches.

I would build the sprint into your Axis input. Set the Sprinting bool with the sprint key but gate movement with a branch, Is Sprinting AND Forward Axis >0. That way if the axis became negative it would stop the modifier.

The biggest thing I have learned about Loops is that they trigger a new loop every tick when an input is held. A change in input doesnt effect any loops already firing

Ohh i think i get what you guys are saying… ok let me try a new approach

Nope i still dont understand why this loop is infinate.

If anyone would not mind showing me a better way to hangle this i would be very grateful

I simply want to be able to sprint forward but not backward (i do not use controller rotation pitch)

OK I got it how i want it… just need some timelines to make it smooth but at least the player cant sprint in reverse now!

For anyone comming here from search engines here is what i did in the end. (Note: this will not be my final code just a start on the logic)

I created a function to update the direction of travel eg forward or backward and i call it on the Add Movement Input from the Input controler

My Jog Speed Variable is the speed i want my character to move unmodified.

The rest of my modifiers look like this all with their relative desired speed attached (Walk, Jog, Sprint)


and my key inputs call the functions

Thanks to those who replied, feel free to let me know if you know a better way to handle it

Forward Only Sprinting

The Branch check if sprinting and moving forward to set sprint max speed. all other cases (backpedal or side step) are normal speed

lol this is one of reasons why I love state machines for gameplay logic.
I don’t even have to think about this kind of problem anymore…

I was working on a controller that is very similar to movement modes from Metal Gear series (run, sneak, crouching, wall cover, etc)

Before using FSMs for input/action graphs the problems were driving me mad and tired.

The only drawback is I often have duplicated graphs with tiny changes for each state and I end up using the “Switch on xxx” nodes a lot inside of animation blueprint when deciding behavior based on current state of the gameplay state machines.

Thank you **CaveMonkey72 **that makes it much more staright forward “pardon the punn” :wink:

**BrUnO XaVIeR **yea i have just binge watched a series on state machines / root motion and will be starting again from scratch on this. Still, im learning so i dont mind. I am glad i was able to work out at least a reasonable way to do it but its time to do it the right way!

Man ive seen some crazy complex looking ones but the results are so smooth!

Thanks to all for your help