|
@@ -13,12 +13,27 @@
|
|
|
|
|
|
using namespace godot;
|
|
|
|
|
|
+int ExampleRef::instance_count = 0;
|
|
|
+int ExampleRef::last_id = 0;
|
|
|
+
|
|
|
+int ExampleRef::get_id() {
|
|
|
+ return id;
|
|
|
+}
|
|
|
+
|
|
|
+void ExampleRef::_bind_methods() {
|
|
|
+ ClassDB::bind_method(D_METHOD("get_id"), &ExampleRef::get_id);
|
|
|
+}
|
|
|
+
|
|
|
ExampleRef::ExampleRef() {
|
|
|
- UtilityFunctions::print("ExampleRef created.");
|
|
|
+ id = ++last_id;
|
|
|
+ instance_count++;
|
|
|
+
|
|
|
+ UtilityFunctions::print("ExampleRef ", itos(id), " created, current instance count: ", itos(instance_count));
|
|
|
}
|
|
|
|
|
|
ExampleRef::~ExampleRef() {
|
|
|
- UtilityFunctions::print("ExampleRef destroyed.");
|
|
|
+ instance_count--;
|
|
|
+ UtilityFunctions::print("ExampleRef ", itos(id), " destroyed, current instance count: ", itos(instance_count));
|
|
|
}
|
|
|
|
|
|
int Example::test_static(int p_a, int p_b) {
|
|
@@ -99,8 +114,9 @@ void Example::_bind_methods() {
|
|
|
ClassDB::bind_method(D_METHOD("simple_const_func"), &Example::simple_const_func);
|
|
|
ClassDB::bind_method(D_METHOD("return_something"), &Example::return_something);
|
|
|
ClassDB::bind_method(D_METHOD("return_something_const"), &Example::return_something_const);
|
|
|
+ ClassDB::bind_method(D_METHOD("return_empty_ref"), &Example::return_empty_ref);
|
|
|
ClassDB::bind_method(D_METHOD("return_extended_ref"), &Example::return_extended_ref);
|
|
|
- ClassDB::bind_method(D_METHOD("extended_ref_checks"), &Example::extended_ref_checks);
|
|
|
+ ClassDB::bind_method(D_METHOD("extended_ref_checks", "ref"), &Example::extended_ref_checks);
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("test_array"), &Example::test_array);
|
|
|
ClassDB::bind_method(D_METHOD("test_tarray_arg", "array"), &Example::test_tarray_arg);
|
|
@@ -184,15 +200,23 @@ Viewport *Example::return_something_const() const {
|
|
|
return nullptr;
|
|
|
}
|
|
|
|
|
|
+Ref<ExampleRef> Example::return_empty_ref() const {
|
|
|
+ Ref<ExampleRef> ref;
|
|
|
+ return ref;
|
|
|
+}
|
|
|
+
|
|
|
ExampleRef *Example::return_extended_ref() const {
|
|
|
+ // You can instance and return a refcounted object like this, but keep in mind that refcounting starts with the returned object
|
|
|
+ // and it will be destroyed when all references are destroyed. If you store this pointer you run the risk of having a pointer
|
|
|
+ // to a destroyed object.
|
|
|
return memnew(ExampleRef());
|
|
|
}
|
|
|
|
|
|
Ref<ExampleRef> Example::extended_ref_checks(Ref<ExampleRef> p_ref) const {
|
|
|
+ // This is therefor the prefered way of instancing and returning a refcounted object:
|
|
|
Ref<ExampleRef> ref;
|
|
|
ref.instantiate();
|
|
|
- // TODO the returned value gets dereferenced too early and return a null object otherwise.
|
|
|
- ref->reference();
|
|
|
+
|
|
|
UtilityFunctions::print(" Example ref checks called with value: ", p_ref->get_instance_id(), ", returning value: ", ref->get_instance_id());
|
|
|
return ref;
|
|
|
}
|