Free [Source] Fake Async Loop System

Binsk

Member
This was an idea I had at the back of my mind for a while and so I whipped something up really quick and wanted to share it.

You can download the project with the script and object here.

What this does is allows you to fake asynchronous behavior with long computation-heavy loops. I kept the task sorting extremely simple and, should you stack multiple tasks, it will merely run a cycle for each, so long as there is time, top-to-bottom of the list (sorted via a specified priority).

It will attempt to run as many of the tasks as it can in the allotted time before pausing and waiting for the next step to proceed. This makes dynamic loading screens and such a breeze for these kinds of things.

The format is basically as such. Let's say you had this loop for some procedural generation:
Code:
// Create Event:
for (var i = 0; i < 256; ++i)
  for (var j = 0; j < 256; ++j)
    heightmap[i, j] = calculate_simplex(i, j);
Instead, you could generate an async task, one for each i value, where the task would calculate all the j values for it's row. Like this:
Code:
// Create Event:
for (var i = 0; i < 256; ++i)
    async_loop_create(calculate_simplex_wrapper, 0, 257, i);

// calculate_simplex_wrapper
calculate_simplex(argument0[ASYNC_PACKAGE.data1], argument0[ASYNC_PACKAGE.i_current]);
The system will then calculate through one "cycle" of as many loops as possible without lagging the game. If it can make multiple cycles in a step, it will. If not then it will proceed on the next step.

You can tweak the number of milliseconds permitted to be taken up by these loops per second via the variable _msSCap in the asyncControllerObj object. By default it is set to 1000 (the whole second).

Note, everything is estimated and isn't perfect. The "estimated cost" for each task is rough and takes an initial calculation to figure. Also, if a single computation is estimated to take an entire step or more to calculate, there is no way to split it and there will still be lag.

Anyway, there is only one script and one object. I kept it simple but also made it easy to edit in case you wanted to add features. Hopefully it will help someone out. Cheers.
 
Last edited:
Top