If you have a dictionary, you should be able to access it directly like this:
var value = dictionary_here["key_name_here"]
Though if you have multiple dictionaries, like multiple weapons, then you probably will want to store them in a dictionary as well if you are wanting to use the component
variable as the key. Something like this, for example:
var component = "shock_rifle"
var weapons = {
"shock_rilfe": {
"damage": 5,
"type": 1
},
"shock_pistol": {
"damage":3,
"type":2
}
}
func _ready():
# to access:
var current_weapon = weapons[component]
print ("current weapon: ", current_weapon, " | damage: ", current_weapon["damage"])
# change weapons
component = "shock_pistol"
current_weapon = weapons[component]
print ("current weapon: ", current_weapon, " | damage: ", current_weapon["damage"])
# you can also add to the dictionary in _ready, which can sometimes lead to easier to read code
weapons["shock_shotgun"] = {
"damage": 10,
"type":3
}
I think the str2var
and var2str
(assuming it exists) are for serialization to a file, though I have not used either function so I cannot say for sure.