godotengine / godot

Godot Engine – Multi-platform 2D and 3D game engine
https://godotengine.org
MIT License
90.11k stars 21.16k forks source link

Unable to call super.to_string() from overridden _to_string() method #86559

Open jdeppe-pivotal opened 9 months ago

jdeppe-pivotal commented 9 months ago

Tested versions

System information

MacOS

Issue description

I'd like to customize the string representation of a class using the default representation plus my own message. So my class contains:

class_name TrafficLight 
extends Sprite2D

var is_green: bool

# Unnecessary code removed...

func _to_string():
    var my_string = super.to_string()
    my_string += " -> GREEN" if is_green else " -> RED"
    return my_string

However, this results in a stack overflow because of infinite recursion. Also tried using (self as Object).to_string() with the same result. From a purely OO POV I'd expect this to be possible.

Steps to reproduce

As above

Minimal reproduction project (MRP)

None

AThousandShips commented 9 months ago

What if you call super._to_string()? Why would you call the non-virtual one?

AFAIK super only works for the current method, not any other method, in this case it doesn't matter since to_string naturally invokes the virtual method as designed

AThousandShips commented 9 months ago

This is a limitation where built-in methods can't be accessed with super, in this case the proper method would be to call super() here, but it can't be done as Sprite2D doesn't have a _to_string as such, this is a limitation and I think it has been reported elsewhere but can't find it right now

See also:

AThousandShips commented 9 months ago

That would require the engine to detect that to_string calls _to_string, how would that work?

If instead you call the virtual method by super it does error, arguably you could warn whenever super is used with a non-virtual method, or a built-in one, where it doesn't have any effect