Nooby issues trying to get my creeps to move by a path written to memory



  • Hey all,

    Forgive me for my nooby question. I'm trying to define a path from my spawn to a source in an attempt to conserve cpu. I'm not getting an error, however my screeps are not moving. This is my first script im trying to write outside of the tutorial, so forgive me as im sure there are a bunch of nooby problems with it. Idk if I can call a creep to "moveByPath" the way I am. Or do I need to serialize the path first? I'm very sorry if this is a nooby question, if I need to give more detail just let me know! 🙂

    module.exports.loop = function () {
    var harvesterId = _.filter(Game.creeps, (creep) => creep.memory.role == 'drone'); 
    var harvesterRun = require('harvesters'); 
    var controllerLevel = StructureController.level;
    var newName = Game.time;
    var spawn1 =  Game.spawns['Spawn1']
    
    //Find sources from spawn, to be written into the memory of creeps later
    var sources = Game.spawns['Spawn1'].room.find(FIND_SOURCES);
    var source1 = sources[1].id; // Create two variables from the list "sources" defined above
    var source2 = sources[2].id; 
    
    //create flags and then path to them from spawn. then serialize into string that can be stored into memory
    spawn1.room.createFlag(sources[1].pos, 's1');
    spawn1.room.createFlag(sources[2].pos, 's2');
    const sourceFlag1 = Game.flags.s1;
    const sourceFlag2 = Game.flags.s2;
    const sourcePath1 = spawn1.pos.findPathTo(sourceFlag1);
    const sourcePath2 = spawn1.pos.findPathTo(sourceFlag2);
    //Opposite of above, searlize path from source to spawn to conserve cpu
    spawn1.room.createFlag(spawn1.pos, 'sp1');
    const spawnFlag1 = Game.flags.sp1;
    const spawnPathFromSource1 = sourceFlag1.pos.findPathTo(spawnFlag1);
    const spawnPathFromSource2 = sourceFlag2.pos.findPathTo(spawnFlag1);
    
    //Spawns
    if(harvesterId.length < 4) {
        spawn1.spawnCreep([WORK,CARRY,MOVE], newName, {memory: {role: 'drone', sp1: sourcePath1, sp2: sourcePath2, spawnPath1: spawnPathFromSource1, spawnPath2: spawnPathFromSource2, mine1: source1, mine2: source2, creepName: newName}}); //Create a creep if there arent enough. role "drone" with two sources defined as "mines" written to memory        
    };
    
    
    
    for(var name in Game.creeps) {
        var creep = Game.creeps[name];
        if(creep.memory.role == 'drone') {
            harvesterRun.run(creep);
        }
    }
    

    };

    (Below is a second document running on creeps with the "harvester" role in their memory)

    var roleHarvester = {

    /** @param {Creep} creep **/
    run: function(creep) {
        var freeCap = creep.store.getFreeCapacity(RESOURCE_ENERGY);
        var totalCap = creep.store.getCapacity;
        //var roomSpawn = creep.room.find(FIND_MY_STRUCTURES, { filter: { structureType: STRUCTURE_SPAWN }})
        var roomSpawn =  Game.spawns['Spawn1'] //Gross workaround as the code above doesnt work. Ideally in the future it isn't identified by name but rather but something like above. Maybe it can even be written to memory?
        var mine1 = Game.getObjectById(creep.memory.mine1);
        console.log(creep.memory.creepName+' '+freeCap)
        console.log('completely open?'+freeCap == totalCap)
        if(freeCap == 0){
            if(creep.transfer(roomSpawn, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
                creep.moveTo(roomSpawn)
            }
        }
        if(freeCap != 0){
            if(creep.harvest(mine1) == ERR_NOT_IN_RANGE) {
                creep.moveByPath(Memory.sourcePath1)
            }
        }
    }
    

    };

    module.exports = roleHarvester;



  • Greetings,

    Even without a generated error, functions in screeps will return an ERR_ code or an OK when run, this assists in the debugging process. So your creep.moveByPath(Memory.sourcePath1) should be returning a code other than OK.

    https://docs.screeps.com/api/#Creep.moveByPath has a table showing all the codes that can return, and what they mean.

    My guess is that you are calling Memory.sourcePath1 instead of creep.memory.sp1 * because from the first code, it looks like you set the paths to the creep's memory, not just the Memory object itself (unless you've done this somewhere not displayed in your post)

    As a side note, if the creep does not start on/adj to a step on your path (depending on how your path was generated) then you will likely get ERR_NOT_FOUND as they moveByPath() fails to find where it is on the path.

    Would recommend checking out Screep's Discord server, its a bit more dynamic and faster to get assistance, normally some one around can help in #world-help.

    If you don't like discord, would recommend checking out:

    https://docs.screeps.com/debugging.html

    https://wiki.screepspl.us/index.php/Basic_debugging

    https://wiki.screepspl.us/index.php/A_brief_guide_to_reading_the_Screeps:World_API_Documentation