Dictionary Referencing with variable

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

I would like to do something like this

var attack1 = "bite"
attack1_dmg = Global.attack1["dmg"]

The problem is I do not know how to make the variable work as a path to the dictionary and not just be the actual path

So you don’t want to store a reference to Global.attack1["dmg"] in attack1_dmg?

exuin | 2021-04-11 19:38

No I just want it to be read as

Global.bite["dmg"]

instead of

Global.attack1["dmg"]

if that is possible

EDIT: I also keep receiving

Invalid get index 'dmg' (on base: 'String').

Shazelz | 2021-04-11 19:51

So why don’t you call the dictionary “bite” then?

exuin | 2021-04-11 20:24

Because I want to be able to change between dictionaries so I change attack1 then it changes the dictionary that it interacts with

Shazelz | 2021-04-11 20:38

:bust_in_silhouette: Reply From: exuin

Use nested dictionaries. Put all of your attack dictionaries in another dictionary, like this:

var attacks = {
    "bite": {},
    "another_attack": {},
}

This way you can access the dictionary with a String.

Thank you very much

Shazelz | 2021-04-11 20:55