0 votes

I have this code that basically replaces a "Unit" list value in EnemyUnits dic with a list value from EnemyDic. And then it takes "HP", "Aim", "Def" values in newly replaces list and appends them at the end (for future use as base values).

var EnemyUnits = {"Unit7" : ["Name", "HP", "Aim", "Def", "Status", "Lvl"],
                "Unit8" : ["Name", "HP", "Aim", "Def", "Status", "Lvl"],
                "Unit9" : ["Name", "HP", "Aim", "Def", "Status", "Lvl"],
                "Unit10" : ["Name", "HP", "Aim", "Def", "Status", "Lvl"],
                "Unit11" : ["Name", "HP", "Aim", "Def", "Status", "Lvl"],
                "Unit12" : ["Name", "HP", "Aim", "Def", "Status", "Lvl"],
                "Unit13" : ["Name", "HP", "Aim", "Def", "Status", "Lvl"],
                "Unit14" : ["Name", "HP", "Aim", "Def", "Status", "Lvl"],
                "Unit15" : ["Name", "HP", "Aim", "Def", "Status", "Lvl"]}

var EnemyDic = {"0" : null,
                "1" : ["Goblin", 70, 50, 50, [], 1],
                "2" : ["Orc", 100, 60, 90, [], 1],
                "3" : ["Skeleton", 130, 30, 150, [], 1],
                "4" : ["Bear", 170, 90, 150, [], 1]
                }

func populate_enemy_nodes():
    for i in 9:
    #gets a value from UnitListNum that is a list of 9 generated numbers*
        var UnitListValue = UnitListNum[i]
    #based on a number from UnitListNum takes a list value from EnemyDic and replaces Unit* with it in EnemyUnits*
        EnemyUnits["Unit"+str(7+i)] = EnemyDic[str(UnitListValue)]
        if EnemyUnits["Unit"+str(7+i)] != null:
            EnemyUnits["Unit"+str(7+i)].append(EnemyUnits["Unit"+str(7+i)][1])
            EnemyUnits["Unit"+str(7+i)].append(EnemyUnits["Unit"+str(7+i)][2])
            EnemyUnits["Unit"+str(7+i)].append(EnemyUnits["Unit"+str(7+i)][3])
        print(i)
        print(EnemyUnits.keys()[i])
        print(EnemyUnits["Unit"+str(7+i)])
        print(EnemyDic)

But for some reason not only EnemyUnits lists get appends but also EnemyDic lists. And EnemyDic occurs only once in my code outside of var declaration.

0
Unit7
[Orc, 100, 60, 90, [], 1, 100, 60, 90]
{0:Null, 1:[Goblin, 70, 50, 50, [], 1], 2:[Orc, 100, 60, 90, [], 1, 100, 60, 90], 3:[Sceleton, 130, 30, 150, [], 1], 4:[Bear, 170, 90, 150, [], 1]}
1
Unit8
[Orc, 100, 60, 90, [], 1, 100, 60, 90, 100, 60, 90]
{0:Null, 1:[Goblin, 70, 50, 50, [], 1], 2:[Orc, 100, 60, 90, [], 1, 100, 60, 90, 100, 60, 90], 3:[Sceleton, 130, 30, 150, [], 1], 4:[Bear, 170, 90, 150, [], 1]}
2
Unit9
[Goblin, 70, 50, 50, [], 1, 70, 50, 50]
{0:Null, 1:[Goblin, 70, 50, 50, [], 1, 70, 50, 50], 2:[Orc, 100, 60, 90, [], 1, 100, 60, 90, 100, 60, 90], 3:[Sceleton, 130, 30, 150, [], 1], 4:[Bear, 170, 90, 150, [], 1]}
3
Unit10
[Orc, 100, 60, 90, [], 1, 100, 60, 90, 100, 60, 90, 100, 60, 90]
{0:Null, 1:[Goblin, 70, 50, 50, [], 1, 70, 50, 50], 2:[Orc, 100, 60, 90, [], 1, 100, 60, 90, 100, 60, 90, 100, 60, 90], 3:[Sceleton, 130, 30, 150, [], 1], 4:[Bear, 170, 90, 150, [], 1]}
etc

I assume that somewhere here EnemyUnits["Unit"+str(7+i)] = EnemyDic[str(UnitListValue)] EnemyDic gets the same "appended" values as EnemyUnits for some reason. How do I fix this?

asked Jan 29 in Engine by Fenisto (63 points)

[Orc, 100, 60, 90, [], 1, 100, 60, 90] and [Orc, 100, 60, 90, [], 1, 100, 60, 90]
Are they just references to the same list?
So I need to replace values in the list one by one for them to be separate entities?

So I changed this:

func populate_enemy_nodes():
    for i in 9:
        var UnitListValue = UnitListNum[i]
        EnemyUnits["Unit"+str(7+i)] = EnemyDic[str(UnitListValue)]
        if EnemyUnits["Unit"+str(7+i)] != null:
            EnemyUnits["Unit"+str(7+i)].append(EnemyUnits["Unit"+str(7+i)][1])
            EnemyUnits["Unit"+str(7+i)].append(EnemyUnits["Unit"+str(7+i)][2])
            EnemyUnits["Unit"+str(7+i)].append(EnemyUnits["Unit"+str(7+i)][3])

to this:

func populate_enemy_nodes():
    for i in 9:
        var UnitListValue = UnitListNum[i]
        if EnemyDic[str(UnitListValue)] == null:
            EnemyUnits["Unit"+str(7+i)] = EnemyDic["0"]
        else:
            for j in 6:
                EnemyUnits["Unit"+str(7+i)][j] = EnemyDic[str(UnitListValue)][j]
        if EnemyUnits["Unit"+str(7+i)] != null :
            EnemyUnits["Unit"+str(7+i)].append(EnemyUnits["Unit"+str(7+i)][1])
            EnemyUnits["Unit"+str(7+i)].append(EnemyUnits["Unit"+str(7+i)][2])
            EnemyUnits["Unit"+str(7+i)].append(EnemyUnits["Unit"+str(7+i)][3])

Works fine

1 Answer

+1 vote
Best answer

I see you've found a solution, but you could use duplicate instead of manually copying each element in the array:

EnemyUnits["Unit"+str(7+i)] = EnemyDic[str(UnitListValue)].duplicate()
answered Jan 30 by Bernard Cloutier (378 points)
selected Jan 30 by Fenisto
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.