Inconsistent Variables

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.



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. line 50

How to Reproduce

If you go to the link 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.

1 Like

The overall goal of what I am trying to do is to update the grid display ever time it changes, but I can’t because I keep running into random problems like this one.

THis is probably happening because the grid is changing after that message is logged. The developer tools only evaluate a logged object when you expand it.

Try doing

console.log("CREATURE - B", this.pos, JSON.stringify(grid), grid.getCell(this.pos));

and check to see if the logged output makes sense


WOW! I just applied that to another spot as well and it literally just solved all the problems I’ve been having :laughing: . I never knew that the values could change from when you called console.log to when it was actually outputted, but I’ll keep that in mind.


I don’t think they can change if you use Chromium’s devtools, but Eruda runs in the same renderer process as the webpage, so it has some limitations.

They can. Devtools evaluates the logged value when you click expand:
image image

Is that lazy evaluation?

That’s the phrase I was looking for! Saying that they “change” isn’t strictly accurate.

Also, hovering over the blue “i” shows “This value was evaluated upon first expanding. It may have changed since then.”

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.