godotengine / godot-proposals

Godot Improvement Proposals (GIPs)
MIT License
1.11k stars 69 forks source link

GDScript interation with GDNative #1579

Open nonunknown opened 3 years ago

nonunknown commented 3 years ago

Describe the project you are working on: Studying GDNative

Describe the problem or limitation you are having in your project: When you have a working GDNS and GDNLIB file, you GDScript can use for example:

Player.Gdns Test.gd ->

class_name Test extends Player

Basically you can't inherit Classes compiled with GDNative, but this should be possible.

Another problem is that if you have a class that inherits from any Node derived class, it doesn't appear here: Screenshot from 2020-09-28 06-45-30

Describe the feature / enhancement and how it helps to overcome the problem or limitation:

Describe how your proposal will work, with code, pseudocode, mockups, and/or diagrams:

//user creates a class
class Test : public Node() {}

//inside gdscript
class_name Player extends Test

If this enhancement will not be used often, can it be worked around with a few lines of script?: It will be useful for anyone using GDNative, and it can note be worked around by now, as far as I know.

Is there a reason why this should be core and not an add-on in the asset library?: Because involves core changes

PranavSK commented 3 years ago

I think it is possible to inherit and use GDNative classes and functions within GDScript it is just not very apparent. This I think is a side effect of introducing the class_name in GDScript. Before any inheritance was done by the path to the script resource. eg:

extends "res://path/to/MyScript.gdns"

But yes this also means that a lot of the code completion advantages that is facilitated via class_name is no longer available.

So regarding the enhancements,

colugomusic commented 3 years ago

I think it is possible to inherit and use GDNative classes and functions within GDScript it is just not very apparent. This I think is a side effect of introducing the class_name in GDScript. Before any inheritance was done by the path to the script resource. eg:

extends "res://path/to/MyScript.gdns"

But yes this also means that a lot of the code completion advantages that is facilitated via class_name is no longer available.

So regarding the enhancements,

  • The inheritance is possible, the issue is more with code completion and discovery.
  • The Node derived classes, I believe, do show up in the "Create new Node" panel if registered correctly within the GDNative API. Not sure why you are not seeing this. Maybe they are not grouped correctly with the rest of the in-built nodes?

This entire post is wrong. GDScript cannot inherit from GDNative classes. Your code snippet will produce a generic "Couldn't load the base class" error message. And Node derived GDNative classes will not show up in the "Create New Node" dialog. As far as I know this has never been the case.

colugomusic commented 3 years ago

If it is possible to get this working then the flexibility gained from allowing GDScripts to extend GDNative classes would be pretty big.

One of the advantages of GDNative is the ability to take some relatively stable GDScript and recode it in C++ for reasons of performance or robustness or whatever.

At the moment if you have a script that is at the base of some complex inheritance hierarchy, it's not possible to convert it to GDNative without also converting the entire inheritance hierarchy along with it.

Calinou commented 3 years ago

If it is possible to get this working then the flexibility gained from allowing GDScripts to extend GDNative classes would be pretty big.

See https://github.com/godotengine/godot/pull/40147 and https://github.com/godotengine/godot/pull/48201 which improve cross-language interoperability. However, cross-language inheritance is very difficult to implement and may not even be possible in the first place (not without ruining performance at least).