Using Wait in loops

9

Attached Files

The following files have been attached to this tutorial:

.capx

wait-in-loops.capx

Download now 172.21 KB

Stats

10,956 visits, 16,378 views

Tools

License

This tutorial is licensed under CC BY 4.0. Please refer to the license text if you wish to reuse, share or remix the content contained within this tutorial.

Published on 27 Nov, 2013. Last updated 19 Feb, 2019

Although this can be found in the fabulous manual, there are topics about it popping out in the forums from time to time. This tutorial will show how to use the System.Wait action in a Repeat event, and why to use it that way.

The problem with Wait is that it only pauses the current event stream. What does it not pause is:

- the whole game,

- the loop,

- other parallel events;

As explained by Ashley at one point: "I think people keep assuming it stops running the rest of the events during the wait. It can't possibly do that, because it would make it nearly useless - the whole game would hang during the wait..."

The result of all this is that if you do something like the following:

The first instinct is usually that the position will be updated every 0.5 seconds. This is not the case. C2 does not run the loops one after another, waiting for the previous iteration to finish. As a matter of fact, this is not even technically a loop. A loop is usually understood as something that runs to the end and then 'loops'.

What follows is that all 100 position changes in the above example will be calculated almost instantly and there will be no visible movement. The system will first wait 0.5 s one hundred times - but it will do so simultaneously.

The way Repeat works in C2 is that the system runs the events X times without caring what's inside, so that they become parallel, independed streams. This is not what happens:

     |---loop 1---||---loop 2---||---loop 3---||---loop 4---||---loop 5---|

What really happens is more like this:

     |---loop 1---|
      |---loop 2---|
       |---loop 3---|
        |---loop 4---|
         |---loop 5---|

No matter how many Waits you put inside the loop (I'll call it that for the sake of simplicity) it will still look like this.

What we want to achieve with Wait, however, is almost always the former situation. Fortunately, there is a simple way of doing that which consists in "manually" delaying each iteration of the loop.

In the above example, we want the first iteration to start at 0.5 s, the next one at 1 s, the next at 1.5 s. What is necessary, is delaying the start of each loop by (time) multiplied by loopindex, i.e. the number of the particular iteration.

This way, the loop waits:

    0.5 s * 1 = 0.5 s
    0.5 s * 2 = 1 s
    0.5 * 3 = 1.5 s
    0.5 * 4 = 2 s
    etc. 


Thus:

Should work as expected. And it does!

CAPX included.

NOTE: in this particular example it would perhaps be better to just use the Every X seconds condition, but there are situations where WAIT is more flexible.

.CAPX

wait-in-loops.capx

Download now 172.21 KB
  • 1 Comments

  • Order by
Want to leave a comment? Login or Register an account!