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

I am creating a cost matrix, and setting specific cost values (my console log is firing), but when I read data I get nothing (no results in console) what am I doing wrong here?

creating a cost matrix

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;

when reading data from the costmatrix

//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


Solution 1:[1]

You're likely getting an error from deserialized as you're trying to deserialize an actual CostMatrix, not a serialized version of it

From the docs

PathFinder.CostMatrix.deserialize(val)

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

You're going to want to serialize it before storing it in memory

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Fiskmans