Screeps:在creeps内存中存储对source的引用?

bit*_*ter 9 javascript screeps

迷人的游戏!

寻找一个如何在creep的内存中持久引用特定能源的示例.似乎存储实际的源对象将不起作用(?).

art*_*tch 14

您无法存储对象实例,但可以存储其ID.

if(!creep.memory.targetSourceId) {
    var source = creep.pos.findNearest(Game.SOURCES_ACTIVE);
    creep.memory.targetSourceId = source.id;
}  
Run Code Online (Sandbox Code Playgroud)

然后你可以Game.getObjectById()用来找到这个特定的来源.

var source = Game.getObjectById(creep.memory.targetSourceId);
creep.moveTo(source);
Run Code Online (Sandbox Code Playgroud)