javascript - Screeps - 内存中的属性重置

标签 javascript screeps

我的内存有问题。它应该存储我的所有数据(而不是对象),但它会不时重置。 这是示例:

Memory.creeps_idx = 0;   

module.exports.loop = function () {

    console.log("c_idx:", Memory.creeps_idx, Game.time);

    if(Memory.creeps_idx==0)
    {
        Memory.creeps_idx+=1;
    }    
}

这是控制台的输出。

[13:41:01]c_idx: 0 1115
[13:41:03]c_idx: 1 1116
[13:41:05]c_idx: 0 1117
[13:41:07]c_idx: 1 1118
[13:41:09]c_idx: 1 1119
[13:41:11]c_idx: 1 1120
[13:41:13]c_idx: 1 1121
[13:41:15]c_idx: 1 1122
[13:41:17]c_idx: 0 1123
[13:41:19]c_idx: 1 1124
[13:41:21]c_idx: 1 1125
[13:41:23]c_idx: 1 1126
[13:41:25]c_idx: 1 1127

你能告诉我为什么 creeps_idx 将值更改为 0 吗? (这是在模拟器中测试的)

最佳答案

基本上,module.exports.loop 之外的任何内容都会每隔几个周期运行一次。这就是为什么 cree_idx 在原始示例中被重置,以及您的解决方案有效的原因。

What is new here is the way you main module is executed. In the old-style architecture, it is executed every tick, and every module required from it is also executed every tick. In the new architecture, your main module should contain the module.exports.loop function which is executed in a game loop, while the rest of the main module contents are executed only after a new global is instantiated.

http://support.screeps.com/hc/en-us/articles/204825672-New-main-loop-architecture

关于javascript - Screeps - 内存中的属性重置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43963772/

相关文章:

javascript - 日期以字符串格式出现在 json 对象中。我想要它的日期格式,因为我想显示它

javascript - 有没有办法删除小兵?

javascript - 游戏 Screeps - creep.room.find(Game.CONSTRUCTION_SITES);不工作

javascript - findNearest, findInRange - 如何在 Screeps 中使用?

screeps - C.Game.structs 中缺少墙壁和道路

javascript - 使嵌套异步函数嵌套同步函数

javascript - 无法在 Internet Explorer 11 中执行 Javascript 执行器

javascript - 如何在数据网格中显示由具有属性和值的对象组成的数组?

javascript - React Router 和任意查询参数 : Page Refreshes Unintentionally on Load?

javascript - 如何在 screeps 中扩展 creep 类