+2 votes

In many other languages you can overwrite an to_string() func/method that is used to set the output when you directly attempt to print the classvar.

For example;

var calendar = preload("../../Calendar.gd").new()

Output should be todays datetime, such as "15-09-16"
However it is something like [Node:500]. Is there a way to make the print(calendar) output "15-09-16"?

in Engine by (474 points)

I suggest you create a get_date_string() method instead. I would expect this kind of output on a Date object, not a Calendar, but it's my personal opinion :p
You could even have get_date() outputting a dictionary with day, month and year, then str() would work out of the box on it.

2 Answers

+1 vote
Best answer
by (306 points)
selected by

Glad to see that I am not the only one wanting this :)

+2 votes

Yet! As of May 23, 2019 https://github.com/godotengine/godot/pull/27886 was merged, adding the _to_string() virtual method to Object.

It behaves exactly as you'd expect, though at present you may have to get/make a custom build to have access to that patch.

by (101 points)

Hooray! And it only took about three years! :D

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 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.