@@ -12,59 +12,72 @@ General definition
1212------------------
1313
1414:ref: `Object <class_object >` is the base class for almost everything. Most classes in Godot
15- inherit directly or indirectly from it. Objects provide reflection and
16- editable properties, and declaring them is a matter of using a single
15+ inherit directly or indirectly from it. Declaring them is a matter of using a single
1716macro like this:
1817
1918.. code-block :: cpp
2019
2120 class CustomObject : public Object {
22-
23- GDCLASS(CustomObject, Object); // this is required to inherit
21+ GDCLASS(CustomObject, Object); // This is required to inherit from Object.
2422 };
2523
26- This adds a lot of functionality to Objects. For example :
24+ Objects come with a lot of built-in functionality, like reflection and editable properties :
2725
2826.. code-block :: cpp
2927
30- obj = memnew(CustomObject);
28+ CustomObject * obj = memnew(CustomObject);
3129 print_line("Object class: ", obj->get_class()); // print object class
3230
33- obj2 = Object::cast_to<OtherClass>(obj); // converting between classes, this also works without RTTI enabled.
31+ OtherClass * obj2 = Object::cast_to<OtherClass>(obj); // Converting between classes, similar to dynamic_cast
3432
3533 References:
3634~~~~~~~~~~~
3735
3836- `core/object/object.h <https://github.com/godotengine/godot/blob/master/core/object/object.h >`__
3937
40- Registering an Object
41- ---------------------
42-
43- ClassDB is a static class that holds the entire list of registered
44- classes that inherit from Object, as well as dynamic bindings to all
45- their methods properties and integer constants.
38+ Registering Object classes
39+ --------------------------
4640
47- Classes are registered by calling:
41+ Most `` Object `` subclasses are registered by calling `` GDREGISTER_CLASS ``.
4842
4943.. code-block :: cpp
5044
51- ClassDB::register_class<MyCustomClass>( )
45+ GDREGISTER_CLASS(MyCustomClass )
5246
53- Registering it will allow the class to be instanced by scripts, code, or
54- creating them again when deserializing.
47+ This will register it as a named, public class in ``ClassDB ``, which will allow the class to be instantiated by scripts,
48+ code, or by deserialization. Note that classes registered as ``GDREGISTER_CLASS `` should expect to be instantiated or
49+ freed automatically, like by the editor or the documentation systems.
5550
56- Registering as virtual is the same but it can't be instanced.
51+ Besides `` GDREGISTER_CLASS ``, there are a few other modes of privateness:
5752
5853.. code-block :: cpp
5954
60- ClassDB::register_virtual_class<MyCustomClass>()
55+ // Registers the class publicly, but prevents automatic instantiation through ClassDB.
56+ GDREGISTER_VIRTUAL_CLASS(MyCustomClass);
57+
58+ // Registers the class publicly, but prevents all instantiation through ClassDB.
59+ GDREGISTER_ABSTRACT_CLASS(MyCustomClass);
60+
61+ // Registers the class in ClassDB, but marks it as private,
62+ // such that it is not visible by scripts or extensions.
63+ // This is the same as not registering the class explicitly at all
64+ // - in this case, the class is registered as internal automatically
65+ // when it is first constructed.
66+ GDREGISTER_INTERNAL_CLASS(MyCustomClass);
67+
68+ // Registers the class such that it is only available at runtime (but not in the editor).
69+ GDREGISTER_RUNTIME_CLASS(MyCustomClass);
70+
71+ It is also possible to use ``GDSOFTCLASS(MyCustomClass, SuperClass) `` instead of ``GDCLASS(MyCustomClass, SuperClass) ``.
72+ Classes defined this way are not registered in ``ClassDB `` at all. This is sometimes used for platform-specific subclasses.
73+
74+ Registering bindings
75+ ~~~~~~~~~~~~~~~~~~~~
6176
6277Object-derived classes can override the static function
63- ``static void _bind_methods() ``. When one class is registered, this
78+ ``static void _bind_methods() ``. When the class is registered, this
6479static function is called to register all the object methods,
65- properties, constants, etc. It's only called once. If an Object derived
66- class is instanced but has not been registered, it will be registered as
67- virtual automatically.
80+ properties, constants, etc. It's only called once.
6881
6982Inside ``_bind_methods ``, there are a couple of things that can be done.
7083Registering functions is one:
@@ -95,12 +108,12 @@ documented as thoroughly, the ``D_METHOD()`` macro can safely be ignored and a
95108string passing the name can be passed for brevity.
96109
97110References:
98- ~~~~~~~~~~~
111+ ^^^^^^^^^^^
99112
100113- `core/object/class_db.h <https://github.com/godotengine/godot/blob/master/core/object/class_db.h >`__
101114
102115Constants
103- ---------
116+ ~~~~~~~~~
104117
105118Classes often have enums such as:
106119
@@ -126,7 +139,7 @@ The constants can also be bound inside ``_bind_methods``, by using:
126139 BIND_CONSTANT(MODE_SECOND);
127140
128141 Properties (set/get)
129- --------------------
142+ ~~~~~~~~~~~~~~~~~~~~
130143
131144Objects export properties, properties are useful for the following:
132145
@@ -181,7 +194,7 @@ This creates the property using the setter and the getter.
181194.. _doc_binding_properties_using_set_get_property_list :
182195
183196Binding properties using ``_set ``/``_get ``/``_get_property_list ``
184- -----------------------------------------------------------------
197+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
185198
186199An additional method of creating properties exists when more flexibility
187200is desired (i.e. adding or removing properties on context).
@@ -201,26 +214,9 @@ call).
201214 This is also a little less efficient since ``p_property `` must be
202215compared against the desired names in serial order.
203216
204- Dynamic casting
205- ---------------
206-
207- Godot provides dynamic casting between Object-derived classes, for
208- example:
209-
210- .. code-block :: cpp
211-
212- void somefunc(Object *some_obj) {
213-
214- Button *button = Object::cast_to<Button>(some_obj);
215- }
216-
217- If cast fails, NULL is returned. This system uses RTTI, but it also
218- works fine (although a bit slower) when RTTI is disabled. This is useful
219- on platforms where a small binary size is ideal, such as HTML5 or
220- consoles (with low memory footprint).
221217
222218Signals
223- -------
219+ ~~~~~~~
224220
225221Objects can have a set of signals defined (similar to Delegates in other
226222languages). This example shows how to connect to them:
@@ -244,35 +240,108 @@ Adding signals to a class is done in ``_bind_methods``, using the
244240
245241 ADD_SIGNAL(MethodInfo("been_killed"))
246242
247- Notifications
248- -------------
243+ Object ownership and casting
244+ ----------------------------
249245
250- All objects in Godot have a :ref: `_notification <class_Object_private_method__notification >`
251- method that allows it to respond to engine level callbacks that may relate to it.
252- More information can be found on the :ref: `doc_godot_notifications ` page.
246+ Objects are allocated on the heap. There are two different ownership models:
253247
254- References
255- ----------
248+ - Objects derived from ``RefCounted `` are reference counted.
249+ - All other objects are manually memory managed.
250+
251+ The ownership models are fundamentally different. Refer to the section for each respectively to learn about how to
252+ create, store, and free the object.
256253
257- :ref: `RefCounted <class_RefCounted >` inherits from Object and holds a
258- reference count. It is the base for reference counted object types.
259- Declaring them must be done using Ref<> template. For example:
254+ When you do not know whether an object passed to you (via ``Object * ``) is ``RefCounted ``, and you need to store it,
255+ you should store its ``ObjectID `` rather than a pointer (as explained below, in the manual memory management section).
256+
257+ When an object is passed to you via :ref: `Variant<class_Variant> `, especially when using deferred callbacks, it is
258+ possible that the contained ``Object * `` was already freed by the time your function runs.
259+ Instead of converting directly to ``Object * ``, you should use ``get_validated_object ``:
260+
261+ .. code-block :: cpp
262+
263+ void do_something(Variant p_variant) {
264+ Object *object = p_variant.get_validated_object();
265+ ERR_FAIL_NULL(object);
266+ }
267+
268+ Manual memory management
269+ ~~~~~~~~~~~~~~~~~~~~~~~~
270+
271+ Manually memory managed objects are created using ``memnew `` and freed using ``memdelete ``:
260272
261273.. code-block :: cpp
262274
263- class MyReference: public RefCounted {
275+ Node *node = memnew(Node);
276+ // ...
277+ memdelete(node);
278+ node = nullptr;
279+
280+ When you are not the sole owner of an object, storing a pointer to it is dangerous: Any other references to the object
281+ may call ``memdelete `` at any time, causing your pointer to become a dangling pointer, which will eventually result in a
282+ crash.
283+
284+ When storing objects you are not the only owner of, you should store its ``ObjectID `` rather than a pointer:
285+
286+ .. code-block :: cpp
287+
288+ Node *node = memnew(Node);
289+ ObjectID node_id = node.get_instance_id();
290+ // ...
291+ Object *maybe_node = ObjectDB::get_instance(node_id);
292+ ERR_FAIL_NULL(maybe_node); // Node may have been freed in between calls.
293+
294+ ``RefCounted `` memory management
295+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
296+
297+ :ref: `RefCounted <class_RefCounted >` subclasses are memory managed with
298+ `reference counting semantics <https://en.wikipedia.org/wiki/Reference_counting >`__.
299+
300+ They are constructed using ``memnew ``, and should be stored in ``Ref `` instances. When the last ``Ref `` instance is
301+ dropped, the object automatically self-destructs.
302+
303+ .. code-block :: cpp
304+
305+ class MyRefCounted: public RefCounted {
264306 GDCLASS(MyReference, RefCounted);
265307 };
266308
267- Ref<MyReference> myref(memnew(MyReference));
309+ Ref<MyRefCounted> my_ref = memnew(MyRefCounted);
310+ // ...
311+ my_ref->get_class_name(); // Calls to Ref are always safe because of shared ownership.
268312
269- ``myref `` is reference counted. It will be freed when no more Ref<>
270- templates point to it.
313+ You should never call ``memdelete `` for ``RefCounted `` subclasses, because there may be other owners of it.
314+
315+ You should also never store ``RefCounted `` subclasses using raw pointers, for example
316+ ``RefCounted *object = memnew(RefCounted) ``. This is unsafe because other owners may destruct the object, leaving you
317+ with a dangling pointer, which will eventually result in a crash.
271318
272319References:
273- ~~~~~~~~~~~
320+ ^^^^^^^^^^^
321+
322+ - `core/object/ref_counted.h <https://github.com/godotengine/godot/blob/master/core/object/ref_counted.h >`__
323+
324+ Dynamic casting
325+ ~~~~~~~~~~~~~~~
326+
327+ Godot provides dynamic casting between Object-derived classes, for example:
328+
329+ .. code-block :: cpp
330+
331+ void somefunc(Object *some_obj) {
332+ Button *button = Object::cast_to<Button>(some_obj);
333+ }
334+
335+ If cast fails, ``nullptr `` is returned. This works the same as ``dynamic_cast ``, but does not use
336+ `C++ .RTTI <https://en.wikipedia.org/wiki/Run-time_type_information >`__.
337+
338+ Notifications
339+ -------------
340+
341+ All objects in Godot have a :ref: `_notification <class_Object_private_method__notification >`
342+ method that allows it to respond to engine level callbacks that may relate to it.
343+ More information can be found on the :ref: `doc_godot_notifications ` page.
274344
275- - `core/object/reference.h <https://github.com/godotengine/godot/blob/master/core/object/ref_counted.h >`__
276345
277346Resources
278347----------
@@ -291,7 +360,7 @@ References:
291360- `core/io/resource.h <https://github.com/godotengine/godot/blob/master/core/io/resource.h >`__
292361
293362Resource loading
294- ----------------
363+ ~~~~~~~~~~~~~~~~
295364
296365Resources can be loaded with the ResourceLoader API, like this:
297366
@@ -307,12 +376,12 @@ the same time.
307376- resourceinteractiveloader (TODO)
308377
309378References:
310- ~~~~~~~~~~~
379+ ^^^^^^^^^^^
311380
312381- `core/io/resource_loader.h <https://github.com/godotengine/godot/blob/master/core/io/resource_loader.h >`__
313382
314383Resource saving
315- ---------------
384+ ~~~~~~~~~~~~~~~
316385
317386Saving a resource can be done with the resource saver API:
318387
@@ -326,6 +395,6 @@ be bundled with the saved resource and assigned sub-IDs, like
326395``res://someresource.res::1 ``. This also helps to cache them when loaded.
327396
328397References:
329- ~~~~~~~~~~~
398+ ^^^^^^^^^^^
330399
331400- `core/io/resource_saver.h <https://github.com/godotengine/godot/blob/master/core/io/resource_saver.h >`__
0 commit comments