New main loop architecture

We have just implemented an important feature which affects how the runtime engine treats your script modules. Now we have two module architectures available at the same time.

What is new here is the way you main module is executed. In the old-style architecture, it is executed every tick, and every module required from it is also executed every tick. In the new architecture, your main module should contain the module.exports.loop function which is executed in a game loop, while the rest of the main module contents are executed only after a new global is instantiated. It's the same with required modules - their contents are executed only once per global instantiation, after which they get cached. You have to export some functions from your required module and run it explicitly from the main loop. The architecture is switched automatically upon detecting module.exports.loop function in the main.

Here is a comparison table:

OLD NEW

main:

// executed every tick

var mod = require('mod');

mod.foo();


main:

// executed on new global 
var mod = require('mod');

module.exports.loop = function() {
    // executed every tick
    mod.foo();
}

mod:

// executed every tick

module.exports.foo = function() {
    // executed every tick
}

mod:

// executed on new global

module.exports.foo = function() {
    // executed every tick
}

This new style is now recommended. It allows caching of your modules which drastically affects your script performance. Also, you can perform some initial operatons in your modules body (e.g. extending prototypes) which won't be executed every tick, thus saving your CPU.