In an array of variables where each var has it's own value how do I get var's name out of the array?

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

I have an array of variables. Each variable points to a resource.
For example:
var Archer = “res://Sprites/Archer.png”
var Bear = “res://Sprites/Bear.png”
var GobAr = “res://Sprites/GobAr.png”
var Enemies = [Archer, Bear, GobAr]

Enemies[0] returns “res://Sprites/Archer.png”

Can I make the array return Archer instead?

You can’t have an array of variables. Both archer and enemies[0] are referencing the same image resource.

Magso | 2020-01-18 15:36

That is what dictionaries are made for. See:
Dictionary — Godot Engine (latest) documentation in English

wombatstampede | 2020-01-18 15:52

print(Enemies[0].get_file()) #return Archer.png
print(Enemies[0].get_file().get_basename()) #return Archer

But it has nothing to do with the name of the variable. For that, as they told you, a dictionary is better:

var Enemies={
    	Archer= "res://Sprites/Archer.png",
    	Bear= "res://Sprites/Bear.png",
    	GobAr = "res://Sprites/GobAr.png"
    	}
    func _ready():
    	
    	print(Enemies)
    	for i in Enemies.keys():
    		print(i)
        for i in Enemies.values():
            print(i)
    	print(Enemies.Archer)	

estebanmolca | 2020-01-18 16:05

Yeah, I know that they both point to the same memory address.
And that it can be done with a dictionary.
But I wanted to know if there is some kind of method that can let me do this kind of thing with an array.
Just like that:

print(Enemies[0].get_file()) #return Archer.png
print(Enemies[0].get_file().get_basename()) #return Archer

Thank you.

Fenisto | 2020-01-18 18:07