3838
3939using namespace godot ;
4040
41+ uint64_t ExampleRef::instance_count = 0 ;
42+
43+ void ExampleRef::_bind_methods () {
44+ ClassDB::bind_method (D_METHOD (" get_value" ), &ExampleRef::get_value);
45+ ClassDB::bind_method (D_METHOD (" set_value" , " value" ), &ExampleRef::set_value);
46+ ADD_PROPERTY (PropertyInfo (Variant::INT, " value" ), " set_value" , " get_value" );
47+ }
48+
4149ExampleRef::ExampleRef () {
42- UtilityFunctions::print (" ExampleRef created." );
50+ instance_count++;
51+ UtilityFunctions::print (" ExampleRef created. Our total instance count is now: " , instance_count);
52+
53+ // default this
54+ value = 1 ;
4355}
4456
4557ExampleRef::~ExampleRef () {
46- UtilityFunctions::print (" ExampleRef destroyed." );
58+ instance_count--;
59+ UtilityFunctions::print (" ExampleRef destroyed. Our total instance count is now: " , instance_count);
60+ }
61+
62+ void ExampleRef::set_value (int64_t p_value) {
63+ value = p_value;
64+ }
65+
66+ int64_t ExampleRef::get_value () const {
67+ return value;
4768}
4869
4970void Example::_bind_methods () {
5071 // Methods.
5172 ClassDB::bind_method (D_METHOD (" simple_func" ), &Example::simple_func);
5273 ClassDB::bind_method (D_METHOD (" simple_const_func" ), &Example::simple_const_func);
53- ClassDB::bind_method (D_METHOD (" return_something" ), &Example::return_something);
74+ ClassDB::bind_method (D_METHOD (" return_something" , " base " ), &Example::return_something);
5475 ClassDB::bind_method (D_METHOD (" return_something_const" ), &Example::return_something_const);
5576 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);
77+ ClassDB::bind_method (D_METHOD (" extended_ref_checks" , " ref " ), &Example::extended_ref_checks);
5778
5879 ClassDB::bind_method (D_METHOD (" test_array" ), &Example::test_array);
5980 ClassDB::bind_method (D_METHOD (" test_dictionary" ), &Example::test_dictionary);
@@ -73,6 +94,10 @@ void Example::_bind_methods() {
7394 ClassDB::bind_method (D_METHOD (" set_custom_position" , " position" ), &Example::set_custom_position);
7495 ADD_PROPERTY (PropertyInfo (Variant::VECTOR2, " group_subgroup_custom_position" ), " set_custom_position" , " get_custom_position" );
7596
97+ ClassDB::bind_method (D_METHOD (" get_ref_obj" ), &Example::get_ref_obj);
98+ ClassDB::bind_method (D_METHOD (" set_ref_obj" , " ref_obj" ), &Example::set_ref_obj);
99+ ADD_PROPERTY (PropertyInfo (Variant::OBJECT, " ref_obj" , PROPERTY_HINT_RESOURCE_TYPE, " ExampleRef" ), " set_ref_obj" , " get_ref_obj" );
100+
76101 // Signals.
77102 ADD_SIGNAL (MethodInfo (" custom_signal" , PropertyInfo (Variant::STRING, " name" ), PropertyInfo (Variant::INT, " value" )));
78103 ClassDB::bind_method (D_METHOD (" emit_custom_signal" , " name" , " value" ), &Example::emit_custom_signal);
@@ -85,11 +110,11 @@ void Example::_bind_methods() {
85110}
86111
87112Example::Example () {
88- UtilityFunctions::print (" Constructor." );
113+ UtilityFunctions::print (" Example Constructor." );
89114}
90115
91116Example::~Example () {
92- UtilityFunctions::print (" Destructor." );
117+ UtilityFunctions::print (" Example Destructor." );
93118}
94119
95120// Methods.
@@ -115,15 +140,19 @@ Viewport *Example::return_something_const() const {
115140 return nullptr ;
116141}
117142
118- ExampleRef *Example::return_extended_ref () const {
119- return memnew (ExampleRef ());
143+ Ref<ExampleRef> Example::return_extended_ref () const {
144+ // 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.
145+ // We should never instantiate the object directly such as this:
146+ // return memnew(ExampleRef());
147+
148+ Ref<ExampleRef> ref;
149+ ref.instantiate ();
150+ return ref;
120151}
121152
122153Ref<ExampleRef> Example::extended_ref_checks (Ref<ExampleRef> p_ref) const {
123154 Ref<ExampleRef> ref;
124155 ref.instantiate ();
125- // TODO the returned value gets dereferenced too early and return a null object otherwise.
126- ref->reference ();
127156 UtilityFunctions::print (" Example ref checks called with value: " , p_ref->get_instance_id (), " , returning value: " , ref->get_instance_id ());
128157 return ref;
129158}
@@ -165,6 +194,14 @@ Vector2 Example::get_custom_position() const {
165194 return custom_position;
166195}
167196
197+ void Example::set_ref_obj (const Ref<ExampleRef> p_ref) {
198+ ref_obj = p_ref;
199+ }
200+
201+ Ref<ExampleRef> Example::get_ref_obj () const {
202+ return ref_obj;
203+ }
204+
168205// Virtual function override.
169206bool Example::_has_point (const Vector2 &point) const {
170207 Label *label = get_node<Label>(" Label" );
0 commit comments