Variable name from another variable

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Eidam

Hello,
I´m looking the way, how I can create or refer to variable using another variable…
Something as:

var number = 1
var ("variable"+str(number)) = 0

Can it be done in Godot? And how?
Thank you for your help!

:bust_in_silhouette: Reply From: A112

You could achieve that effect using dictionaries:

extends Node2D
var dict={
"var1":{
	"a1":60000,
	"a2":2
},
"var2":{
	"a1":6000,
	"a2":0.6
},
"var3":{
	"a1":4000,
	"a2":0.3
}
}
func _ready():
var x =2
var y = "a"
print(dict["var"+str(x)][y+str(x)])

You can also add your own variables to a dictionary, just refer to it’s name and assign a value

Thanks for your answer!
She helped me a lot!

Eidam | 2020-05-05 19:45

I’m glad I could help :slight_smile:

A112 | 2020-05-05 22:07