0 votes


I'm a C# user and I was thinking about the performance and ease of use of the has_method() and method calling on GDScript versus the interface direct call of C#.

Would it be faster to call GDScript methods if they were implemented like the C# interface keyword?

I think it would be an useful feature because we would know if the class implements the methods and properties in code completion, to write typed complex code easily and if I understand correctly it would be faster too.

I'm thinking about opening a feature proposal but I don't know if the performance speed gains worth.

Thanks in advance.

asked Feb 29 in Engine by equus (96 points)

1 Answer

0 votes
Best answer

You are wanting to interface a statically-typed language to a dynamically-typed one that doesn't compile within the same unit (not even the same time), so interoperating between them will outright require two checks before calling the function: check if the interface exists, and check if the object implements it (even if GDScript had that feature).

has_method or checking inheritance is how you do it today, and it turns out it does both at the same time. If you had an interface system in GDScript, it would still require a check.
While in C#, the first check is never needed because you know it exists, since your code can already compile the mention of IYourInterface symbol.

A form of interface was discussed in the next GDScript improvements to come in Godot 4.0, but I'm not sure it will reduce the amount of checks a C# script will need to do in order to call into GDScript.

answered Mar 1 by Zylann (26,033 points)
selected Mar 2 by equus

Thank you for the fast answer and the knowledge given. I understand the first part but the end part I don't know if I get it correctly. At the end you say about call C# script inside of GDScript. I didn't mean interface in that way. But if I understand right you are talking about the implementation of the interface in C# and use that in GDScript? Because I was thinking on GDScript side only. I was thinking about a new keyword on GDScript working like the interface keyword as is in C#. Thanks.

Oh yes there is an "interface" system being considered for the future, for GDScript:

But I think there will always be a check needed for calls between the two languages, unless they can both ensure the validity of the call at compilation time...

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.