How to properly use delays?

I have this Bubble Sorting Algorithm implemented using the blueprints. Basically the process is I spawn objects with randomly generated integers. Get all of them and store in an array, do the sorting and swap the actor location accordingly.

I have to use delays or something to make the whole process slower so the swapping that’s happening are visible.
I have tried using delays in the while and for loop. the logic for the delay is for example duration is set to 1 second. for each loop is 1 second delay, the while loop since its nesting the for loop is 1 second * number of elements * 1.5. but the delay still does not work properly.

Any other suggestions? optimizations? fixes on the actual code?

Here is my swap Loc function

and Here is my get Loc function

Delay don’t stop execution of the code, delay initiate latent action, will break the thread and looping node will continue the operation, triggering another delay create new latent action. So all those latent actions will be executed on same time as it is initiated on same frame

Maybe insted of doing loop use Tick event which executes on every frame (with Delta Seconds being time passed between the frames) or use timers which you can loop

https://docs.unrealengine.com/en-us/Gameplay/HowTo/UseTimers/Blueprints

That said Arrays already have sorting functions which can sort any type that can use > operator in C++ (which you can declare and overload in C++)

I have a video for a typewriter widget where I needed a custom “For Each Loop With Delay”. Starting at 2:15 I go through the process of creating the custom for each loop macro. The delay is an input pin for easy access so you don’t ever have to go back into the macro to change the speed at which it runs through. Hope this helps.

The only catch is, I see in your last screen shot you have the “for each loop” in a function, this method will not work inside a function due to the delay node. You will have to bring the for each loop outside the function like I did into the event graph and call the function as part of the loop body.

Ah this is very helpful thank you. Though I still have to learn how to use them and their behaviour… any tutorials other than the documentation?