GDScript method call vs C# interface method call and feature proposal

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By equus

Hello!

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.

:bust_in_silhouette: Reply From: Zylann

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.

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.

equus | 2020-03-01 23:26

Oh yes there is an “interface” system being considered for the future, for GDScript:
GDScript Proposals - Google Docs

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…

Zylann | 2020-03-01 23:54