creep.memory.role



  • I am getting an error:
    main:20
    if (creep.memory.role == "miner") {
    ^TypeError: Cannot read property 'role' of undefined
    at main:20:25

    I recently expanded to a new room and I am trying to adapt my code to work with two rooms.
    However, I am running into issues.
    THis is my code in main:

    for (var spawnName in Game.spawns) {
        var spawn = Game.spawns[spawnName];
        cm.checkForNewSpawn(spawn);
        var creeps = _(Game.creeps).filter({
            memory: {
                room: spawn.room.name
            }
    });
    for (var name in creeps) {
        console.log("name " + name);
        var creep = Game.creeps[name];
        if (creep === undefined) {
            continue;
        }
        console.log(creep.name);
        if (creep.memory.role == "miner") {
            cm.harvest(creep, spawn);
        } else if (creep.memory.role == "builder") {
            cm.build(creep, spawn);
        } else if (creep.memory.role == "runner") {
            cm.transfer(creep, spawn);
        } else if (creep.memory.role == "struct") {
            cm.repair(creep, spawn);
        } else {
            creep.moveTo(creep.room.controller);
            creep.claimController(creep.room.controller);
        }
    

    When I create a creep I set in its memory 'room' to equal the room's name that the spawnpoint which created the creep is in.

    That way I can keep track of my creeps by the room.

    So, I get the list of all creeps (Game.creeps), and filter it (with lodash) by 'memory: { room: "roomname" } So, it should return only the creeps within the room of the current spawn.

    However, when i then assign the creep to a variable and try to use it. I get all sorts of errors.

    Like, when I try to print the name of the creep I got all sort of weird messages instead like "___constructor___"

    Any idea what I am doing wrong?



  • var creeps = _(Game.creeps).filter({
    memory: {
    room: spawn.room.name
    }
    I am pretty sure that this already gives you a list of Creep objects, not their names