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?
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.
5
u/Aaronspark777 Nov 19 '22
I believe current iteration is the number of times it's repeated the loop. First run is 1, 2nd is 2, etc
3
u/iMightBeWright Nov 19 '22 edited Nov 19 '22
I use this node a lot, so I've had a chance to learn what all the circles mean, but I was also confused before trying a bunch of things. You've got some good answers already, but I'll throw my info in, too, just for the heck of it:
The Inputs:
The outputs:
Edit: and it's also worth noting that currently you can't use this node to move a prefab a set distance in a certain amount of time like you might be hoping. Scripting movement and rotation onto non-normal-physics prefabs is broken right now, and unfortunately this isn't a workaround for making a moving gondola, elevator, or other groups of objects. It can, however, teleport stuff if you know how to calculate their independent locations.
0
u/[deleted] Nov 19 '22
[deleted] — view deleted comment