3838
3939using namespace godot ;
4040
41+ uint64_t ExampleRef::instance_count = 0 ;
42+
4143ExampleRef::ExampleRef () {
42- UtilityFunctions::print (" ExampleRef created." );
44+ instance_count++;
45+ UtilityFunctions::print (" ExampleRef created. Our total instance count is now: " , instance_count);
4346}
4447
4548ExampleRef::~ExampleRef () {
46- UtilityFunctions::print (" ExampleRef destroyed." );
49+ instance_count--;
50+ UtilityFunctions::print (" ExampleRef destroyed. Our total instance count is now: " , instance_count);
4751}
4852
4953void Example::_bind_methods () {
5054 // Methods.
5155 ClassDB::bind_method (D_METHOD (" simple_func" ), &Example::simple_func);
5256 ClassDB::bind_method (D_METHOD (" simple_const_func" ), &Example::simple_const_func);
53- ClassDB::bind_method (D_METHOD (" return_something" ), &Example::return_something);
57+ ClassDB::bind_method (D_METHOD (" return_something" , " base " ), &Example::return_something);
5458 ClassDB::bind_method (D_METHOD (" return_something_const" ), &Example::return_something_const);
5559 ClassDB::bind_method (D_METHOD (" return_extended_ref" ), &Example::return_extended_ref);
56- ClassDB::bind_method (D_METHOD (" extended_ref_checks" ), &Example::extended_ref_checks);
60+ ClassDB::bind_method (D_METHOD (" extended_ref_checks" , " ref " ), &Example::extended_ref_checks);
5761
5862 ClassDB::bind_method (D_METHOD (" test_array" ), &Example::test_array);
5963 ClassDB::bind_method (D_METHOD (" test_dictionary" ), &Example::test_dictionary);
@@ -73,6 +77,10 @@ void Example::_bind_methods() {
7377 ClassDB::bind_method (D_METHOD (" set_custom_position" , " position" ), &Example::set_custom_position);
7478 ADD_PROPERTY (PropertyInfo (Variant::VECTOR2, " group_subgroup_custom_position" ), " set_custom_position" , " get_custom_position" );
7579
80+ // ClassDB::bind_method(D_METHOD("get_ref_obj"), &Example::get_ref_obj);
81+ // ClassDB::bind_method(D_METHOD("set_ref_obj", "ref_obj"), &Example::set_ref_obj);
82+ // ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "ref_obj", PROPERTY_HINT_RESOURCE_TYPE, "ExampleRef"), "set_ref_obj", "get_ref_obj");
83+
7684 // Signals.
7785 ADD_SIGNAL (MethodInfo (" custom_signal" , PropertyInfo (Variant::STRING, " name" ), PropertyInfo (Variant::INT, " value" )));
7886 ClassDB::bind_method (D_METHOD (" emit_custom_signal" , " name" , " value" ), &Example::emit_custom_signal);
@@ -115,15 +123,19 @@ Viewport *Example::return_something_const() const {
115123 return nullptr ;
116124}
117125
118- ExampleRef *Example::return_extended_ref () const {
119- return memnew (ExampleRef ());
126+ Ref<ExampleRef> Example::return_extended_ref () const {
127+ // When subclassing RefCounted we should ALWAYS use Ref<..> or Godot will start doing confusing things as it will start using reference counting to manage the object.
128+ // We should never instantiate the object directly such as this:
129+ // return memnew(ExampleRef());
130+
131+ Ref<ExampleRef> ref;
132+ ref.instantiate ();
133+ return ref;
120134}
121135
122136Ref<ExampleRef> Example::extended_ref_checks (Ref<ExampleRef> p_ref) const {
123137 Ref<ExampleRef> ref;
124138 ref.instantiate ();
125- // TODO the returned value gets dereferenced too early and return a null object otherwise.
126- ref->reference ();
127139 UtilityFunctions::print (" Example ref checks called with value: " , p_ref->get_instance_id (), " , returning value: " , ref->get_instance_id ());
128140 return ref;
129141}
@@ -165,6 +177,16 @@ Vector2 Example::get_custom_position() const {
165177 return custom_position;
166178}
167179
180+ /*
181+ void Example::set_ref_obj(const Ref<ExampleRef> p_ref) {
182+ ref_obj = p_ref;
183+ }
184+
185+ Ref<ExampleRef> Example::get_ref_obj() const {
186+ return ref_obj;
187+ }
188+ */
189+
168190// Virtual function override.
169191bool Example::_has_point (const Vector2 &point) const {
170192 Label *label = get_node<Label>(" Label" );
0 commit comments