r/forgescripting • • 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 Upvotes

9 comments sorted by

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:

 x: 10 * current iteration 

Which would find the first object at x: 10, the second at x: 20, then x:30, etc.

3

u/0mni42 Nov 19 '22

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.

Yeah, that's what I'm trying to avoid. In short, I'm making a door with eight "teeth" that dilate open; making a separate script for each tooth is a pain and I'm looking for a way to do it more efficiently. There must be a way to apply a script to objects in a list simultaneously; it would be a baffling thing to not include.

2

u/Schmeck Nov 19 '22

Have you tried looking into custom events? I haven’t tested out this scenario myself, but you may be able to trigger a custom event for each object in the list that would run each movement action independently.

3

u/0mni42 Nov 20 '22

I've been looking into that yeah, though once again I don't quite understand what the terminology means and what I'm missing. I keep getting "On Custom Event node missing information; node graph failed to build" errors, and I don’t understand why.

→ More replies (1)

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

0

u/[deleted] Nov 19 '22

[deleted] — view deleted comment

4

u/hey-im-root Moderator Nov 19 '22

Use the documentation in the sticky post. At the moment we are building each nodes inputs and outputs so you know what they connect to.