Code outside any loop.

  • I want to execute code outside any game loop. It should execute only once:

    var transferToController = [];
    var harvestEnergy = [];
    for(var i = 0; i < maxNumberOfCreeps; i++)

    It should be possible that during the game, certain variables should change but be initialized only once at the beginning outside any game loop.

  • Add code you want to execute outside any loop in you main file outside of the exports.loop function.

    let i = 10;
    console.log('only once', i);
    exports.loop = function() {
      console.log("everytick", i);

  • @deft-code

    The problem is, that also outside the exports.loop function, the code is executed from time to time. Not every tick but I think every 10 ticks.

  • @hacknet yes, code outside the loop is executed every time your code is re-initialized globally. This is down to the way screeps code is running, and is called "global reset". You must work around that fact that it happens from time to time. Judging by the fact that you said it's happening every like 10 ticks, you're likely running it in sim. Simulation works a little bit differently from actual MMO server in that it resets globals a lot more often. On the actual MMO server due to the new IVM code your globals can often last for tens of thousands of ticks at once, but you still have to consider they will get reset once in a while.

    If you absolutely want some of your code to only execute once and then never, you'll have to resort to using Memory flags for a permanent trigger.

  • YP

    if you want code only run exactly once you could save a flag in Memory for example.