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

    for y in range(height):

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

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

Now if I call
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?

in Engine by (140 points)

1 Answer

0 votes

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

by (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.
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 webmaster@godotengine.org with your username.