How do I get a string version of a variable?

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

For example:

var power_up = 5

func _ready():
         print(#some method(power_up))
         #prints   power_up

Since you want it to print the word “power_up”, isn’t the (daft) solution to just print("power_up"). Since you would need to specify the name of the variable to tell it which variable name to print out, you’re basically just printing the name of the variable!

SteveSmith | 2023-01-05 14:53

:bust_in_silhouette: Reply From: fractavius

Not sure what your method does, but you should be able to do this:

func _ready(): 
    method(power_up)
    print(str(power_up))

So if I have var power_up = 6

I want to be able to print out the name itself.

print(str(power_up)) #prints the number 6.

I want print() to print “power_up”, but because of printing the variable name itself, is there a method for this?

Dumuz | 2021-08-07 01:27

:bust_in_silhouette: Reply From: Help me please

You can convert any integer into string by using str()
in your case:-

var power_up = 5
func _ready():
	print(str(power_up))
#this prints power_up

If you want to convert this string into integer again then you can use int()

var power_up = "5"
func _ready():
	integral_power_up = int(power_up)

So if I have var power_up = 6

I want to be able to print out the name itself.

print(str(power_up)) #prints the number 6.

I want print() to print “power_up”, but because of printing the variable name itself, is there a method for this?

Dumuz | 2021-08-07 01:34

I don’t think if there is a method to print the name of variable. If you have do so then you need to do this manually

var power_up = 5
func _ready():
    print(str(power_up))
    #this prints value of power_up
    print("power_up : " + str(power_up)) 
    #this prints "power_up : 5"

Help me please | 2021-08-09 06:12