Open NoctemCat opened 2 months ago
If the GodotObject
has a script attached (doesn't matter if it's GDScript or C#), the script's to_string
method always takes precedence:
In the case of a C# script, we end up calling CSharpInstanceBridge.CallToString
:
This method calls System.Object.ToString
which, if not overridden, falls back to the base implementation in GodotObject
:
Then I guess the question is, should it set outValid
to false if it is not being overriden in your class, similar to GDScript?
Also, I'm guessing this is also why this doesn't work?
Checking if the C# user class implements/overrides a certain method, which is akin to duck typing, feels like the wrong approach for C#.
Maybe we can change godotsharp_object_to_string
to match the Object::to_string
implementation, and check if the Object has an extension. I've opened https://github.com/godotengine/godot/pull/94665 to explore this approach.
Tested versions
v4.2.2.stable.mono.official [15073afe3]
System information
Godot v4.2.2.stable.mono - Windows 10.0.19045 - Vulkan (Forward+) - dedicated NVIDIA GeForce GTX 1650 (NVIDIA; 32.0.15.5612) - Intel(R) Core(TM) i5-9300H CPU @ 2.40GHz (8 Threads)
Issue description
I was testing with overriding
_to_string
method in GDExtension class and when I attached C# script on that node, GDExtension's_to_string
stopped working.ToString
in C# was not overriden. When I tested the same script, but in GDScript it worked as expected, GDExtension's_to_string
got called and printed. C#GetNode
also can't access GDExtension's_to_string
I expect GDExtension
_to_string
to get called when C# script does not override it. At least to stop it from overriding C++ sideSteps to reproduce
_to_string
GD.Print
, or try to useto_string
from GDExtension side, it now doesn't workMinimal reproduction project (MRP)
to_string_repro.zip