2017-06-25 19 views
1

將存儲在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; 

似乎失敗一次爬行移動。這有什麼原因會發生?我應該做什麼不同?

回答

1

這花了我很長的時間來弄清楚,但一旦你知道什麼是錯的,這是有道理的,爲什麼它不工作。

您無法將遊戲對象存儲在內存中。

我看到其他人也使用我的替代方法,而不是存儲ID。

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

後來

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