r/forgescripting • u/0mni42 • Nov 19 '22
For Each Object--how does it work?
I'm trying to make a series of objects all move at the same time, but when I use the [For Each Object] node, it makes them move one at a time, according to their order in the object list. How can I make them move simultaneously, or at least reduce the amount of time between them? Am I using the wrong node for this kind of thing?
Honestly, I just don't quite grasp the logic behind some of these nodes yet; I just have no idea what some pins are supposed to connect to. [For Each Object]'s last pin, for instance: "current iteration." What can that connect to? What's it for?
View all comments
3
u/Schmeck Nov 19 '22
It seems that if you use For Each Object with a node that includes a duration parameter (like Move Object to Transform and Translate Object to Point do), then each iteration of the loop will wait for the previous one to finish. Your intuition is correct, though. This delay wouldn't happen in a typical programming language.
If I had to guess, this behavior is a limitation imposed on the scripting engine. For each source event that is triggered, the engine appears to only allow one "thread" of actions to be carried out. Using the For Each Object node could effectively split one event source into multiple simultaneous threads.
To get around this limitation, you can just wire up multiple event sources instead of relying on the object loop, but that may be impractical for certain situations.
And yes, the "current iteration" pin will simply output a number that will count up for each loop. You can use this to, for example, apply some offset for your object movement. So the objects may move to:
Which would find the first object at x: 10, the second at x: 20, then x:30, etc.