Is it possible to "divide" a loop on several ticks ?

0 favourites
  • 9 posts
From the Asset Store
SynthWave Loop Pack includes 68 seamless loops, founded on 11 original melodies. For use in game and custom works.
  • Hello,

    I got a project with an essential "For each" loop that can be CPU intensive. I have to restrain it to every x seconds action instead of every tick to be comfortably playable at some level in the game. I was wondering how it is possible to divide, or allocate this loop into several ones. For example, I would like to check the first half of the picked instances in a tick, and the second in the next one. Or if I use "every x seconds", check the first half at t 0, the second at t 0.15 seconds, again the first at 0.3, etc...

    Any idea ? Did anyone ever come to this optimization ?

  • just an idea

    If you would make a variable that you change every 0.15 you could make a condition to your loop to run it only when var is set.

  • Hey fm4fanAT,

    Thanks, that seems to be the way. But I just made a stress test and found no improvement, CPU usage is the same. I set an instance variable for the sprites and a global variable for keeping track of checked groups. On sprite creation, I set the instance variable to the global and increment the global (from 0 to 2, so I got 3 groups of sprites). In the loop I filter the selected sprite to check with condition (if instance variable match the actual global) and increment the global. Before I tested every 0.6 seconds, now 0.2 with 3 times less calculation per loop.

    So it does not lower the total amount of calculations needed of course, but only better divide the amount during same time as before. It only decreases little stuttering that can occur when there is too much calculation in one big loop, by doing more smaller loops.

    Very good in theory, but few results in practice.

    Do you know other kind of optimization when it comes to large number of units ?

  • We'd have to see what you are doing.

  • you can do it with a normal for loop and the pick nth condition. Like the first half would be:

    For "" from 0 to Sprite.count/2

    Pick Sprite instance loopindex

  • blackhornet, I could make a little capx but it's more about ideas on optimizations for many units.

    Yes, thanks What I did and explained in my last post is very similar. But as I told it's very small improvement unless you have hundreds or thousands of units. It basically splits the amount of calculations, that remains the same, onto multiple smaller loops. Instead of having one periodic "big" stutter, you get small ones.

    I thought it could be a great optimization, but it's "only" a good little trick. Am I wrong ?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Or use this event balancer.

  • Nabu,

    Running a thousand loops will take just as long all running in one frame, or split 100 each into ten frames. The advantage of splitting them is that you can update a progress par for instance, since you get frames during the duration of the loop to draw updates.

    That said, there are generally many ways to optimize loops to speed them up, either by reducing the number of loops, lowering the amount of data that needs to be checked by conditions, reducing the actions taken per loop, ect.

  • Thanks 99Instances2Go, the plugin looks great. Hard to adapt an already existing project, but promising !

    Also thank you oosyrag for the advices.

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