0 votes

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?

in Engine by (88 points)

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

That is what dictionaries are made for. See:
https://docs.godotengine.org/en/latest/classes/class_dictionary.html

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)   

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.

Please log in or register to answer this question.

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.