How to print a class like an array?

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

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.

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.

Ertain | 2022-09-11 08:48

:bust_in_silhouette: Reply From: Ninfur

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]