Problems with finding containers



  • Hi, I'm having some problems to get some code work. I need to make an array, and get the lenght of how many full containers do I have on the creep's room, so I use this code:

    var storePlaces = creep.room.find(FIND_STRUCTURES, {
                filter: (i) => {return
                    (i.structureType == STRUCTURE_CONTAINER &&
                    i.store[RESOURCE_ENERGY] == 2000)}}).length;

     

    using that code in the console results as expected, using Game.creeps['creepname'].room...,

     

    But when I also make: creep.memory.places = storePlaces , it always gets 0 on the creeps memory, so I can't use that code. Any ideas?



  • You can'y store live objects in Memory, store the id of game objects.

    something like:

     

    var newstorePlaces = new Array();

     

    for (var i in storePlaces) {

    var sp = storePlaces[i];

    newstorePlaces.push(sp.id);

    }

     

    creep.memory.places = newstorePlaces;

     

    and then Game.getObjectById() to get the objects back again.

     



  • So, that explains why I get 0 in the memory, but not why my code isn't working as expected. I only use that variables to seek the values while executing.

    Now I tried to execute most of the code line by line in the console and works exactly as expected, but I have found something interesting.

    once storePlaces.lenght returns 3, for example, I use this to change the value of the var targets with this code:

    if (targets == null && storePlaces > 1) {
               targets = creep.room.storage;
            }

    This code makes creeps to do nothing, but this one starts working as expected.

    if (targets == null /*&& storePlaces > 1*/) {
               targets = creep.room.storage;
            }

    Also if I make :

    if (targets == null && storePlaces > -1) {
               targets = creep.room.storage;
            }

    creep works again, so I can guess that the problem is that

    var storePlaces = creep.room.find(FIND_STRUCTURES, {
                filter: (i) => {return
                    (i.structureType == STRUCTURE_CONTAINER &&
                    i.store[RESOURCE_ENERGY] == 2000)}}).length;

    Always return 0 in the script, but the correct value if use in the console



  • room.find() returns an array of objects.

    You can't (shouldn't) store objects in memory.  Instead, store IDs and then dereference them.

    Try something like this

    var storePlaces = // same as your code

    // To store that result in memory (store ONLY the ID of each place)
    Memory.storePlaces = _.map(storePlaces, sp => sp.id);

    // To read that result from memory elsewhere in code (presumably in a subsequent tick)
    var storePlaces = _.map(Memory.storePlaces, id => Game.getObjectById(id));

     



  • But when i make

    var storePlaces = creep.room.find(FIND_STRUCTURES, {
                filter: (i) => {return
                    (i.structureType == STRUCTURE_CONTAINER &&
                    i.store[RESOURCE_ENERGY] == 2000)}}).length;

    I'm storing just a number, exactly, the lenght of the .find array; not an object.



  • Ah, I see.  I misunderstood your OP.

    All I can think is that what you're pasting into the console is not identical to what you're running in the script, as there is no reason that it should behave differently.

    When you type a console command, the server essentially adds that to the end of your script and runs it at the end of the tick.



  • just make Ctrl+C Ctrl+V and change creep. for Game.creeps('nameOfACreep').



  • I was using

       var roomContainers = Game.rooms[creep.room.name].find(FIND_MY_STRUCTURES, { filter: (structure) => { return ((structure.structureType == STRUCTURE_CONTAINER)) } });


                        console.log("Containers: "+ roomContainers.length);

     

    Apparently Containers arent in the MY.

    Once I swapped to FIND_STRUCTURES it worked