How do I wait for the loop to end?

0 favourites
  • 6 posts
From the Asset Store
For Adventure, Action, Heroes, Sci-fi and Action-RPG games.
  • Hello!

    I have some "repeat" loops and I'm having trouble because I feel they are not going sequentianly. For example the last condition doesn't work inside the 'on start layout' but it does work if I take it out (which I don't want). I feel I'm arranging it wrong :/

    code

    ibb.co/rQg3kpk

    Intended Result

    ibb.co/hX3MwZ8

    *Image attaching was not working for me today, so I just added links

    Any idea on how to arrange it better or wait for the loop to complete?

  • The reason the last one doesn't work is because in C3 you need to wait for the next tick before the created objects become pickable and you have it all in one event. You could try adding a small wait then after this set a variable to true, which then triggers the for each loop if var=true.

  • It’s actually sooner than the next tick. What you can do is split that into multiple start of layout conditions.

    So instead of this which doesn’t work since the for each doesn’t have access to the newly created sprites.

    Start of layout 
    — repeat 10 times
    — — create sprite at random(100), random(100)
    — for each sprite ordered by sprite.y
    — — do something 

    You can do this which will work. The second start of layout has access to the newly created instances.

    Start of layout 
    repeat 10 times
    — create sprite at random(100), random(100)
    
    Start of layout
    For each sprite ordered by sprite.y
    — do something 

    In general any instances you create in an event aren’t pickable until the next “top level” event. A top level event is an event that isn’t a sub event of another event. They can be inside a group though.

    An alternate way that’s popular in the community is to use a wait. It effectively delays events from running until way after the created objects are pickable in general. I say way after since the shortest wait is 0 which seems to delay till the end of the event sheet.

    That would look like this:

    Start of layout 
    — repeat 10 times
    — — create sprite at random(100), random(100)
    — every tick
    — — wait 0 seconds
    — — pick all sprite
    — — for each sprite ordered by sprite.y
    — — — do something 

    Which works fine in a simple case like that but it makes it really hard to follow the order events run with heavy use.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • So you agree then, it's a Wait. The idea of having multiple on start of layout events seems confusing to me, I've never done that myself.

  • I prefer using multiple events over wait. Seems cleaner to me and keeps the events running in place.

    Honestly when new objects are pickable is one of the ugliest parts of the event system. If you don’t understand it it’s confusing, and annoying when you do since it feels like you’re doing code juggling to work around it.

  • Heya! R0J0houndR0J0houndThank you so much for your replies!!! I had to add some 'waits' and it worked >_< It looks dirty though...

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