Need help with loops with delay

okay, i need to do a loop with delay (i want to spawn some enemies from a “spawner” but i don’t want them to spawn together, i want a small delay between every spawn, looks better to me this way). I can’t think of 3 ways of doing it first and most basic way, doing a for loop and putting a delay node in the end of it, but seems that the delay node doesn’t work inside for loops and this seems to be a known problem still unsolved, so i can do it this way

the delay is ignored in this case

another way to do it is doing a “manual loop” but in this way looks like i have some kind of bug:
the value of “iterator” just goes from “2” to “0” after the delay node so the branch gives false and the loop doesn’t work, this makes no sense to me. i know it goes from 2 to 0 because i printed the value of iterator before and after the delay node

another way is to use “set timer by function name” it seems to don’t work in functions with some kind of input, so what can i do?

1 Like

i think a much simpler way to fix your issue, make a function with all the spawning stuff in it
make a boolean called “canSpawn” or something like that
from event tick, make a branch that checks this boolean. if its true, call the function, at the end of the funciton set the boolean to false. and set your timer variable to some value
From the false part of the branch, subtract delta time from the float variable. then check if it equals zero, if it does, set canSpawn to true

Create your own “For Loop” macro and incorporate a delay in that. If you notice on the original For Loop, the header of the node is grey which denotes that it is a macro - double click it to open up it’s internal coding. Now select everything inside and copy it all.

Back out into your blueprint you want to use it on, or better yet create your own global Macro Library. Create a new macro, call it “For Loop With Delay” then paste the code you copied into your new macro. Ensure to create input and output pins to match the original For Loop and hook it all up the exact same. To make your new macro versatile, create an added input pin of type Float and call it ‘Delay’. In the proper section of the macro, add a ‘Delay’ node and either enter the desired delay or if you created the input pin, plug that into the delay and VOILA! You’re own ‘Custom’ For Loop with built in Delay!

NOTE: The method shown in the first image will still have a delay of your designated time before the ‘Completed’ execution pin runs. This may be an undesirable side effect. This can be easily fixed with a simple branch to check if the current integer is greater than the last index and if it is, don’t execute the delay. See the final image for that alternative method.

Here’s some images to help you understand! If this solved your problem please do me a favor by accepting the answer as correct! Remember that commenting after you accept the answer deselects it as accepted. You’ll know it’s good when it stays green! Thanks and Good Luck!

1 Like

You can’t place delay in loops, you have to make your own :

Loop iterations will take place between frames, so large loops may incur a performance hit.

https://docs.unrealengine.com/latest/INT/Engine/Blueprints/UserGuide/FlowControl/#forloop

I would definitely recommend to use the “SetTimerByFunction” blueprint and repeat this as many times as possible. Each time it is called it will handle finding a valid spawn point etc… If you have to pass data in, simply save it into a variable. I believe this method is much smoother and cleaner than using a delayed for loop.

ty but it seems that i can’t place a delay node in a macro, so i can’t accept your answer, at least not yet

PS.: ok it looks like its possible to put the “delay” node in a “local macro” but it’s not working, the delay is just being ignored.

Just put the delay node right bwfore the macro gets called

Correct, you can put a delay into a macro but you can’t, nor do you want to, put one in the original Standard Macro. Send some SS of what you did because if the delay isn’t working inside your custom macro you did something wrong. It works fine for me and I use my custome for loops with delay frequently with no issues.

Putting a delay before a loop does not delay the execution of each iteration inside the loop body, it only delays the initial execution of the loop. Creating a custom macro and setting it up exactly as I show in the images is how to create a for loop with custom delay so that the loop body delays on each iteration.

ok, just opened the project today and it was working, i used something similar to your first SS because i need the delay in the last node in this case.

Very close to what jtsmith said to do, a macro in the BP. But replicating the ForEachLoop’s Macro with a delay.

Imgur: The magic of the Internet

Imgur: The magic of the Internet

I have used the marked reply solution for over two years, then I noticed it introduces a slight additional delay every iteration. This should be the preferred method when you need extremely precise timing.

This work perfectly . Thank you

Your Macro Type needs to be ACTOR, not Object in order to add the delay.