Loop with delay inside a function?

How to add a delay inside FOR or FOREACH loop inside a function? It seems impossible (design limitation, can’t put Delay nodes inside function :frowning: ) but how to achieve this in some workaround-way?
For example: Let’s say i want to, for each Bob to say “Hello”, wait 2 seconds and iterate this loop again, with another Bob saying “Hello”. Effect: Every Bob on my scene will say “Hello” with 2 seconds interval.
All this inside a ‘MakeAllBobsSayHello’ function :slight_smile: How to achieve this?

SO are you using a Loop or you just want it to loop?

I’ve redesigned my question to be more specific, with an example :slight_smile: So now the question is, how to make every Bob say ‘hello’ with 2 seconds delay inside a function? :smiley:

Use a custom event, not a function.

OK, i did it: First, use custom event instead a function, as @ suggested. But that doesn’t solve the problem, because foreach loop will not respect delay node and execute all Bobs at once. [HERE][1], in @ answer, there is an instruction how to modify for loop to respect Delay node. After modifying it this way, just setup it like this:

Now, all Bobs on scene will say Hello with interval specified in Delay node :slight_smile:

Thanks for suggestion, that doesn’t solve the problem because of FOREACH loop not respecting Delay node, but i’ve posted a workaround. +1 for You ^^

That is simple. Make a boolean which will determine if the function is supposed to be looping and on the outer node of delay, bring it back to For Each Loop.

This will still execute all array items (all foreach iterations) at once, because foreach loop doesn’t respect Delay nodes. I’ve already found a solution though (with use of modified ForEach), it’s in the answer :slight_smile:

This could be another approach:
(UE4.10) → Using “[Set timer by function name][1]” (link to the Unreal Docs).

Using this node, with the loop checked, and setting the for 2 seconds (from your question description) will produce similar effect that will solve the task (trigger a “hello” every two seconds).

https://docs.unrealengine.com/latest/INT/Gameplay/HowTo/UseTimers/Blueprints/index.html

There is a way to nest the “delay” within the Macro itself which I find much simpler and cleaner. Everything I have seen on this has the delay outside the macro but here I show how you can actually modify the macro to “wait” before looping so everything is self-contained.

1 Like