javascript - Game Screeps - creep.room.find(Game.CONSTRUCTION_SITES); not working - Stack Overflow

I have the following code from the tutorial, except in simulation mode it isn't working..if(creep

I have the following code from the tutorial, except in simulation mode it isn't working..

if(creep.memory.role == 'builder') {

    if(creep.energy === 0) {
        creep.moveTo(Game.spawns.Spawn1);
        Game.spawns.Spawn1.transferEnergy(creep);
    }
    else {
        var targets = creep.room.find(Game.CONSTRUCTION_SITES);
        if(targets.length) {
            creep.moveTo(targets[0]);
            creep.build(targets[0]);
        }
    }
}

Printing the result of "targets" in the console, responds undefined. Can anyone explain how the "CONSTRUCTION_SITES" constant is working and how to find them inside the room?

I have the following code from the tutorial, except in simulation mode it isn't working..

if(creep.memory.role == 'builder') {

    if(creep.energy === 0) {
        creep.moveTo(Game.spawns.Spawn1);
        Game.spawns.Spawn1.transferEnergy(creep);
    }
    else {
        var targets = creep.room.find(Game.CONSTRUCTION_SITES);
        if(targets.length) {
            creep.moveTo(targets[0]);
            creep.build(targets[0]);
        }
    }
}

Printing the result of "targets" in the console, responds undefined. Can anyone explain how the "CONSTRUCTION_SITES" constant is working and how to find them inside the room?

Share Improve this question edited Jul 14, 2021 at 14:40 Wenfang Du 11.5k13 gold badges76 silver badges114 bronze badges asked Nov 21, 2014 at 18:56 webecreativewebecreative 1971 gold badge3 silver badges11 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

The code looks right to me, it seems that you don't actually have any constructions set for the builders to build. Did you use the "Construct" menu on the left to build a road, for example?

I don't know if this has been answered yet, or if it's just that the syntax from the game has changed which is why you initially wrote it this way, but this code works for what you're doing:

var targets = creep.room.find(FIND_CONSTRUCTION_SITES);

In Screeps console.log() always prints undefined. In line before undefined you can find printed value:

> console.log(Game)
[hh:mm:ss] [object Object]
< undefined

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742315526a4420762.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信