Game code not loading? I need to re-save it for it to actually work...



  • So, I'm having this strange problem where, my code doesn't get run in the game... unless I re-save the file(basically I just refresh). No changes made.

    I literally just add a single space, save again, AND MAGICALLY EVERTHING WORKS!

    At first I thought the reason for this was that I had a few things defined outside of the module.exports.loop, and thus not being executed every tick. So what I've done is, I've put the array I was using( that needs to persist tick to tick) inside Memory, and changed stuff around so that outside of the loop there's only the definition of basically a class, but the object to this class is created inside the loop.

     

    Why, I've been smashing my head for hours, I can't understand it.... Here's the code... :

     

    var mainSpawner = Game.spawns['Main Spawner'];
    var mainRoom = Game.rooms['E22S23'];
    Memory._queue = [];

    var roleUpgrader = require('role.upgrader');
    var roleHarvester = require('role.harvester');
    var roleBuilder = require('role.builder');
    var roleDefender = require('role.defender');
    var roleTower = require('role.tower');

    var creepEnum = {
    HARVESTER: 0,
    UPGRADER: 1,
    BUILDER: 2,
    DEFENDER: 3
    };

    function creep(creepType, creepCost)
    {
    this.type = creepType; this.cost = creepCost;
    };

    function popCreepFromQueue(creep, queue)
    {
    if(queue[queue.length-1] == creep.type)
    { queue.pop(); } else {console.log("Creep passed on pop wasn't last creep on stack");}
    };

    function _spawnManager()
    {
    this.energy = 0;

    this.maxHarvesters = 0;
    this.maxUpgraders = 0;
    this.maxBuilders = 0;
    this.maxDefenders = 0;

    this.harvesterCost = 250;
    this.upgraderCost = 350;
    this.builderCost = 350;
    this.defenderCost = 330;

    this.isAttacked = false;
    this.controllerFlag = 0;
    };

    _spawnManager.prototype.processQueue = function(queue)
    {
    var i = queue.length, c;
    c = queue[i-1];
    switch(c)
    {
    case creepEnum.HARVESTER:
    { mainSpawner.createCreep([WORK, WORK, CARRY, MOVE, MOVE], undefined, {role: 'harvester', isWorking: false});
    popCreepFromQueue(c, queue);
    break; };

    case creepEnum.UPGRADER:
    { mainSpawner.createCreep([WORK, WORK, CARRY, MOVE, MOVE], undefined, {role: 'upgrader', isWorking: false});
    popCreepFromQueue(c, queue);
    break; };

    case creepEnum.BUILDER:
    { mainSpawner.createCreep([WORK, WORK, CARRY, MOVE, MOVE], undefined, {role: 'builder', isWorking: false});
    popCreepFromQueue(c, queue);
    break; };

    case creepEnum.DEFENDER:
    { mainSpawner.createCreep([ATTACK, RANGED_ATTACK, MOVE, MOVE], undefined,
    {role: 'defender', defendsController: ((this.controllerFlag++ % 2) == 0) ? true : false});
    popCreepFromQueue(c, queue);
    break; };
    }
    };

    _spawnManager.prototype.checkQueue = function(queue, currMax)
    {
    if((currMax.harvester < this.maxHarvesters) && (this.energy >= this.harvesterCost))
    {
    var c = new creep(creepEnum.HARVESTER, this.harvesterCost);
    queue.push(c.type);
    }
    if((currMax.upgrader < this.maxUpgraders) && (this.energy >= this.upgraderCost))
    {
    var c = new creep(creepEnum.UPGRADER, this.upgraderCost);
    queue.push(c.type);
    }
    if((currMax.builder < this.maxBuilders) && (this.energy >= this.builderCost))
    {
    var c = new creep(creepEnum.BUILDER, this.builderCost);
    queue.push(c.type);
    }
    if((currMax.defender < this.maxDefenders) && (this.energy >= this.defenderCost) && this.isAttacked)
    {
    var c = new creep(creepEnum.DEFENDER, this.defenderCost);
    queue.push(c.type);
    }
    };

    _spawnManager.prototype.tickSetup = function(availableEnergy)
    {
    this.energy = availableEnergy;
    this.maxHarvesters = 3;
    this.maxUpgraders = 2;
    this.maxBuilders = 2;
    this.maxDefenders = 4;
    };

    module.exports.loop = function()
    {
    /*TODO: Instead of checking every single tick for cleanup, should track creeps lifetime and cleanup after death*/
    for(var i in Memory.creeps)
    { if(!Game.creeps[i])
    { delete Memory.creeps[i]; } }

    var queue = Memory._queue;
    var spawnManager = new _spawnManager();

    var currMax = {harvester: (_.filter(Game.creeps, function(c) {return c.memory.role == 'harvester';})).length,
    upgrader: (_.filter(Game.creeps, function(c) {return c.memory.role == 'upgrader';})).length,
    builder: (_.filter(Game.creeps, function(c) {return c.memory.role == 'builder';})).length,
    defender: (_.filter(Game.creeps, function(c) {return c.memory.role == 'defender';})).length};

    spawnManager.tickSetup(mainRoom.energyAvailable);

    var hostiles = Game.rooms['E22S23'].find(FIND_HOSTILE_CREEPS);
    if(hostiles.length != 0) { spawnManager.isAttacked = true; }

    if(!mainSpawner.spawning) { spawnManager.checkQueue(queue, currMax); }
    console.log("Queue Length: " + queue.length);
    if(!mainSpawner.spawning && (queue.length != 0)) { spawnManager.processQueue(queue); }

    var i = 0, y = 0, k = 0;
    for(var name in Game.creeps)
    {
    if(Game.creeps[name].memory.role == 'upgrader') {roleUpgrader.run(Game.creeps[name], k); k++;}
    if(Game.creeps[name].memory.role == 'harvester') {roleHarvester.run(Game.creeps[name], i); i++;}
    if(Game.creeps[name].memory.role == 'builder') {roleBuilder.run(Game.creeps[name], y); y++;}
    if(Game.creeps[name].memory.role == 'defender') {roleDefender.run(Game.creeps[name], isAttacked);}

    }

    var towers = mainRoom.find(FIND_MY_STRUCTURES, {filter: function(struct) {return struct.structureType == STRUCTURE_TOWER;}});
    if(towers) { roleTower.run(mainRoom, towers); }

    console.log("CurrHarvesters: " + currMax.harvester + '\n'
    + "CurrBuilders: " + currMax.builder + '\n'
    + "CurrUpgraders: " + currMax.upgrader);

    };


  • If you do several saves in short order. Occasionally the save doesn't go through and saves an empty file or incomplete file. In which case you resave again to fix the issue. This is a long standing bug. Look at a different file and then look back at the offending file to refresh your window to see what is really loaded. The exact code isn't the offender here.

    Long time bug



  • @RaskVann I don't think this is the actual bug causing the problem. Because the problem kept appearing even without making any actual changes to the code. Anyway, after a day of this nonsense I got annoyed and just re-factored all the buggy code. I moved the spawnManager system in its own module and removed the queue system. Now 1 entry -> 1 spawn. No queueing entries, but at least it works fine.