Creep not following moveTo() pathing



  • Having a problem using the moveTo() function in that when giving a creep a position to move to, it'll just go between the same two points that aren't related to its target and sometimes will fix it pathing after making a minute change to the script and saving even though the script was already saved and the change was insignificant (I.E making a comment.) It doesn't even adhere to the pathing it planned when I visualized it.

    Here is a video demonstrating that: https://streamable.com/7qoqkb

    My code is very simple and I don't understand what particularly is wrong with it. Code being:

    Main

    var roleWorker = require("role.worker");
    
    var gameSpawns = Game.spawns;
    var gameCreeps = Game.creeps;
    
    module.exports.loop = function () {
        if(Object.keys(Game.creeps).length < 1){
            gameSpawns["Spawn1"].spawnCreep([WORK,MOVE,CARRY], "worker" + Object.keys(Game.creeps).length);
        }
        
        for(var creep in gameCreeps){
            roleWorker.run(gameCreeps[creep]);
        }
    }
    

    Worker role

    var roleWorker = {
        
        run: function(creep){
            var sources = creep.room.find(FIND_SOURCES);
            
            creep.moveTo(sources[0], {visualizePathStyle:{
        fill: 'transparent',
        stroke: '#fff',
        lineStyle: 'dashed',
        strokeWidth: .15,
        opacity: .1
    }});
        }
        
    }
    
    module.exports = roleWorker;
    


  • forgive the sloppy indentation on the visualizepathstyle option, im in a rush and didn't have time to make it look neat



  • Try passing range: 1 as an option - you don't actually want to move to the source - you want to move within 1 tile of it. Your current code will try to find a path into a wall, which causes issues.



  • @tigga tried range: 1 but still no results. Even tried range 2 but it's still the same.

    Here's a video with range: 1 and pathing visualised: https://streamable.com/iqk38x

    Same as before, updated code looks like:

    var roleWorker = {
        
        run: function(creep){
            var sources = creep.room.find(FIND_SOURCES);
            var creepPath = creep.pos.findPathTo(sources[0]);
            
            creep.moveTo(sources[0], {visualizePathStyle:{
                fill: 'transparent',
                stroke: '#fff',
                lineStyle: 'dashed',
                strokeWidth: .15,
                opacity: .1
            }, range: 1});
        }
        
    }
    
    module.exports = roleWorker;
    


  • move this into the main loop.

    var gameSpawns = Game.spawns;
    var gameCreeps = Game.creeps;
    


  • @explicit said in Creep not following moveTo() pathing:

    move this into the main loop.

    var gameSpawns = Game.spawns;
    var gameCreeps = Game.creeps;
    

    this will be it, yes. since you have it outside the loop, spawns and creeps only get initialized on global resets and not get updated until the next one. this means that the position data on your creeps is the same as on the global reset tick and they end up thinking they havent moved yet. for spawns this could also have side effects like it throwing not enough energy when it's full or spawning when it's not. since it looks like you're playing in sim, you'll have those resets every 10 seconds, fixing the problem and beginning the ordeal anew

    i would also advise against using [0] like this. while certain databases generally return objects in the same order, it is undocumented behavior and not guaranteed. it could also lead to your creeps running back and forth because sources[0] returns a different source suddenly



  • @explicit @RayderBlitz Yeah, that was it. Movement is still kinda funky when there is more than one creep out but at least now it's actually going where it is told.

    Thanks for the help fellas (o゚v゚)ノ