0 votes

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:
https://ibb.co/DrQqKcY
https://ibb.co/PzqSjmg
https://ibb.co/dQcWQ77

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

Godot version 3.3.2
in Engine by (14 points)

1 Answer

+1 vote

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.

by (3,932 points)

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 "luckyoretypes = []" 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!

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.