Append is pushing elements to two arrays at the same time

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Leo_Guzman

Hello. I have this weird problem where I have two arrays and whenever I try to append to one array it also appends it to the second one.

I have them declared as export class variables:

export var lucky_ore_upgrades: Array = []
export var lucky_ore_types: Array = []

Inside a function I append to them like this:

func UpgradeLuckyOre(amount, cost, upgrade_type):
if typeof(amount) == TYPE_STRING:
	match amount:
		"Lucky Ores":
			lucky_ore_chance += 1
			lucky_ore_upgrades.append(amount)
			lucky_ore_types.append("Ore")

when debugging, here is what I see:

As you can see, for some reason it adds the variable “amount” which has the value “Lucky Ores” and also the value “Ore” to both these arrays

And I already did a search to see if by chance I have them assigned to one another like:

lucky_ore_types = lucky_ore_upgrades

and I never use this line or any assignment to these arrays other than in this function

I also found out that doing this works:

func UpgradeLuckyOre(amount, cost, upgrade_type):
if typeof(amount) == TYPE_STRING:
	match amount:
		"Lucky Ores":
			lucky_ore_chance += 1
			lucky_ore_upgrades.append(amount)
			lucky_ore_types = []
			lucky_ore_types.append("Ore")

But why would it assign the same value to both arrays? Does it think its the same variable? I also did a test with two other arryas and it worked fine. Maybe its a bug with godot?

Anyhow if you have any ideas please let me know :slight_smile:

Did you ever find a solve for this issue. I seemed to have run into the very same issue with a much different type of setup.

I have a Dictionary with 3 other dictionaries in it. Two of them hold arrays with the same key name, but their own respective key names are different: “Player1” and “Player2”. However in a particular portion of my script, when appending a String to one of them, the other is also appended with the same string. I’ve combed through the script and there is nothing else that should be doing this.
But I’ve done prints to check both the array directly before and after the single append and sure enough it always appends to both arrays. I don’t get it.

But if I replicated the function on a new Godot scene, it appends the one just fine.

Dumuz | 2021-12-20 01:43

:bust_in_silhouette: Reply From: klaas

Hi!
Does this fixes the problem?

export var lucky_ore_upgrades: Array = [] setget set_lucky_ore_upgrades
export var lucky_ore_types: Array = [] setget set_lucky_ore_types

func set_lucky_ore_upgrades( value ):
	lucky_ore_upgrades = value.duplicate()

func set_lucky_ore_types( value ):
	lucky_ore_types = value.duplicate()

I remember i once had a problem with unwanted instances when using objects in export variables. But i dont remember how i solved the problem.

Hello! unfortunately it does not solve the problem because the problem is only presented when appending to the array and not when setting or getting it.

I guess my solution is this:

func UpgradeLuckyOre(amount, cost, upgrade_type):
if typeof(amount) == TYPE_STRING:
	match amount:
		"Lucky Ores":
			lucky_ore_chance += 50
			lucky_ore_upgrades.append(amount)
			lucky_ore_types = []
			lucky_ore_types.append("Ore")	

After re-initializing the array like “lucky_ore_types = ” the problem isn’t presented anymore later in the function. Maybe it is a bug with arrays being Export Variables.

Thank you for the response though!

Leo_Guzman | 2021-08-14 21:36