当我遍历它时,Screeps Costmatrix 不显示除 0 以外的任何值

Screeps Costmatrix not showing any value except 0 when I iterate through it

我正在创建一个成本矩阵,并设置特定的成本值(我的控制台日志正在触发),但是当我读取数据时我什么也没得到(控制台中没有结果)我在这里做错了什么?

创建成本矩阵

let cm = new PathFinder.CostMatrix();
let f = creep.room.find(FIND_STRUCTURES);

//10x10 radius of tower slightly unwalkable
f.filter(s => s.structureType == STRUCTURE_TOWER).forEach(r => {
  for (let i = -10; i < 10; i++)
    for (let j = -10; j < 10; j++) {
      cm.set(r.pos.x + i, r.pos.y + j, 5);
      console.log(`updated cm`) ////////////console log when setting values
    }
});
creep.room.memory.avoidTowerMatrix = cm;

从成本矩阵读取数据时

//convert it to an instance of a costmatrix from an object
  let x = PathFinder.CostMatrix.deserialize(creep.room.memory.avoidTowerMatrix as any);
  for (let i = 1; i < 50; i++)
    for (let j = 1; j < 50; j++)
      if (x.get(i, j) > 0) console.log(`high cost square`) /////not showing up

当您尝试反序列化一个实际的 CostMatrix,而不是它的序列化版本时,您可能会收到反序列化错误

来自文档

PathFinder.CostMatrix.deserialize(val)

| parameter | type   | description                 |
----------------------------------------------------
| val       | object | Whatever serialize returned |

您需要在将其存储到内存之前对其进行序列化

creep.room.memory.avoidTowerMatrix = cm.serialize();