1

我在让存储在cree内存中的位置工作时遇到了一些麻烦。例如,这段代码:

var sources = creep.room.find(FIND_SOURCES);
if(creep.memory.sourceLocationIndex == null) {
    creep.memory.sourceLocationIndex = Math.floor(Math.random() * sources.length);
}
return sources[creep.memory.sourceLocationIndex];

完美运行,但这段代码:

if(creep.memory.sourceLocation == null) {
    var sources = creep.room.find(FIND_SOURCES);
    var sourceLocationIndex = Math.floor(Math.random() * sources.length);
    creep.memory.sourceLocation = sources[sourceLocationIndex];
}
return creep.memory.sourceLocation;

一旦小兵移动,似乎就失败了。发生这种情况有什么原因吗?我应该做些什么不同的事情?

4

2 回答 2

1

这花了我很长时间才弄清楚,但是一旦你知道出了什么问题,它为什么不起作用就很有意义了。

您不能将游戏对象存储在内存中。

我看到其他人也使用的替代方法是存储 ID。

creep.memory.sourceId = sources[sourceLocationIndex].id;

然后

var source = Game.getObjectById(creep.memory.sourceId);
于 2017-10-20T17:54:31.553 回答
0

bame53 在我的 Reddit 帖子中给出了一个很好的答案: https ://www.reddit.com/r/screeps/comments/6jfjob/locations_not_valid_when_stored_in_memory/djdzgn1/

于 2017-06-25T19:45:48.587 回答