resources.rst 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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
  45. :ref:`Texture <class_Texture>`
  46. in a
  47. :ref:`Sprite <class_Sprite>`
  48. node:
  49. .. image:: /img/spriteprop.png
  50. Pressing the the ">" button the right side of the preview, allows to
  51. view and edit the resources properties. One of the properties (path)
  52. shows where did it come from. In this case, it came from a png image.
  53. .. image:: /img/resourcerobi.png
  54. When the resource comes from a file, it is considered an *external*
  55. resource. If the path property is erased (or never had a path o begin
  56. with), it is then considered a built-in resource.
  57. For example, if the path \`"res://robi.png"\` is erased from the "path"
  58. property in the above example, and then the scene is saved, the resource
  59. will be saved inside the .scn scene file, no longer referencing the
  60. external "robi.png". However, even if saved as built-in, and even though
  61. the scene can be instanced multiple times, the resource will still
  62. always be loaded once. That means, different Robi robot scenes instanced
  63. at the same time will still share the same image.
  64. Loading Resources from Code
  65. ---------------------------
  66. Loading resources from code is easy, there are two ways to do it. The
  67. first is to use load(), like this:
  68. ::
  69. func _ready():
  70. var res = load("res://robi.png") h1. resource is loaded when line is executed
  71. get_node("sprite").set_texture(res)
  72. The second way is more optimal, but only works with a string constant
  73. parameter, because it loads the resource at compile-time.
  74. ::
  75. func _ready():
  76. var res = preload("res://robi.png") h1. resource is loaded at compile time
  77. get_node("sprite").set_texture(res)
  78. Loading Scenes
  79. --------------
  80. Scenes are also resources, but there is a catch. Scenes saved to disk
  81. are resources of type
  82. :ref:`PackedScene <class_PackedScene>`,
  83. this means that the scene is packed inside a resource.
  84. To obtain an instance of the scene, the method
  85. :ref:`PackedScene.instance() <class_PackedScene_instance>`
  86. must be used.
  87. ::
  88. func _on_shoot():
  89. var bullet = preload("res://bullet.scn").instance()
  90. add_child(bullet)
  91. This method creates the nodes in hierarchy, configures them (sets all
  92. the properties) and returns the root node of the scene, which can be
  93. added to any other node.
  94. The approach has several advantages. As the
  95. :ref:`PackedScene.instance() <class_PackedScene_instance>`
  96. function is pretty fast, adding extra content to the scene can be done
  97. efficiently. New enemies, bullets, effects, etc can be added or
  98. removed quickly, without having to load them again from disk each
  99. time. It is important to remember that, as always, images, meshes, etc
  100. are all shared between the scene instances.
  101. Freeing Resources
  102. -----------------
  103. Resource extends from
  104. :ref:`Reference <class_Reference>`.
  105. As such, when a resource is no longer in use, it will automatically free
  106. itelf. Since, in most cases, Resources are contained in Nodes, scripts
  107. or other resources, when a node is removed or freed, all the children
  108. resources are freed too.
  109. Scripting
  110. ---------
  111. Like any object in Godot, not just nodes, Resources can be scripted too.
  112. However, there isn't generally much of a win, as resources are just data
  113. containers.