0 votes

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!

Godot version 3.5
in Engine by (191 points)

var apples = "apples" + 6

Func process(delta):
print(apples)

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)

output:

apples 6
pears 10
limes ripe

var2str?

String var2str ( Variant var )

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

a = { "a": 1, "b": 2 }
print(var2str(a))

prints

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

1 Answer

0 votes

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.

by (327 points)
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 webmaster@godotengine.org with your username.