main script runs outside of the loop



  • Can anyone explain me why is A called outside of the main loop? It happens periodicaly.

     

    module.exports.loop = function () {
        console.log("MAIN START");
        Game.test = test;//for console commands like "Game.test()";
        console.log("MAIN FINISH");
    };

    var A = (function(x, y) {
        console.log("A START");
        var B = function(x, y) {
            console.log("B START");
            console.log(y);
            console.log("B FINISH");
        }
        
        console.log("A RETURNS B");
        return B;
    })();

    var test = function() {
        console.log("TEST START");
        A(1, 2);
        return "TEST FINISH";
    };

    /* CONSOLE LOG

    Game.test()
    [11:04:33 AM]
    MAIN START
    [11:04:33 AM]
    MAIN FINISH
    [11:04:33 AM]
    TEST START
    [11:04:33 AM]
    B START
    [11:04:33 AM]
    2
    [11:04:33 AM]
    B FINISH
     
    TEST FINISH
    [11:04:34 AM]
    MAIN START
    [11:04:34 AM]
    MAIN FINISH
    [11:04:35 AM]
    MAIN START
    [11:04:35 AM]
    MAIN FINISH
    [11:04:36 AM]
    MAIN START
    [11:04:36 AM]
    MAIN FINISH
    [11:04:37 AM]
    A START
    [11:04:37 AM]
    A RETURNS B
    [11:04:37 AM]
    MAIN START
    [11:04:37 AM]
    MAIN FINISH
    [11:04:38 AM]
    MAIN START
    [11:04:38 AM]
    MAIN FINISH
    [11:04:39 AM]
    MAIN START
    [11:04:39 AM]
    MAIN FINISH
    [11:04:40 AM]
    MAIN START
    [11:04:40 AM]
    MAIN FINISH
    [11:04:41 AM]
    MAIN START
    [11:04:41 AM]
    MAIN FINISH
    [11:04:42 AM]
    MAIN START
    [11:04:42 AM]
    MAIN FINISH
    [11:04:43 AM]
    MAIN START
    [11:04:43 AM]
    MAIN FINISH
    [11:04:44 AM]
    MAIN START
    [11:04:44 AM]
    MAIN FINISH
    [11:04:45 AM]
    MAIN START
    [11:04:45 AM]
    MAIN FINISH
    [11:04:46 AM]
    MAIN START
    [11:04:46 AM]
    MAIN FINISH
    [11:04:47 AM]
    MAIN START
    [11:04:47 AM]
    MAIN FINISH
    [11:04:48 AM]
    A START
    [11:04:48 AM]
    A RETURNS B
    [11:04:48 AM]
    MAIN START
    [11:04:48 AM]
    MAIN FINISH
    */

  • Culture

    The "outside of the main loop" script is called when the script is loaded.

    This is normal behavior of the game.

    When committing code you can actually see this behavior quite clearly, just add a console.log("compile script"); on the top of your script.

    The first few ticks it will show "compile script", in later ticks it might show up randomly depending on the server-side cache.



  • Dissi, you mean "outside of the main loop" is loading from time to time on server? not just one time when i compile it with Ctrl+S or something?


  • Culture

    Everything outside the loop is ran at random intervals when the server loads your code onto a new shard/server:

     

    console.log('run when loaded, at random times, at least once when you commit your script')

    module.exports.loop  = {
    console.log('run every tick ' + Game.time);
    }

     

     

    It's the way the server caches your require()'s as well.



  • Thank you, thats all i wanted to know.)



  • The caching appears to happen once every 30 ticks or so. I that helps you in any way.