Printing variable name (as a string)

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

I want to print the variable name for debugging. Not the value or the conversion to string–I need the variable name.

For example:

var apples = 6
print_var_name( apples ) # output will print "apples", not 6

The Godot debugger does this, so perhaps it’s possible to do it in gdscript? TY!

var apples = “apples” + 6

Func _process(_delta):

umma | 2022-05-30 17:39

If you are bug fixing and you want to see what each of your variables are you need to let print know so for example

var apples = 6
var pears = 10
var limes = "ripe"

print("apples ", apples)
print("pears ", pears)
Print("limes ", limes)


apples 6
pears 10
limes ripe

Gluon | 2022-05-31 15:27


String var2str ( Variant var )

Converts a Variant var to a formatted string that can later be parsed using str2var.

a = { "a": 1, "b": 2 }


"a": 1,
"b": 2

CassanovaWong | 2022-06-09 05:17

I’m looking for a way to automate this, I’m used to using lines like print("apples ", apples) but it’s pretty annoying and weird there isn’t a simpler solution.

zeroanaphora | 2023-03-11 23:26

Maybe using setget would help you with automating it?
And, there’s other methods to print:

@GDScript — Godot Engine (3.1) documentation in English

void print ( … )
void print_debug ( … )
void print_stack ( )
void printerr ( … )
void printraw ( … )
void prints ( … )
void printt ( … )

Don’t know if this helps, but I don’t know what, specifically, you are doing… If I had a little more context I might be able to help you solve it. for instance, why are you printing apples and how often, and what do you need done to make it less tedious?

CassanovaWong | 2023-03-12 19:13

After further searching I’ve come to accept its impossible, but I just wanted to print() the variable name in the console for debugging, to check a variable at certain points. I find myself constantly typing the same thing twice, print("some_variable: ", some_variable)This seems an obvious usage but the responses sometimes seem aghast the question is even being asked.

zeroanaphora | 2023-03-12 22:39

So let me ask you this… Is your trouble that you want to see the apple count in rea-time or something or that you just want a report of instances with the apples… With the former, you could just make a label that updates that value on the game window, instead of having to do the console switchback… You could even make that label appear as a separate program running in tandem with the game… you could have a timer, just print off the value, every so often or called from some function events… I don’t know, like, what are these apples anyways, are the just a variant you call in your script? Are the a scene you made with some nodes, or a node you made yourself or what? It’s hard to advise on this…

You could use signals, so that when something happens here, or there it goes to a printout… using a singleton, you can access the value globally… I don’t see why you have to keep typing apples, apples, or whatever. I mean, is this produce app? fruit ninja-like, or what?

i mean, Godot can access APIs and such, publishing real-time statistics, like Dogecoin and all that… limited only by your hardware. I doubt it is impossible to achieve you’re goal here… just need to know more details so i can inform you. Godot is just endless with solutions accessible to non-programmers and professionals alike, and if Godot can’t provide the functionality, there’s usually a plugin or add-on whatever that can be used… and if that fails, there’s always the potential to write it yourself, using the source code, and such… and it’s totally free… good luck. and dont’ get discouraged by this site, it’s not the most helpful though, at times there are gems… I have some questions still posted here, unsolved…

CassanovaWong | 2023-03-13 05:54

I’m just using print() to check the current state of variables at runtime. (To see if they’re being set as I expected, if a property is the type I want, etc) . I add and remove them as needed, which is why labels aren’t an option. I’m a self-taught hobbyist so maybe it’s bad practice but I thought it seemed an obvious debug procedure.

“Apples” was just an example lol.

zeroanaphora | 2023-03-13 21:45

:bust_in_silhouette: Reply From: CharlesMerriam

Bear with me. this feels like a non-problem so I will push some random references.

The only use cases I can see for this involve showing a debug variable list sorted by value:

234:    levelNumber, Enimies (len), Enemy.points
783: ...

This information will only be in debugging mode. The information will not be there during an optimized run.

You could use get_property_list, walk through each property, and look for the value.

You could use get_stack , read the text of the script file, and parse from there.

You could read the Godot source code.

Let me know if this was a passing fancy or the idea for something interesting.