Godot Version
4.6.3
Question
I’m new to godot so let me know if it is a redundant question or if I’m thinking about things the wrong way, but I have been working with an itemList and a tileMap to create a turn based game. I’m storing data via the metadata of the item in the itemList and want to dynamically add the clicked tile to an array to later loop through and action. All of this works well except whenever I add to the array, it is overriding all elements of the array.
This is my function and I am declaring a new dictionary variable inside the loop so I feel it shouldn’t be a reference issue.
func insertIntoQueue(itemsToAdd : Array, mousePos : Vector2i) -> void:
for items : int in itemsToAdd :
var metadata : Dictionary = item_list.get_item_metadata(items)
metadata["tilePosition"] = mousePos
GameGlobals.constructionQueue.append(metadata)
print(GameGlobals.constructionQueue)
The output:
#First click
[
{
"time":1,
"cost":0,
"atlasPos":(3,0),
"tilePosition":(5,22)
}
]
#Second click
[
{
"time":1,
"cost":0,
"atlasPos":(3,0),
"tilePosition":(4,21)
},
{
"time":1,
"cost":0,
"atlasPos":(3,0),
"tilePosition":(4,21)
}
]
#Third click
[
{
"time":1,
"cost":0,
"atlasPos":(3,0),
"tilePosition":(4,20)
},
{
"time":1,
"cost":0,
"atlasPos":(3,0),
"tilePosition":(4,20)
},
{
"time":1,
"cost":0,
"atlasPos":(3,0),
"tilePosition":(4,20)
}
]
Just for my testing I added a .duplicate_deep() to the get get metadata call and this works as expected but it feels wrong.
func insertIntoQueue(itemsToAdd : Array, mousePos : Vector2i) -> void:
for items : int in itemsToAdd :
var metadata : Dictionary = item_list.get_item_metadata(items).duplicate_deep()
metadata["tilePosition"] = mousePos
GameGlobals.constructionQueue.append(metadata)
Output with duplicat_deep():
#First click
[
{
"time":1,
"cost":0,
"atlasPos":(3,0),
"tilePosition":(5,22)
}
]
#Second click
[
{
"time":1,
"cost":0,
"atlasPos":(3,0),
"tilePosition":(5,22)
},
{
"time":1,
"cost":0,
"atlasPos":(3,0),
"tilePosition":(4,21)
}
]
#Third click
[
{
"time":1,
"cost":0,
"atlasPos":(3,0),
"tilePosition":(5,22)
},
{
"time":1,
"cost":0,
"atlasPos":(3,0),
"tilePosition":(4,21)
},
{
"time":1,
"cost":0,
"atlasPos":(3,0),
"tilePosition":(4,20)
}
]
Any pointers are greatly appreciated.