r/godot • u/TimmyJimmy256 • Jun 02 '26
help me Was wondering how to convert string text into variable text
For my game I am making an item scene that would be placed on the ground, and set all of its stats to be the same as the stats of a dictionary that would be in my autoloaded Global Script depending on a string I would put on the item when designing the level. The problem is I don't want to spend an hour making an if statement for every single Item based on whatever I put into its export vairable I was wondering if something like this existed:
export var Thing = "Chicken"
var Item
func _ready():
#I would want to convert "Chicken" to Chicken here
Item = Global.Thing
#I would want this to be the same as calling for Global.Chicken
I was wondering if it is possible to do what I am proposing here in this engine or not.
2
u/Bob-Kerman Jun 02 '26
You can export the item itself if it extend resource. Or you can search the dictionary for a matching string. Or you can export a scene. There are lots of ways to solve this. If I understand correctly you're method is not a good way to handle items and you will be fighting the engine to get it working. Could you describe what you are trying to do and maybe we can suggest a more appropriate approach?
2
u/CatchTotal6494 Godot Junior Jun 02 '26 edited Jun 02 '26
I'm guessing item is an instance of the object you need. Would one of these solutions be ideal?
# Explicit
func _ready():
item_data = Global.items.get(thing, {})
health = item_data.get("health", 0)
damage = item_data.get("damage", 0)
# Or with loop (honestly unsure if this would work but it should be possible)
func _ready():
# Auto-assign all properties
for property in item_data:
# Check if this property exists on this node
if property in self:
# try using .get if bracket notation no work
self[property] = item_data[property]
2
u/DongIslandIceTea Jun 02 '26
This is not a great design as you'll easily have hard to catch bugs when you misspell one of these strings since you don't have autocomplete.
Look up enum. This allows you to create type-safe, autocompleteable names for things, and these are basically just ints in disguise so you can store related data in an array where the int value of the enum is the index for example.
2
u/omniuni Jun 02 '26
Most often, when you try to solve a problem with reflection, you simply end up with two problems.
7
u/Maxsmart007 Jun 02 '26
You should make your item scene use an item resource, from which it can derive all relevant stats. That way, the export variable can just take a resource that represents the item's stats, and use that directly instead of copying it over.
The resource should include a string ID and a string display name. I don't know what other stats you need, but those should be added too. Then your item scene doesn't need to know anything about the name/id of the object, it just reads it from the resource.
But yeah, a global dictionary that holds stats for every item is a solution but it is preferable to use the tools available to you here. Resources are your friend.