The Problem
So I have a class called Grid
, and this class has a cells
property. The cells
property is a 2d matrix that stores each cell of the grid, where each cell is just a number. I have a function called getCell
that takes in a position (in the format { x, y }) and returns the value of that cell.
Now here is the problem, I have a console line that prints out information for debugging, and all of the sudden I see a contradiction. What I am doing is logging the pos
property, and then I am logging grid.getCell(this.pos)
which returns 1
, saying that the cell at my position has a value of one. Then I am also logging the entire grid of cells. The contradiction comes in because when I look at the grid that was logged, the cell at my position isn’t 1
, it’s 0
. I’ve been trying to figure this out for so long now and I just don’t get it.
Resources
getCell
getCell(pos: Position) {
pos = this.wrapPos(pos, true);
return this.cells[pos.y][pos.x];
}
the console line
console.log("CREATURE - B", this.pos, grid, grid.getCell(this.pos));
the console output
The link the file with the console log is here. https://replit.com/@SharkCoding/grid-test#src/js/Creature.ts line 50
How to Reproduce
If you go to the link https://grid-test.sharkcoding.repl.co/ then open the console. Now press the step button and open one of the logs that starts with “CREATURE”. Now you should be able to see the problem. It won’t have the exact same numbers, but you should still be able to see the problem.