Nero529
Nero529

Reputation: 21

How to build construction sites in another room in Screeps?

I just recently started attacking some code for controlling 2 rooms at a time. I claimed another room and have a creep sitting in it at all times. I use this for determining construction sites in all rooms:

for(let i = 0; i < numberOfRooms; i++)
    amountOfConstructionSites[i] = Game.rooms[roomName[i].name].find(FIND_CONSTRUCTION_SITES);

amountOfConstructionSites[0] contains the spawn from a different room and is visible when using console.log(), but whenever I try to use something like console.log(amountOfConstructionSites[0].room.name), it returns undefined. Another note to add is that the room does appear in Game.rooms, so I do have vision in the room. Would really like some help so I can keep expanding to other rooms

Upvotes: 1

Views: 471

Answers (1)

Nero529
Nero529

Reputation: 21

I figured out the answer after I wrote code that was very similar to this. Game.rooms[roomName[i].name].find(FIND_CONSTRUCTION_SITES) returns an array of construction sites. What I wrote was storing an array within one element of another array, without looping through that new array. It was returning undefined in the same way that you would get if you didn't loop through a normal one-dimensional array. My remedy was as simple as this:

for(let i = 0; i < numberOfRooms; i++)
{
        amountOfConstructionSites = Game.rooms[roomName[i].name].find(FIND_CONSTRUCTION_SITES);
}

This solves two problems that I was facing with this other part of code I was writing. The game limits your code with a CPU limit, so by storing an array of construction sites for each room that you have control of, you'd be using a large amount of this limit instead of using it elsewhere.

Upvotes: 1

Related Questions