resources.rst 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. .. _doc_resources:
  2. Resources
  3. =========
  4. Nodes and resources
  5. -------------------
  6. So far, :ref:`Node <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. though, another datatype that is equally as important. That is
  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:`Sample <class_Sample>`,
  22. :ref:`AudioStream <class_AudioStream>`,
  23. :ref:`Font <class_Font>`,
  24. :ref:`Translation <class_Translation>`,
  25. etc.
  26. When Godot saves or loads (from disk) a scene (.scn or .xml), an image
  27. (png, jpg), a scrit (.gd) or pretty much anything, that file is
  28. considered a resource.
  29. When a resource is loaded from disk, **it is always loaded once**. That
  30. means, if there is a copy of that resource already loaded in memory,
  31. trying to load the resource again will just return the same copy again
  32. and again. This corresponds with the fact that resources are just data
  33. containers, so there is no need to have them duplicated.
  34. Typically, every object in Godot (Node, Resource, or anything else) can
  35. export properties, properties can be of many types (like a string,
  36. integer, Vector2, etc) and one of those types can be a resource. This
  37. means that both nodes and resources can contain resources as properties.
  38. To make it a litle more visual:
  39. .. image:: /img/nodes_resources.png
  40. External vs built-in
  41. --------------------
  42. The resource properties can reference resources in two ways,
  43. *external* (on disk) or **built-in**.
  44. To be more specific, here's a :ref:`Texture <class_Texture>`
  45. in a :ref:`Sprite <class_Sprite>` node:
  46. .. image:: /img/spriteprop.png
  47. Pressing the the ">" button the right side of the preview, allows to
  48. view and edit the resources properties. One of the properties (path)
  49. shows where did it come from. In this case, it came from a png image.
  50. .. image:: /img/resourcerobi.png
  51. When the resource comes from a file, it is considered an *external*
  52. resource. If the path property is erased (or never had a path o begin
  53. with), it is then considered a built-in resource.
  54. For example, if the path \`"res://robi.png"\` is erased from the "path"
  55. property in the above example, and then the scene is saved, the resource
  56. will be saved inside the .scn scene file, no longer referencing the
  57. external "robi.png". However, even if saved as built-in, and even though
  58. the scene can be instanced multiple times, the resource will still
  59. always be loaded once. That means, different Robi robot scenes instanced
  60. at the same time will still share the same image.
  61. Loading resources from code
  62. ---------------------------
  63. Loading resources from code is easy, there are two ways to do it. The
  64. first is to use load(), like this:
  65. ::
  66. func _ready():
  67. var res = load("res://robi.png") # resource is loaded when line is executed
  68. get_node("sprite").set_texture(res)
  69. The second way is more optimal, but only works with a string constant
  70. parameter, because it loads the resource at compile-time.
  71. ::
  72. func _ready():
  73. var res = preload("res://robi.png") # resource is loaded at compile time
  74. get_node("sprite").set_texture(res)
  75. Loading scenes
  76. --------------
  77. Scenes are also resources, but there is a catch. Scenes saved to disk
  78. are resources of type :ref:`PackedScene <class_PackedScene>`,
  79. this means that the scene is packed inside a resource.
  80. To obtain an instance of the scene, the method
  81. :ref:`PackedScene.instance() <class_PackedScene_instance>`
  82. must be used.
  83. ::
  84. func _on_shoot():
  85. var bullet = preload("res://bullet.scn").instance()
  86. add_child(bullet)
  87. This method creates the nodes in hierarchy, configures them (sets all
  88. the properties) and returns the root node of the scene, which can be
  89. added to any other node.
  90. The approach has several advantages. As the
  91. :ref:`PackedScene.instance() <class_PackedScene_instance>`
  92. function is pretty fast, adding extra content to the scene can be done
  93. efficiently. New enemies, bullets, effects, etc can be added or
  94. removed quickly, without having to load them again from disk each
  95. time. It is important to remember that, as always, images, meshes, etc
  96. are all shared between the scene instances.
  97. Freeing resources
  98. -----------------
  99. Resource extends from :ref:`Reference <class_Reference>`.
  100. As such, when a resource is no longer in use, it will automatically free
  101. itelf. Since, in most cases, Resources are contained in Nodes, scripts
  102. or other resources, when a node is removed or freed, all the children
  103. resources are freed too.
  104. Scripting
  105. ---------
  106. Like any object in Godot, not just nodes, resources can be scripted too.
  107. However, there isn't generally much of a win, as resources are just data
  108. containers.