mirror of
https://github.com/godotengine/godot-docs.git
synced 2026-01-04 14:11:02 +03:00
Reference -> RefCounted
This commit is contained in:
@@ -247,14 +247,14 @@ More information can be found on the :ref:`doc_godot_notifications` page.
|
||||
References
|
||||
----------
|
||||
|
||||
:ref:`Reference <class_ReferenceCounted>` inherits from Object and holds a
|
||||
:ref:`RefCounted <class_RefCounted>` inherits from Object and holds a
|
||||
reference count. It is the base for reference counted object types.
|
||||
Declaring them must be done using Ref<> template. For example:
|
||||
|
||||
.. code-block:: cpp
|
||||
|
||||
class MyReference: public Reference {
|
||||
GDCLASS(MyReference, Reference);
|
||||
class MyReference: public RefCounted {
|
||||
GDCLASS(MyReference, RefCounted);
|
||||
};
|
||||
|
||||
Ref<MyReference> myref(memnew(MyReference));
|
||||
@@ -265,7 +265,7 @@ templates point to it.
|
||||
References:
|
||||
~~~~~~~~~~~
|
||||
|
||||
- `core/object/reference.h <https://github.com/godotengine/godot/blob/master/core/object/reference.h>`__
|
||||
- `core/object/reference.h <https://github.com/godotengine/godot/blob/master/core/object/ref_counted.h>`__
|
||||
|
||||
Resources:
|
||||
----------
|
||||
|
||||
@@ -121,7 +121,7 @@ node, including classes it extends, like ``Node2D``, ``CanvasItem``, and
|
||||
``Node``.
|
||||
|
||||
.. note:: In GDScript, if you omit the line with the ``extends`` keyword, your
|
||||
class will implicitly extend :ref:`Reference <class_Reference>`, which
|
||||
class will implicitly extend :ref:`RefCounted <class_RefCounted>`, which
|
||||
Godot uses to manage your application's memory.
|
||||
|
||||
Inherited properties include the ones you can see in the Inspector dock, like
|
||||
|
||||
@@ -29,7 +29,7 @@ is to get a reference to an existing object from another acquired instance.
|
||||
Object obj = node.Object; // Property access.
|
||||
Object obj = node.GetObject(); // Method access.
|
||||
|
||||
The same principle applies for :ref:`Reference <class_Reference>` objects.
|
||||
The same principle applies for :ref:`RefCounted <class_RefCounted>` objects.
|
||||
While users often access :ref:`Node <class_Node>` and
|
||||
:ref:`Resource <class_Resource>` this way, alternative measures are available.
|
||||
|
||||
|
||||
@@ -130,7 +130,7 @@ consider:
|
||||
would be to unload the entire script. If they are instead loaded
|
||||
properties, then one can set them to ``null`` and remove all references
|
||||
to the resource entirely (which, as a
|
||||
:ref:`Reference <class_Reference>`-extending type, will cause the
|
||||
:ref:`RefCounted <class_RefCounted>`-extending type, will cause the
|
||||
resources to delete themselves from memory).
|
||||
|
||||
Large levels: static vs. dynamic
|
||||
|
||||
@@ -41,8 +41,8 @@ available from the ``ClassDB``.
|
||||
.. note::
|
||||
|
||||
Even scripts that don't use the ``extends`` keyword implicitly inherit from the engine's base
|
||||
:ref:`Reference <class_Reference>` class. As a result, you can instantiate scripts without the
|
||||
``extends`` keyword from code. Since they extend ``Reference`` though, you cannot attach them to
|
||||
:ref:`RefCounted <class_RefCounted>` class. As a result, you can instantiate scripts without the
|
||||
``extends`` keyword from code. Since they extend ``RefCounted`` though, you cannot attach them to
|
||||
a :ref:`Node <class_Node>`.
|
||||
|
||||
Scenes
|
||||
|
||||
@@ -52,7 +52,7 @@ Most Godot nodes and resources contain these RIDs from the servers internally, a
|
||||
be obtained with different functions. In fact, anything that inherits :ref:`Resource <class_Resource>`
|
||||
can be directly casted to an RID. Not all resources contain an RID, though: in such cases, the RID will be empty. The resource can then be passed to server APIs as an RID.
|
||||
|
||||
.. Warning:: Resources are reference-counted (see :ref:`Reference <class_Reference>`), and
|
||||
.. Warning:: Resources are reference-counted (see :ref:`RefCounted <class_RefCounted>`), and
|
||||
references to a resource's RID are *not* counted when determining whether
|
||||
the resource is still in use. Make sure to keep a reference to the resource
|
||||
outside the server, or else both it and its RID will be erased.
|
||||
|
||||
@@ -80,7 +80,7 @@ There are two ways to load resources from code. First, you can use the ``load()`
|
||||
|
||||
func _ready():
|
||||
# Godot loads the Resource when it reads this very line.
|
||||
var imported_resource = load("res://robi.png")
|
||||
var imported_resource = load("res://robi.png")
|
||||
$sprite.texture = imported_resource
|
||||
|
||||
.. code-tab:: csharp
|
||||
@@ -165,7 +165,7 @@ This comes with many distinct advantages over alternative data
|
||||
structures, such as JSON, CSV, or custom TXT files. Users can only import these
|
||||
assets as a :ref:`Dictionary <class_Dictionary>` (JSON) or as a
|
||||
:ref:`File <class_File>` to parse. What sets Resources apart is their
|
||||
inheritance of :ref:`Object <class_Object>`, :ref:`Reference <class_Reference>`,
|
||||
inheritance of :ref:`Object <class_Object>`, :ref:`RefCounted <class_RefCounted>`,
|
||||
and :ref:`Resource <class_Resource>` features:
|
||||
|
||||
- They can define constants, so constants from other data fields or objects are not needed.
|
||||
@@ -210,7 +210,7 @@ Attach a script to it named ``bot_stats.gd`` (or just create a new script, and t
|
||||
.. tabs::
|
||||
.. code-tab:: gdscript GDScript
|
||||
extends Resource
|
||||
|
||||
|
||||
@export var health : int
|
||||
@export var sub_resource : Resource
|
||||
@export var strings : PackedStringArray
|
||||
@@ -222,7 +222,7 @@ Attach a script to it named ``bot_stats.gd`` (or just create a new script, and t
|
||||
health = p_health
|
||||
sub_resource = p_sub_resource
|
||||
strings = p_strings
|
||||
|
||||
|
||||
.. code-tab:: csharp
|
||||
|
||||
// BotStats.cs
|
||||
@@ -252,9 +252,9 @@ Attach a script to it named ``bot_stats.gd`` (or just create a new script, and t
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Now, create a :ref:`CharacterBody3D <class_CharacterBody3D>`, name it ``Bot``, and add the following script to it:
|
||||
|
||||
|
||||
.. tabs::
|
||||
.. code-tab:: gdscript GDScript
|
||||
extends CharacterBody3D
|
||||
@@ -265,9 +265,9 @@ Now, create a :ref:`CharacterBody3D <class_CharacterBody3D>`, name it ``Bot``, a
|
||||
# Uses an implicit, duck-typed interface for any 'health'-compatible resources.
|
||||
if stats:
|
||||
stats.health = 10
|
||||
print(stats.health)
|
||||
print(stats.health)
|
||||
# Prints "10"
|
||||
|
||||
|
||||
.. code-tab:: csharp
|
||||
// Bot.cs
|
||||
using System;
|
||||
@@ -287,7 +287,7 @@ Now, create a :ref:`CharacterBody3D <class_CharacterBody3D>`, name it ``Bot``, a
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Now, select the :ref:`CharacterBody3D <class_CharacterBody3D>` node which we named ``bot``, and drag&drop the ``bot_stats.tres`` resource onto the Inspector. It should print 10! Obviously, this setup can be used for more advanced features than this, but as long you really understand *how* it all worked, you should figure out everything else related to Resources.
|
||||
|
||||
.. note::
|
||||
|
||||
Reference in New Issue
Block a user