virtualx-engine-docs/_sources/classes/class_resource.rst.txt
2024-10-23 09:41:33 -07:00

280 lines
16 KiB
ReStructuredText

:github_url: hide
.. DO NOT EDIT THIS FILE!!!
.. Generated automatically from Godot engine sources.
.. Generator: https://github.com/godotengine/godot/tree/3.6/doc/tools/make_rst.py.
.. XML source: https://github.com/godotengine/godot/tree/3.6/doc/classes/Resource.xml.
.. _class_Resource:
Resource
========
**Inherits:** :ref:`Reference<class_Reference>` **<** :ref:`Object<class_Object>`
**Inherited By:** :ref:`Animation<class_Animation>`, :ref:`AnimationNode<class_AnimationNode>`, :ref:`AnimationNodeStateMachinePlayback<class_AnimationNodeStateMachinePlayback>`, :ref:`AnimationNodeStateMachineTransition<class_AnimationNodeStateMachineTransition>`, :ref:`AudioBusLayout<class_AudioBusLayout>`, :ref:`AudioEffect<class_AudioEffect>`, :ref:`AudioStream<class_AudioStream>`, :ref:`BakedLightmapData<class_BakedLightmapData>`, :ref:`BitMap<class_BitMap>`, :ref:`ButtonGroup<class_ButtonGroup>`, :ref:`CryptoKey<class_CryptoKey>`, :ref:`CubeMap<class_CubeMap>`, :ref:`Curve<class_Curve>`, :ref:`Curve2D<class_Curve2D>`, :ref:`Curve3D<class_Curve3D>`, :ref:`DynamicFontData<class_DynamicFontData>`, :ref:`EditorSettings<class_EditorSettings>`, :ref:`EditorSpatialGizmoPlugin<class_EditorSpatialGizmoPlugin>`, :ref:`Environment<class_Environment>`, :ref:`Font<class_Font>`, :ref:`GDNativeLibrary<class_GDNativeLibrary>`, :ref:`GIProbeData<class_GIProbeData>`, :ref:`GLTFAccessor<class_GLTFAccessor>`, :ref:`GLTFAnimation<class_GLTFAnimation>`, :ref:`GLTFBufferView<class_GLTFBufferView>`, :ref:`GLTFCamera<class_GLTFCamera>`, :ref:`GLTFCollider<class_GLTFCollider>`, :ref:`GLTFDocument<class_GLTFDocument>`, :ref:`GLTFDocumentExtension<class_GLTFDocumentExtension>`, :ref:`GLTFLight<class_GLTFLight>`, :ref:`GLTFMesh<class_GLTFMesh>`, :ref:`GLTFNode<class_GLTFNode>`, :ref:`GLTFPhysicsBody<class_GLTFPhysicsBody>`, :ref:`GLTFSkeleton<class_GLTFSkeleton>`, :ref:`GLTFSkin<class_GLTFSkin>`, :ref:`GLTFSpecGloss<class_GLTFSpecGloss>`, :ref:`GLTFState<class_GLTFState>`, :ref:`GLTFTexture<class_GLTFTexture>`, :ref:`GLTFTextureSampler<class_GLTFTextureSampler>`, :ref:`Gradient<class_Gradient>`, :ref:`Image<class_Image>`, :ref:`InputEvent<class_InputEvent>`, :ref:`Material<class_Material>`, :ref:`Mesh<class_Mesh>`, :ref:`MeshLibrary<class_MeshLibrary>`, :ref:`MultiMesh<class_MultiMesh>`, :ref:`NavigationMesh<class_NavigationMesh>`, :ref:`NavigationPolygon<class_NavigationPolygon>`, :ref:`OccluderPolygon2D<class_OccluderPolygon2D>`, :ref:`OccluderShape<class_OccluderShape>`, :ref:`OpenSimplexNoise<class_OpenSimplexNoise>`, :ref:`PackedDataContainer<class_PackedDataContainer>`, :ref:`PackedScene<class_PackedScene>`, :ref:`PhysicsMaterial<class_PhysicsMaterial>`, :ref:`PolygonPathFinder<class_PolygonPathFinder>`, :ref:`RichTextEffect<class_RichTextEffect>`, :ref:`Script<class_Script>`, :ref:`Shader<class_Shader>`, :ref:`Shape<class_Shape>`, :ref:`Shape2D<class_Shape2D>`, :ref:`ShortCut<class_ShortCut>`, :ref:`Skin<class_Skin>`, :ref:`Sky<class_Sky>`, :ref:`SpriteFrames<class_SpriteFrames>`, :ref:`StyleBox<class_StyleBox>`, :ref:`TextFile<class_TextFile>`, :ref:`Texture<class_Texture>`, :ref:`TextureLayered<class_TextureLayered>`, :ref:`Theme<class_Theme>`, :ref:`TileSet<class_TileSet>`, :ref:`Translation<class_Translation>`, :ref:`VideoStream<class_VideoStream>`, :ref:`VisualScriptNode<class_VisualScriptNode>`, :ref:`VisualShaderNode<class_VisualShaderNode>`, :ref:`World<class_World>`, :ref:`World2D<class_World2D>`, :ref:`X509Certificate<class_X509Certificate>`
Base class for serializable objects.
.. rst-class:: classref-introduction-group
Description
-----------
Resource is the base class for all Godot-specific resource types, serving primarily as data containers. Since they inherit from :ref:`Reference<class_Reference>`, resources are reference-counted and freed when no longer in use. They can also be nested within other resources, and saved on disk. :ref:`PackedScene<class_PackedScene>`, one of the most common :ref:`Object<class_Object>`\ s in a Godot project, is also a resource, uniquely capable of storing and instantiating the :ref:`Node<class_Node>`\ s it contains as many times as desired.
In GDScript, resources can loaded from disk by their :ref:`resource_path<class_Resource_property_resource_path>` using :ref:`@GDScript.load<class_@GDScript_method_load>` or :ref:`@GDScript.preload<class_@GDScript_method_preload>`.
The engine keeps a global cache of all loaded resources, referenced by paths (see :ref:`ResourceLoader.has_cached<class_ResourceLoader_method_has_cached>`). A resource will be cached when loaded for the first time and removed from cache once all references are released. When a resource is cached, subsequent loads using its path will return the cached reference.
\ **Note:** In C#, resources will not be freed instantly after they are no longer in use. Instead, garbage collection will run periodically and will free resources that are no longer in use. This means that unused resources will linger on for a while before being removed.
.. rst-class:: classref-introduction-group
Tutorials
---------
- :doc:`Resources <../tutorials/scripting/resources>`
- :doc:`When and how to avoid using nodes for everything <../tutorials/best_practices/node_alternatives>`
.. rst-class:: classref-reftable-group
Properties
----------
.. table::
:widths: auto
+-----------------------------+---------------------------------------------------------------------------------+-----------+
| :ref:`bool<class_bool>` | :ref:`resource_local_to_scene<class_Resource_property_resource_local_to_scene>` | ``false`` |
+-----------------------------+---------------------------------------------------------------------------------+-----------+
| :ref:`String<class_String>` | :ref:`resource_name<class_Resource_property_resource_name>` | ``""`` |
+-----------------------------+---------------------------------------------------------------------------------+-----------+
| :ref:`String<class_String>` | :ref:`resource_path<class_Resource_property_resource_path>` | ``""`` |
+-----------------------------+---------------------------------------------------------------------------------+-----------+
.. rst-class:: classref-reftable-group
Methods
-------
.. table::
:widths: auto
+---------------------------------+------------------------------------------------------------------------------------------------------------------+
| void | :ref:`_setup_local_to_scene<class_Resource_method__setup_local_to_scene>` **(** **)** |virtual| |
+---------------------------------+------------------------------------------------------------------------------------------------------------------+
| :ref:`Resource<class_Resource>` | :ref:`duplicate<class_Resource_method_duplicate>` **(** :ref:`bool<class_bool>` subresources=false **)** |const| |
+---------------------------------+------------------------------------------------------------------------------------------------------------------+
| void | :ref:`emit_changed<class_Resource_method_emit_changed>` **(** **)** |
+---------------------------------+------------------------------------------------------------------------------------------------------------------+
| :ref:`Node<class_Node>` | :ref:`get_local_scene<class_Resource_method_get_local_scene>` **(** **)** |const| |
+---------------------------------+------------------------------------------------------------------------------------------------------------------+
| :ref:`RID<class_RID>` | :ref:`get_rid<class_Resource_method_get_rid>` **(** **)** |const| |
+---------------------------------+------------------------------------------------------------------------------------------------------------------+
| void | :ref:`setup_local_to_scene<class_Resource_method_setup_local_to_scene>` **(** **)** |
+---------------------------------+------------------------------------------------------------------------------------------------------------------+
| void | :ref:`take_over_path<class_Resource_method_take_over_path>` **(** :ref:`String<class_String>` path **)** |
+---------------------------------+------------------------------------------------------------------------------------------------------------------+
.. rst-class:: classref-section-separator
----
.. rst-class:: classref-descriptions-group
Signals
-------
.. _class_Resource_signal_changed:
.. rst-class:: classref-signal
**changed** **(** **)**
Emitted when the resource changes, usually when one of its properties is modified. See also :ref:`emit_changed<class_Resource_method_emit_changed>`.
\ **Note:** This signal is not emitted automatically for custom resources, which means that you need to create a setter and emit the signal yourself.
.. rst-class:: classref-section-separator
----
.. rst-class:: classref-descriptions-group
Property Descriptions
---------------------
.. _class_Resource_property_resource_local_to_scene:
.. rst-class:: classref-property
:ref:`bool<class_bool>` **resource_local_to_scene** = ``false``
.. rst-class:: classref-property-setget
- void **set_local_to_scene** **(** :ref:`bool<class_bool>` value **)**
- :ref:`bool<class_bool>` **is_local_to_scene** **(** **)**
If ``true``, the resource is duplicated for each instance of all scenes using it. At run-time, the resource can be modified in one scene without affecting other instances (see :ref:`PackedScene.instance<class_PackedScene_method_instance>`).
\ **Note:** Changing this property at run-time has no effect on already created duplicate resources.
.. rst-class:: classref-item-separator
----
.. _class_Resource_property_resource_name:
.. rst-class:: classref-property
:ref:`String<class_String>` **resource_name** = ``""``
.. rst-class:: classref-property-setget
- void **set_name** **(** :ref:`String<class_String>` value **)**
- :ref:`String<class_String>` **get_name** **(** **)**
An optional name for this resource. When defined, its value is displayed to represent the resource in the Inspector dock. For built-in scripts, the name is displayed as part of the tab name in the script editor.
\ **Note:** Some resource formats do not support resource names. You can still set the name in the editor or via code, but it will be lost when the resource is reloaded. For example, only built-in scripts can have a resource name, while scripts stored in separate files cannot.
.. rst-class:: classref-item-separator
----
.. _class_Resource_property_resource_path:
.. rst-class:: classref-property
:ref:`String<class_String>` **resource_path** = ``""``
.. rst-class:: classref-property-setget
- void **set_path** **(** :ref:`String<class_String>` value **)**
- :ref:`String<class_String>` **get_path** **(** **)**
The unique path to this resource. If it has been saved to disk, the value will be its filepath. If the resource is exclusively contained within a scene, the value will be the :ref:`PackedScene<class_PackedScene>`'s filepath, followed by a unique identifier.
\ **Note:** Setting this property manually may fail if a resource with the same path has already been previously loaded. If necessary, use :ref:`take_over_path<class_Resource_method_take_over_path>`.
.. rst-class:: classref-section-separator
----
.. rst-class:: classref-descriptions-group
Method Descriptions
-------------------
.. _class_Resource_method__setup_local_to_scene:
.. rst-class:: classref-method
void **_setup_local_to_scene** **(** **)** |virtual|
Override this method to customize the newly duplicated resource created from :ref:`PackedScene.instance<class_PackedScene_method_instance>`, if the original's :ref:`resource_local_to_scene<class_Resource_property_resource_local_to_scene>` is set to ``true``.
\ **Example:** Set a random ``damage`` value to every local resource from an instantiated scene.
::
extends Resource
var damage = 0
func _setup_local_to_scene():
damage = rand_range(10, 40)
.. rst-class:: classref-item-separator
----
.. _class_Resource_method_duplicate:
.. rst-class:: classref-method
:ref:`Resource<class_Resource>` **duplicate** **(** :ref:`bool<class_bool>` subresources=false **)** |const|
Duplicates this resource, returning a new resource with its ``export``\ ed or :ref:`@GlobalScope.PROPERTY_USAGE_STORAGE<class_@GlobalScope_constant_PROPERTY_USAGE_STORAGE>` properties copied from the original.
If ``subresources`` is ``false``, a shallow copy is returned; nested resources within subresources are not duplicated and are shared from the original resource. If ``subresources`` is ``true``, a deep copy is returned; nested subresources will be duplicated and are not shared.
\ **Note:** For custom resources, this method will fail if :ref:`Object._init<class_Object_method__init>` has been defined with required parameters.
.. rst-class:: classref-item-separator
----
.. _class_Resource_method_emit_changed:
.. rst-class:: classref-method
void **emit_changed** **(** **)**
Emits the :ref:`changed<class_Resource_signal_changed>` signal. This method is called automatically for some built-in resources.
\ **Note:** For custom resources, it's recommended to call this method whenever a meaningful change occurs, such as a modified property. This ensures that custom :ref:`Object<class_Object>`\ s depending on the resource are properly updated.
::
var damage setget set_damage
func set_damage(new_value):
if damage != new_value:
damage = new_value
emit_changed()
.. rst-class:: classref-item-separator
----
.. _class_Resource_method_get_local_scene:
.. rst-class:: classref-method
:ref:`Node<class_Node>` **get_local_scene** **(** **)** |const|
If :ref:`resource_local_to_scene<class_Resource_property_resource_local_to_scene>` is set to ``true`` and the resource has been loaded from a :ref:`PackedScene<class_PackedScene>` instantiation, returns the root :ref:`Node<class_Node>` of the scene where this resource is used. Otherwise, returns ``null``.
.. rst-class:: classref-item-separator
----
.. _class_Resource_method_get_rid:
.. rst-class:: classref-method
:ref:`RID<class_RID>` **get_rid** **(** **)** |const|
Returns the :ref:`RID<class_RID>` of this resource (or an empty RID). Many resources (such as :ref:`Texture<class_Texture>`, :ref:`Shape<class_Shape>`, and so on) are high-level abstractions of resources stored in a specialized server (:ref:`VisualServer<class_VisualServer>`, :ref:`PhysicsServer<class_PhysicsServer>`, etc.), so this function will return the original :ref:`RID<class_RID>`.
.. rst-class:: classref-item-separator
----
.. _class_Resource_method_setup_local_to_scene:
.. rst-class:: classref-method
void **setup_local_to_scene** **(** **)**
Calls :ref:`_setup_local_to_scene<class_Resource_method__setup_local_to_scene>`. If :ref:`resource_local_to_scene<class_Resource_property_resource_local_to_scene>` is set to ``true``, this method is automatically called from :ref:`PackedScene.instance<class_PackedScene_method_instance>` by the newly duplicated resource within the scene instance.
.. rst-class:: classref-item-separator
----
.. _class_Resource_method_take_over_path:
.. rst-class:: classref-method
void **take_over_path** **(** :ref:`String<class_String>` path **)**
Sets the :ref:`resource_path<class_Resource_property_resource_path>` to ``path``, potentially overriding an existing cache entry for this path. Further attempts to load an overridden resource by path will instead return this resource.
.. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
.. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
.. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
.. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`