This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

I would like to be able to print a class like an array, what I mean by this is that when you print an array you get this.

var array: Array = [1,2,3,4,5]
print(array)
# Output
[1,2,3,4,5]

I want to be able to do this with a class but I wanted to know if there was a way to do this without going through each value using get_property_list(). Like this.

class Test:
    var valueA = 1
    var valueB = 1

print(Test)
# Output
{"valueA": 1, "valueB": 1}

Kinda like if you printed a dictionary.

Godot version 3.5
in Engine by (663 points)

Don't strain yourself when you want to look at the contents of a class/object. Instead, put in breakpoints where you want to see the values. When you run the code, it will stop at the breakpoint. Then you can go into the debugger panel, and see the current, in-scope values of the variables.

1 Answer

+2 votes
Best answer

You can customize the output string to whatever you like by overriding the _to_string() function in Object.

Example:

class_name MyClass
var value_a = 1
var value_b = 1

func _to_string():
    return "{ValueA: %s, ValueB: %s}" % [value_a, value_b]
by (1,122 points)
selected by
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.