![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
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
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