Calling a dictionary value, how do I make it look for a string in the variable "plant" instead of just "plant"?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By ITSgaLYXI
export var plant: String

onready var g = get_node("/root/g")

func _ready():
	$Sprite.texture = load("res://src/assets/seedpackets/"+plant+".png")
	$Label.text = g.plant_stats.plant.SunCost

Invalid get index ‘plant’ (on base: ‘Dictionary’).

Unrelated side note: You should rename “g” to something human readable and descriptive. If in doubt, avoid abbreviating variable - It will make you a better coder and help yourself and others that read the code to know what it is without needing to “look it up”. :slight_smile:

ivanskodje | 2022-04-24 08:16

:bust_in_silhouette: Reply From: Ninfur

To read a value from a dictionary, use either dictionary[key] or dictionary.get(key). This will return the value/object that was put in the dictionary on the given key.

Assuming “plant_stats” is a dictionary, “plant” is a key, and “SunCost” is a field on a plant object stored in the dictionary, try the following:

$Label.text = g.plant_stats[plant].SunCost

If that doesn’t work, please provide some more information on what you store in the dictionary.