'wait' inside a 'for'/'repeat' loop

0 favourites
  • 13 posts
From the Asset Store
SynthWave Loop Pack includes 68 seamless loops, founded on 11 original melodies.
  • So I'm trying to achieve the following behavior:

    -subtract 1 from variable X

    -update text that displays variable X's value

    -wait 0.5

    repeat the above steps for Y times. when I enter any value of Y (e.g.27) the behavior I get is a one-step subtraction of Y from X (instead of subtracting 1 each time, until Y-worth of 1's have been subtracted).

    I get the exact same behavior when using 'repeat' instead of 'for'.

    Can one not use 'wait' inside loops? If so, how to achieve desired behavior?

    can anyone shed light?

    Thanks! :)

  • You can use wain in loops, here is an example:

    For n=0 to 10
     Wait (loopindex*0.5)
     Subtract 1 from var
     Text set text to var
    
  • This is a common misunderstanding of "Wait": it doesn't pause all event execution. (It wouldn't be much use if it did, since it would just hang your game.)

    It really means "schedule the remaining actions and subevents to run after a delay, then carry on". So the loop still completes immediately, and the wait schedules a bunch of actions/subevents to run afterwards.

    However in this case, there are no actions or subevents after the wait, so it schedules to do nothing after a delay.

  • dop2000 Thanks! your solution worked:)

    Ashley

    but I get the same behavior even when I put the wait BEFORE the other actions (see pic)

  • That won't do exactly the same thing: it will schedule the following two actions to all run at the same time after the same delay, instead of before.

  • Ashley

    So that I'll understand better when and why to use the wait action, could you please elaborate why it is executed only once when it is a part of a loop? Why do I need to use the 'trick' dop2000 suggested instead of using the regular wait action?

    Thanks!

  • It's not executed once, it's executed as many times as specified in the loop. But if you are using a fixed value for the Wait action, all iterations will be completed after the same delay. For example:

    Repeat 10 times
    Wait 0.5s
    Add 1 to variable
    

    After 0.5 second delay all scheduled actions will happen at the same time. So the variable will be incremented by 10.

    Basically, it's equal to this:

    Wait 0.5s
    Add 1 to variable
    Add 1 to variable
    Add 1 to variable
    Add 1 to variable
    Add 1 to variable
    Add 1 to variable
    Add 1 to variable
    Add 1 to variable
    Add 1 to variable
    Add 1 to variable
    
  • dop2000

    from your explanation:

    Wait 0.5s

    Add 1 to variable

    Add 1 to variable

    Add 1 to variable

    Add 1 to variable

    Add 1 to variable

    Add 1 to variable

    Add 1 to variable

    Add 1 to variable

    Add 1 to variable

    Add 1 to variable

    I see that the wait is executed only once, at the start, while the add action is repeated 10 times...

    How does your trick fix this? Also, how does your trick not increase the wait time between steps the more times the loop is executed?

    Also, thanks for taking the time and effort to explain! :)

  • do

    wait 0.5s x loopindex

    see what happens

  • dop2000 is basically correct, but there are really 10 "Wait 0.5s" at the start - but they all run in parallel. So it's the same as if there was one "Wait 0.5s" at the start.

    Internally, you are creating ten "Wait 0.5s then add 1 to variable" operations, which all run in parallel so all the waits happen at the same time. This has the same effect as "Wait 0.5s then add 10 to variable".

    If you do something like "Wait loopindex * 0.5 seconds", then you create a sequence like this:

    Wait 0.5s then add 1 to variable

    Wait 1.0s then add 1 to variable

    Wait 1.5s then add 1 to variable ...

    These also run in parallel, but they finish at different times, resulting in adding 1 to the variable every 0.5s.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Thanks for the very informative reply.

    Now I feel like I understand the wait concept much better.

  • There is this tutorial about the Wait system action in Construct 2 (still applies to Construct 3) :

    https://www.construct.net/en/tutorials/system-wait-action-63

  • I wonder if renaming the wait action to something like "Delay following actions" might help reduce confusion.

    And/or a tooltip maybe in the description that explicitly states that it does not pause event execution.

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