0 votes

On the same Note as the previous post, but a different problem. Here is the code again

extends Node

var grid := {}

var _cell_info := {
    "id": "",
    "walkable" : true,
    "move_cost" : 1,
    "unit" : null

func create_empty_grid(x_tiles: int, y_tiles: int) -> void:
    for x in range(0, x_tiles, 1):
        for y in range(0, y_tiles, 1):
            grid[str(x) + "," + str(y)] = _cell_info
            grid[str(x) + "," + str(y)].id = str(x) + "," + str(y)

func _ready():
    grid["0,0"].id = "0,0"
    grid["0,1"].id = "0,1"

I am now seeing that when I try to modify the values inside one key, I end up modifing all the values in all the keys. What I get as an output is this

{id:0,0, move_cost:1, unit:Null, walkable:True}
{id:0,0, move_cost:1, unit:Null, walkable:True}
{id:0,1, move_cost:1, unit:Null, walkable:True}
{id:0,1, move_cost:1, unit:Null, walkable:True}

So maybe I am doing something wrong with the dictionary manipulation. I dont think it has to do with the fact that each value gets its initial setup from the same var, but who knows at this point. Madness... (or lack of knowledge from my part)

Godot version v 3.3
in Engine by (448 points)

1 Answer

+1 vote
Best answer

I believe the issue is with the following line:

grid[str(x) + "," + str(y)] = _cell_info

You are assigning the same _cell_info dictionary to each value in grid. To fix this try:

grid[str(x) + "," + str(y)] = _cell_info.duplicate()
by (280 points)
selected by

Thanks for the awnser. It was, as expected, the same problem as my previous post.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.