resources.rst 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. .. _doc_resources:
  2. Resources
  3. =========
  4. Nodes and resources
  5. -------------------
  6. So far, :ref:`Nodes <class_Node>`
  7. have been the most important datatype in Godot as most of the behaviors
  8. and features of the engine are implemented through them. There is
  9. another datatype that is equally important:
  10. :ref:`Resource <class_Resource>`.
  11. Where *Nodes* focus on behaviors, such as drawing a sprite, drawing a
  12. 3D model, physics, GUI controls, etc,
  13. **Resources** are mere **data containers**. This means that they don't
  14. do any action nor process any information. Resources just contain
  15. data.
  16. Examples of resources are
  17. :ref:`Texture <class_Texture>`,
  18. :ref:`Script <class_Script>`,
  19. :ref:`Mesh <class_Mesh>`,
  20. :ref:`Animation <class_Animation>`,
  21. :ref:`AudioStream <class_AudioStream>`,
  22. :ref:`Font <class_Font>`,
  23. :ref:`Translation <class_Translation>`,
  24. etc.
  25. When Godot saves or loads (from disk) a scene (.tscn or .scn), an image
  26. (png, jpg), a script (.gd) or pretty much anything, that file is
  27. considered a resource.
  28. When a resource is loaded from disk, **it is always loaded once**. That
  29. means, if there is a copy of that resource already loaded in memory,
  30. trying to load the resource again will return the same copy again
  31. and again. This corresponds with the fact that resources are just data
  32. containers, so there is no need to have them duplicated.
  33. Typically, every object in Godot (Node, Resource, or anything else) can
  34. export properties. Properties can be of many types (like a string,
  35. integer, Vector2, etc) and one of those types can be a resource. This
  36. means that both nodes and resources can contain resources as properties.
  37. To make it a little more visual:
  38. .. image:: img/nodes_resources.png
  39. External vs built-in
  40. --------------------
  41. The resource properties can reference resources in two ways,
  42. *external* (on disk) or **built-in**.
  43. To be more specific, here's a :ref:`Texture <class_Texture>`
  44. in a :ref:`Sprite <class_Sprite>` node:
  45. .. image:: img/spriteprop.png
  46. Pressing the ">" button on the right side of the preview allows us to
  47. view and edit the resources properties. One of the properties (path)
  48. shows where it comes from. In this case, it comes from a png image.
  49. .. image:: img/resourcerobi.png
  50. When the resource comes from a file, it is considered an *external*
  51. resource. If the path property is erased (or it never had a path to
  52. begin with), it is considered a built-in resource.
  53. For example, if the path \`"res://robi.png"\` is erased from the "path"
  54. property in the above example, and then the scene is saved, the resource
  55. will be saved inside the .tscn scene file, no longer referencing the
  56. external "robi.png". However, even if saved as built-in, and even though
  57. the scene can be instanced multiple times, the resource will always
  58. be loaded only once. That means, different Robi robot scenes instanced
  59. at the same time will still share the same image.
  60. Loading resources from code
  61. ---------------------------
  62. Loading resources from code is easy. There are two ways to do it. The
  63. first is to use load(), like this:
  64. .. tabs::
  65. .. code-tab:: gdscript GDScript
  66. func _ready():
  67. var res = load("res://robi.png") # resource is loaded when line is executed
  68. get_node("sprite").texture = res
  69. .. code-tab:: csharp
  70. public override void _Ready()
  71. {
  72. var texture = (Texture)GD.Load("res://robi.png"); // resource is loaded when line is executed
  73. var sprite = (Sprite)GetNode("sprite");
  74. sprite.Texture = texture;
  75. }
  76. The second way is more optimal, but only works with a string constant
  77. parameter because it loads the resource at compile-time.
  78. .. tabs::
  79. .. code-tab:: gdscript GDScript
  80. func _ready():
  81. var res = preload("res://robi.png") # resource is loaded at compile time
  82. get_node("sprite").texture = res
  83. .. code-tab:: csharp
  84. // preload() is unavailable in C Sharp
  85. Loading scenes
  86. --------------
  87. Scenes are also resources, but there is a catch. Scenes saved to disk
  88. are resources of type :ref:`PackedScene <class_PackedScene>`. This means that
  89. the scene is packed inside a resource.
  90. To obtain an instance of the scene, the method
  91. :ref:`PackedScene.instance() <class_PackedScene_instance>`
  92. must be used.
  93. .. tabs::
  94. .. code-tab:: gdscript GDScript
  95. func _on_shoot():
  96. var bullet = preload("res://bullet.tscn").instance()
  97. add_child(bullet)
  98. .. code-tab:: csharp
  99. private PackedScene _bulletScene = (PackedScene)GD.Load("res://bullet.tscn");
  100. public void OnShoot()
  101. {
  102. Node bullet = _bulletScene.Instance();
  103. AddChild(bullet);
  104. }
  105. This method creates the nodes in the scene's hierarchy, configures
  106. them (sets all the properties) and returns the root node of the scene,
  107. which can be added to any other node.
  108. The approach has several advantages. As the
  109. :ref:`PackedScene.instance() <class_PackedScene_instance>`
  110. function is pretty fast, adding extra content to the scene can be done
  111. efficiently. New enemies, bullets, effects, etc can be added or
  112. removed quickly, without having to load them again from disk each
  113. time. It is important to remember that, as always, images, meshes, etc
  114. are all shared between the scene instances.
  115. Freeing resources
  116. -----------------
  117. Resource extends from :ref:`Reference <class_Reference>`.
  118. As such, when a resource is no longer in use, it will automatically free
  119. itself. Since, in most cases, Resources are contained in Nodes, scripts
  120. or other resources, when a node is removed or freed, all the children
  121. resources are freed too.
  122. Scripting
  123. ---------
  124. Like any object in Godot, not just nodes, resources can be scripted,
  125. too. However, there isn't generally much of an advantage, as resources
  126. are just data containers.