resources.rst 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. Resources
  2. =========
  3. Nodes and Resources
  4. -------------------
  5. So far, `Node <https://github.com/okamstudio/godot/wiki/class_node>`__
  6. have been the most important datatype in Godot, as most of the behaviors
  7. and features of the engine are implemented through them. There is,
  8. though, another datatype that is equally as important. That is
  9. `Resource <https://github.com/okamstudio/godot/wiki/class_resource.>`__.
  10. | Where *Nodes* focus on behaviors, such as drawing a sprite, drawing a
  11. 3D model, physics, GUI controls, etc,
  12. | **Resources** are mere **data containers**. This means that they don't
  13. do any action nor process any information. Resources just contain
  14. data.
  15. Examples of resources are
  16. `Texture <https://github.com/okamstudio/godot/wiki/class_texture>`__,
  17. `Script <https://github.com/okamstudio/godot/wiki/class_script>`__,
  18. `Mesh <https://github.com/okamstudio/godot/wiki/class_mesh>`__,
  19. `Animation <https://github.com/okamstudio/godot/wiki/class_animation>`__,
  20. `Sample <https://github.com/okamstudio/godot/wiki/class_sample>`__,
  21. `AudioStream <https://github.com/okamstudio/godot/wiki/class_audiostream>`__,
  22. `Font <https://github.com/okamstudio/godot/wiki/class_font>`__,
  23. `Translation <https://github.com/okamstudio/godot/wiki/class_translation>`__,
  24. etc.
  25. When Godot saves or loads (from disk) a scene (.scn or .xml), an image
  26. (png, jpg), a scrit (.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 just 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 litle 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
  44. `Texture <https://github.com/okamstudio/godot/wiki/class_texture>`__
  45. in a
  46. `Sprite <https://github.com/okamstudio/godot/wiki/class_sprite>`__
  47. node:
  48. .. image:: /img/spriteprop.png
  49. Pressing the the ">" button the right side of the preview, allows to
  50. view and edit the resources properties. One of the properties (path)
  51. shows where did it come from. In this case, it came from a png image.
  52. .. image:: /img/resourcerobi.png
  53. When the resource comes from a file, it is considered an *external*
  54. resource. If the path property is erased (or never had a path o begin
  55. with), it is then considered a built-in resource.
  56. For example, if the path \`"res://robi.png"\` is erased from the "path"
  57. property in the above example, and then the scene is saved, the resource
  58. will be saved inside the .scn scene file, no longer referencing the
  59. external "robi.png". However, even if saved as built-in, and even though
  60. the scene can be instanced multiple times, the resource will still
  61. always be loaded once. That means, different Robi robot scenes instanced
  62. at the same time will still share the same image.
  63. Loading Resources from Code
  64. ---------------------------
  65. Loading resources from code is easy, there are two ways to do it. The
  66. first is to use load(), like this:
  67. ::
  68. func _ready():
  69. var res = load("res://robi.png") h1. resource is loaded when line is executed
  70. get_node("sprite").set_texture(res)
  71. The second way is more optimal, but only works with a string constant
  72. parameter, because it loads the resource at compile-time.
  73. ::
  74. func _ready():
  75. var res = preload("res://robi.png") h1. resource is loaded at compile time
  76. get_node("sprite").set_texture(res)
  77. Loading Scenes
  78. --------------
  79. Scenes are also resources, but there is a catch. Scenes saved to disk
  80. are resources of type
  81. `PackedScene <https://github.com/okamstudio/godot/wiki/class_packedscene>`__,
  82. this means that the scene is packed inside a resource.
  83. To obtain an instance of the scene, the method
  84. `instance() <https://github.com/okamstudio/godot/wiki/class_packedscene#instance>`__
  85. must be used.
  86. ::
  87. func _on_shoot():
  88. var bullet = preload("res://bullet.scn").instance()
  89. add_child(bullet)
  90. | This method creates the nodes in hierarchy, configures them (sets all
  91. the properties) and returns the root node of the scene, which can be
  92. added to any other node.
  93. | The approach has several advantages. As the
  94. `instance <https://github.com/okamstudio/godot/wiki/class_packedscene#instance()>`__
  95. function is pretty fast, adding extra content to the scene can be done
  96. efficiently. New enemies, bullets, effects, etc can be added or
  97. removed quickly, without having to load them again from disk each
  98. time. It is important to remember that, as always, images, meshes, etc
  99. are all shared between the scene instances.
  100. Freeing Resources
  101. -----------------
  102. Resource extends from
  103. `Reference <https://github.com/okamstudio/godot/wiki/class_reference>`__.
  104. As such, when a resource is no longer in use, it will automatically free
  105. itelf. Since, in most cases, Resources are contained in Nodes, scripts
  106. or other resources, when a node is removed or freed, all the children
  107. resources are freed too.
  108. Scripting
  109. ---------
  110. Like any object in Godot, not just nodes, Resources can be scripted too.
  111. However, there isn't generally much of a win, as resources are just data
  112. containers.
  113. *Juan Linietsky, Ariel Manzur, Distributed under the terms of the `CC
  114. By <https://creativecommons.org/licenses/by/3.0/legalcode>`__ license.*