How do I get for each to work?

0 favourites
  • 9 posts
From the Asset Store
_______ Huge collection of metal fixtures ________
  • I have an object that moves right and then left. It all works fine, but then I instantiate 2 more of thos in the scene and my code runs the same code through a For Each loop.

    The first/initial object continues to move left/right without an issue but the two new instances get trapped in an endless loop not knowing to move left or right.

    Regardless of the actual code within the loop, why does the first instance work fine but additional instances do not? (They are each running the same code.)

    Surely the 'For Each' should have no preference?

    Maybe there are some common gotchas?

    Cheers

    Tagged:

  • Could be an expression that isn't referencing the correct thing in all cases. Hard to say without seeing what you did.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • In all cases it references itself (self). See the code image attached. Maybe there is something wrong here that I'm not seeing? Thanks

  • Put 'Trigger Once' outside 'For each' subevent. Check again all instance variable direction.

  • Thanks but that didn't work. I've tried every permutation of combining this with 'trigger once' both inside and outside the loop as well as breaking the loop into different parts but still doesnt work.

    It seems the two newer instances always keep setting their direction to "right". But why? Even when they are far off screen to the right.?!?!?

    Surely someone in the history of C3 has made a loop where a bunch of objects move left and right repeatedly?

  • SOLVED. The trick is not to use For Each at all and each dino works fine.

    I have no idea why and so now Im scratching my head as to the point of the For Each loop at all?

  • Yes because without 'For each' the expression already evaluate which instance has less or more than specified x position. It would be useful in situation where object has boolean variable as condition true/false state to set some action and toogle the state.

  • There may be something weird with using a wait in a for each loop. Even though it has a trigger once condition, I think that loop will run the trigger each tick, and tell each Dino to wait each tick.

  • For each - you can imagine it as basically copying the event, one copy per instance of the object, with only that one instance picked.

    Looks like this is a sub event of something else, given you have a local variable there. Are there conditions above it?

    Trigger once should be used in conjunction with other conditions. Otherwise, 'On Created' would be more suitable.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)