0 votes

I have a scene called LandTile - which is my Template. My GameMap is made of several scenes of LandTile which inherit from the original scene (the template) with this piece of code:

func create_map(width : int = size.x, height : int = size.y ): #a 2D Array with a LandTile in each cell
var a = []

for x in range(width):
    a.append([])
    a[x].resize(height)

    for y in range(height):

        var lt = load("res://LandTile.tscn")
        var lti = lt.instance()
        add_child(lti)

and everything works as expected so far.
Now I added two variables to LandTile:

var harvest : Dictionary 
var defense : int

Then I have 2 functions:

func set_defense (amount : int):
if amount > 0 :
    defense = amount

func set_harvest(resEnumArray : Array, amountArray : Array):
if resEnumArray.size() == amountArray.size():
    for i in range (resEnumArray.size()):
        harvest[resEnumArray[i]]=amountArray[i]

Now if I call
specialLandTile.set_defense(2)
I get what i expect: just specialLandTile has a defense value of 2, all others have 0.

However: If i call specialLandTile.set_harvest([1,2,3][3,3,3])
I do get {1:3, 2:3, 3:3}
on ALL my landTiles, i.e. if I add to an empty Dictionary the way I do it, the Dictionary is changed in all instances. Is that supposed to be like that or is it a bug?
And: Has anybody an idea for a workaround?

asked Dec 8, 2019 in Engine by Klagsam (140 points)

1 Answer

0 votes

I got the answer myself.
I have to declare
var harvest : Dictionary = {} WITH the brackets in the declaration.

answered Dec 8, 2019 by Klagsam (140 points)
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 How to use this Q&A? before posting your first questions.