存储在内存中的位置无效?

时间:2017-06-25 02:46:48

标签: screeps

我在将存储在creep内存中的位置工作时遇到了一些麻烦。例如,此代码:

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;

蠕动移动后似乎失败了。这有什么原因会发生吗?我应该做些什么?

2 个答案:

答案 0 :(得分:1)

这花了我很长时间才弄明白,但是一旦你知道什么是错的,那么它为什么不起作用是有道理的。

您无法将游戏对象存储在内存中。

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

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

以后

var source = Game.getObjectById(creep.memory.sourceId);

答案 1 :(得分:0)

bame53在我的Reddit帖子中给出了一个很好的答案: getValue(Class<T>)