0 votes

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

Godot version 3.2.3
in Engine by (72 points)

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

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').

So why don't you call the dictionary "bite" then?

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

1 Answer

+1 vote
Best answer

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.

by (7,452 points)
selected by

Thank you very much

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.