If I want to define an [Instantiable] type that can be "instantiated" from GDScript with potentially multiple single-parameter overrides, I can't quite do that properly in C# at the moment.
[Instantiable("Init", "CreateFromTime"]
public partial class Clock : Control
{
private int _time;
public void Init<[MustBeVariant] T>(T value)
{
int time = 0;
if (value is int i)
{
time = i;
}
else if (value is string format)
{
if (TimeSpan.TryParse(format, out var timeSpan))
{
time = (int)timeSpan.TotalSeconds;
}
else
{
GD.PushError($"Invalid time format '{format}'");
}
}
if (time > 0)
{
_time = time;
}
else
{
GD.PushError($"'int' or 'string' must be provided to Init<T> method, but got '{typeof(T)}'");
}
}
}
Resulting generated output currently:
// The `T` here is never declared as a generic parameter anywhere, so it produces an error.
public static Clock CreateFromTime(T value)
{
var scene = (Clock)_Clock.Instantiate();
scene.Init(value);
return scene;
}
If I want to define an [Instantiable] type that can be "instantiated" from GDScript with potentially multiple single-parameter overrides, I can't quite do that properly in C# at the moment.
Resulting generated output currently: