Download

Feature Request: Smarter ForLoop (Blueprints)

Decided to post this here as well. Original post: https://answers.unrealengine.com/que…r-forloop.html

UE Version: 4.20
OS: Windows 10 64bit

I noticed that the standard ForLoop Macro doesn’t behave correctly if you plug a larger integer (i.e. 20) into the “First index” and a smaller integer (i.e. 0) into the “Last Index”. Doing so causes the ForLoop to Complete immediately.

A simple check at the beginning after assigning a local variable would allow the for loop to know whether to increment or decrement on each loop.

The default behavior of the standard ForLoop is to “always” increment, even if the user plugs a negative value into the “first index”. This proposed ForLoop macro should provide the same behavior if a user plugs a negative value into the “first index” and a bigger value into the “last index”. In other words–it should be safe to implement this right into the standard ForLoop without changing the behavior of any existing projects.

The only way this new macro would change behavior in any existing projects, is if users were using the ForLoop incorrectly (i.e. plugging a larger value into the “first index” and a smaller value into the “last index”, in which case, that would cause the ForLoop to “Complete” immediately without actually looping anything. So, if there are any users using ForLoops in that manner (heaven forbid), they will be affected by this newly proposed behavior. For the sake of improving ForLoop behavior, you can safely consider them casualties not worth worrying about.

Macro Setup:

Example of usage:

Produced result:

(Number at top represents most recent loop)

Hope you consider! Thanks! -Neil

I wouldn’t consider this “smart”. Actually I’d consider this “dumb” as it checks the indices every loop iteration, increasing the overhead.
Instead, use this:

Or simply create a new Macro “Reverse For Loop” which simply decerements and not increments the index.