Calling super.to_string from custom _to_string method
Asked Answered
D

3

0

Using GDScript in Godot 4, I'd like to customize the string representation of a class using the default representation plus my own message. So, I'm just doing:

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. Is there a way to do this?

Dorisdorisa answered 24/12, 2023 at 1:37 Comment(0)
R
0

Dorisdorisa Call super._to_string() instead, or just super().

Recurve answered 24/12, 2023 at 10:1 Comment(0)
D
0

Unfortunately that gives a compilation error:

Cannot call the parent class' virtual function "_to_string()" because it hasn't been defined.
Dorisdorisa answered 24/12, 2023 at 16:53 Comment(0)
R
0

Dorisdorisa That's weird because it is defined in Object, which is the base class of everything.

Recurve answered 25/12, 2023 at 10:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.