Search code examples
screeps

Is Creep.moveTo() a non-blocking method?


        // Creep has Energy packed
        creep.say('E: ' + creep.carry.energy);
        if (creep.carry.energy > 0) {
            creep.moveTo(creep.room.controller);
            creep.upgradeController(creep.room.controller);
        }
        // Creep has no Energy
        else {
            creep.moveTo(Game.spawns.Spawn1);
            Game.spawns.Spawn1.transferEnergy(creep, creep.carryCapacity);
            creep.moveTo(creep.room.controller);
            creep.upgradeController(creep.room.controller);
        }

The above code should send a creep to upgrade a controller. When it has no energy, it shall go to the spawn and get some. But instead of moving to the spawn, it stays at the controller.

Question: Which command is cancelling creep.moveTo(Game.spawns.Spawn1);?

Do I have to use the creep's Memory and add a kind of state like isMoving: true and keep track of that?


Solution

  • That is exactly what you command the creep to do. The third last line changes the moveTo to the controller. Delete this and the following line.

    If you send multiple moveTo to the creep, it executes only the last one. Because it overrides the moveTo commands before.