Trouble with .find



  • var towers = Game.Room.find(FIND_MY_STRUCTURES, {filter: {structureType: STRUCTURE_TOWER}});

    and

    var towers = Game.rooms[roomName].find(FIND_MY_STRUCTURES, { filter: { structureType: STRUCTURE_TOWER } });

    keeps throwing Room.find is not a function errors and I can't figure out why.



  • If the exact error text is Room.find then likely as your first example is showing, you need to have Game.rooms['RoomNameStringHere'].find();

    specifically, find() needs to be called on a room object, so you could also do:

    let myRoom = Game.rooms[RoomNameGoesHere];
    let myTowers = myRoom.find(FIND_MY_STRUCTURES, {filter: {structureType: STRUCTURE_TOWER}});

    Would recommend as well if you haven't yet, checking out slack, very rapid response most of the time and more dynamic.



  • @donatzor said in Trouble with .find:

    If the exact error text is Room.find

    let room = _.filter(Game.rooms, function(room){ return room.controller && room.controller.my; })[0];
    let towers = _.filter(room.find(FIND_MY_STRUCTURES), function(structure) { return structure.structureType == STRUCTURE_TOWER; });
    console.log(room, towers.length);
    

    There's plenty of ways / styles of getting / using the room name, as an example. Gets first (not necessarily in claiming order) room and towers under users control.

    @gea said in Trouble with .find:

    keeps throwing Room.find is not a function errors and I can't figure out why.

    let room = _.filter(Game.rooms, function(room){ return room.controller && room.controller.my; })[0];
    var towers = room.find(FIND_MY_STRUCTURES, {filter: {structureType: STRUCTURE_TOWER} });
    console.log(room, towers.length);
    

    Could be a solution.