godot-cpp: Objects instanced in c++ can't be bound to typed gdscript variables

GDNative:

class MyCustomType : public godot::Node
{
    GODOT_CLASS(MyCustomType, godot::Node);
public:
    void calling();
    void functions();
    void works();
    void fine();
};

// ...

class Manager: public godot::Node
{
    GODOT_CLASS(Manager, godot::Node);
public:
    MyCustomType* make_custom_object()
    {
        auto object = MyCustomType::_new();
        add_child(object);
        return object;
    }
};

The .gdns file:

[gd_resource type="NativeScript" load_steps=2 format=2]

[ext_resource path="res://bin/test.gdnlib" type="GDNativeLibrary" id=1]

[resource]
class_name = "MyCustomType"
library = ExtResource( 1 )
script_class_name = "MyCustomType"

GDScript:

var o = Manager.make_custom_object()

o.calling()
o.functions()
o.works()
o.fine()

var test:MyCustomType = o #error

image

Also, if the variable is a function parameter, e.g.:

func broken(param:MyCustomType):
    pass

# ...

var o = Manager.make_custom_object()

broken(o)

…then no runtime error is produced, but param will be null

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Comments: 16 (2 by maintainers)

Most upvoted comments

I don’t understand how an autoload solves this tbh. At some point, you are assigning a value to var test: MyCustomType, which can fail if the cast is invalid, and that will happen with or without an autoload. Unless I misunderstood some strange detail? The cast is also supposed to occur if you return an untyped value from a function with -> MyCustomType, so if that works then that’s another problem lol