Godot 4 Get the variable name

Godot 4
How can i get the variable name? not the value.

Can you be more specific, please?

How do you get the name of a variable without knowing the name of the variable? :sweat_smile:

  • Do you want a list of all variables on a node?
  • Do you want all the keys from a dictionary?
  • Something else?

For example i have a list with variables, and i want to get the names into another variable.

In that case is it better using dictionaries?

It depends on your use case

A dictionary is a key-value store

See Dictionary — Godot Engine (stable) documentation in English

var my_dict = {
    "key_a": "value_a",
    "key_b": "value_b"
}

Small hint: In Godot 4.3, dictionaries can not be typed but this might change in 4.4 Add static type hints for dictionary members · Issue #56 · godotengine/godot-proposals · GitHub

Example to iterate over dictionary keys

var groceries = {"Orange": 20, "Apple": 2, "Banana": 4}
for fruit in groceries:
	var amount = groceries[fruit]

An array only has integer keys so to map keys to values you basically loop over the array index

I’m fairly new to Godot and might miss data structures I’m not yet aware of myself

Does this help? If not, can you tell me what exactly you try to achieve? Maybe we can come up with a solution

2 Likes

You can’t.

1 Like