I have the following code. ``` cpp namespace godot { class Vertex : public Reference { GODOT_CLASS(Vertex, Reference); public: void _init() {} static void _register_methods() {} Vertex() { Godot::print("Construct"); } ~Vertex() { Godot::print("Destruct"); } }; class ListDigraph : public Reference { GODOT_CLASS(ListDigraph, Reference); public: template<class T> Ref<T> __add_vertex() { return Ref<T>::__internal_constructor(T::_new()); } virtual Ref<Vertex> add_vertex() { return __add_vertex<Vertex>(); } void _init() {} static void _register_methods() {} ListDigraph() {} ~ListDigraph() {} }; ///////////////////////////////////////////////////////////////////////// class PropertyVertex : public Vertex { GODOT_SUBCLASS(PropertyVertex, Vertex); public: void _init() {} static void _register_methods() {} PropertyVertex() {} ~PropertyVertex() {} }; class PropertyGraph : public ListDigraph { GODOT_SUBCLASS(PropertyGraph, ListDigraph); public: Ref<Vertex> add_vertex() override { return __add_vertex<PropertyVertex>(); } void _init() {} static void _register_methods() {} PropertyGraph() {} ~PropertyGraph() {} }; } ``` Code compiles OK the problem is in the GDScript API: ```python var g = ListDigraph.new() print(g.add_vertex()) # Oututs #Construct #[Reference:1137] #Destruct var p = PropertyGraph.new() print(p.add_vertex()) # Oututs #[Reference:1140] ``` even though "PropertyVertex" is a "Vertex" its construct and destruct code are not printed.
Activity
[-]Base class Constructor/Destructor NOT called[/-][+][BUG] Templates are messing everything up!![/+]xDGameStudios commentedon Apr 18, 2019
the solution to this was in the template, but it might not be a bug:
so the correct way to initialize an instance in GDNative side is:
Ref<BASE>::__internal_constructor(CHILD::_new());follower commentedon Jun 22, 2020
As of ~ 9eceb16 the
__internal_constructor()workaround should no longer be necessary and use of it may lead to crash, this should now work correctly:Ref<CustomClass> ref = Ref<CustomClass>(custom_class_instance);See #417 for more details.