[solved] How to find containers ? - when assign and concat don't mix
-
Hi all,
I have the same problem as the author of this post :
http://screeps.com/forum/topic/945/CONTAINER-not-found
However, I have already written it correctly (well, obvioulsy not in fact), yet it does not work :
buildings.concat(
Object.assign(
[],
creep.room.find(
FIND_STRUCTURES,
{filter : (s) => {return s.structureType == STRUCTURE_CONTAINER}}
)
)
);
// buildings are filled first with spawns and extensions and only then containers are added to it.
// so iterating over buildings, the first buildings will be spawns and extensions.
assert(buildings.length == 7, 'container not detected in hauler AI');what is wrong with it ?
Could someone explain to me the subtleties of thes evasive unowned structures ?
-
Nah, forget it : apparently concat() and Object.assign() don't mix very well... I really don't know why, but it did. That is... a bit depressing though when things that should work don't ...
anyway... hope it can be of use to someone.
-
hi,
I guess the problem is, assign is intended to use with objects { }, not arrays [ ]. doc
-
I somehow don't see what you use Object.assign for. Wouldn't the following do what you want?
buildings.concat(creep.room.find( FIND_STRUCTURES, {filter : (s) => {return s.structureType == STRUCTURE_CONTAINER}} );
-
Well,
I just got out of bed, so I can't tell you why exactly I used Object.assign beside that I remember observing on objects returned byt the game, that their type were not nececarily basic type[*], thus leaving some method calls with 'undefined is not a function'... that is when its returned value is assigned to a variable, otherwise, no error raised, and hell that is an unwelcomed change from Python or C#. Anyway, now I remember why I used it : I just didn't remebember if the returned object of creep.room.find were a real Array, I still have to remember how to test the type of an array, instead of 'typeof []', whom return value is not helpful [Object object] or something...
By the way, here is the code right now - will be slightly modified to handle cases where there is no containers - with the preceding section, you will notice that I also used Object.assign with no problems whatsoever in the first part:
let buildings = Object.assign([],creep.room.find(FIND_MY_STRUCTURES, {
filter: function(structure){
return (structure.structureType == STRUCTURE_EXTENSION || structure.structureType == STRUCTURE_SPAWN);
}}));
assert(buildings.length > 0, 'spawns and extensions not detected in hauler AI');
let lenBuildings = buildings.length;
let tmp = creep.room.find(
FIND_STRUCTURES,
{filter: (s) => s.structureType == STRUCTURE_CONTAINER}
);
assert(tmp[0].structureType == STRUCTURE_CONTAINER);
buildings = buildings.concat(tmp);
// buildings are filled first with spawns and extensions and only then containers are added to it.
// so iterating over buildings, the first buildings will be spawns and extensions.
assert(buildings.length > lenBuildings, 'container not detected in hauler AI');
assert(buildings[lenBuildings].structureType == STRUCTURE_CONTAINER);[*]:but maybe these observations were just the fruits of tireness...