0 votes

So here is the simple code:

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():

Then what I get in the output is:

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

I cant understand this madness, why on earth are the "id" values all (1,1).
It has to do with the grid size (its 2 arguments), since when I do a create_empty_grid(3,4), what I get is all the "id" are now "2,3".
Of course I notice the pattern, but cant seem to grasp why this is happening.

Godot version v 3.3
in Engine by (448 points)

1 Answer

+1 vote
Best answer

You are assigning all grid elements to the same variable "_cell_info". Since all grid elements point to the same variable, changing one will affect all. Try copying _cell_info before assigning it, see this post for examples

Alternatively turn _cell_info into a function that returns the same dictionary. That way a new instance will be created every call.

by (1,111 points)
selected by

Thanks! that was indeed the problem. I suspected this could be the problem after posting but wasnt sure. I thought that what I was putting in the values were copies of the var

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.