This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes
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').

Godot version 3.4.3
in Engine by (12 points)

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". :)

1 Answer

0 votes

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.

by (1,122 points)
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 Frequently asked questions and 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 [email protected] with your username.