0

I have been looking for a solution everywhere on the internet but nowhere I can see a single script which lets me read the name of a variable as a string in Godot 3.1

What I want to do:

  1. Save path names as variables.
  2. Compare the name of the path variable as a string to the value of another string and print the path value.

Eg -

var Apple = "mypath/folder/apple.png"
var myArray = ["Apple", "Pear"]

Function that compares the Variable name as String to the String -

if (myArray[myposition] == **the required function that outputs variable name as String**(Apple) :
       print (Apple)  #this prints out the path.

Thanks in advance!

Mana
  • 352
  • 5
  • 22
  • 1
    Can you explain _why_ you think you need the name of the variable? What is the bigger picture of what you're trying to achieve? This sounds like an [XY Problem](https://en.m.wikipedia.org/wiki/XY_problem). – rcorre Dec 11 '19 at 01:46
  • I was trying to get it so that I can make another auto-generated array containing paths of the icons of characters, after inputting an array of the character names. This is so that they update themselves on the dialogue box when a character speaks something, without having to do it manually. I hope this makes sense. I can explain it better if required. – Mana Dec 11 '19 at 23:21

3 Answers3

0

I think your approach here might be a little oversimplified for what you're trying to accomplish. It basically seems to work out to if (array[apple]) == apple then apple, which doesn't really solve a programmatic problem. More complexity seems required.

First, you might have a function to return all of your icon names, something like this.

func get_avatar_names():
    var avatar_names = []
    var folder_path = "res://my/path"
    var avatar_dir = Directory.new()
    avatar_dir.open(folder_path)
    avatar_dir.list_dir_begin(true, true)
    while true:
        var avatar_file = avatar_dir.get_next()
        if avatar_file == "":
            break
        else:
            var avatar_name = avatar_file.trim_suffix(".png")
            avatar_names.append(avatar_name)
    return avatar_names

Then something like this back in the main function, where you have your list of names you care about at the moment, and for each name, check the list of avatar names, and if you have a match, reconstruct the path and do other work:

var some_names = ["Jim","Apple","Sally"]
var avatar_names = get_avatar_names()
for name in some_names:
    if avatar_names.has(name):
        var img_path = "res://my/path/" + name + ".png"
        # load images, additional work, etc...

That's the approach I would take here, hope this makes sense and helps.

Nick Schroeder
  • 1,340
  • 1
  • 13
  • 17
0

I think the current answer is best for the approach you desire, but the performance is pretty bad with string comparisons.

I would suggest adding an enumeration for efficient comparisons. unfortunately Godot does enums differently then this, it seems like your position is an int so we can define a dictionary like this to search for the index and print it out with the int value.

var fruits = {0:"Apple",1:"Pear"}
func myfunc():
    var myposition = 0
    if fruits.has(myposition):
        print(fruits[myposition])

output: Apple

If your position was string based then an enum could be used with slightly less typing and different considerations.

reference: https://docs.godotengine.org/en/latest/tutorials/scripting/gdscript/gdscript_basics.html#enums

-2

Can't you just use the str() function to convert any data type to stirng?

var = str(var)
Xavier R
  • 1
  • 1
  • I am not looking to convert a data type to a string but the name of the data type to a string. For example - var myVariable =9 by doing your method, I'd do var myVariableasString = string(myVariable) print (myVariableasString) will return a String that is 9. Instead, I want it to return the variable name "myVariable". I hope this makes it clear. – Mana Dec 12 '19 at 01:20