resources.rst 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. .. _doc_resources:
  2. Resources
  3. =========
  4. Nodes and resources
  5. -------------------
  6. Up to this tutorial, we focused on the :ref:`Node <class_Node>`
  7. class in Godot as that's the one you use to code behavior and
  8. most of the engine's features rely on it. There is
  9. another datatype that is just as important:
  10. :ref:`Resource <class_Resource>`.
  11. *Nodes* give you functionality: they draw sprites, 3D models, simulate physics,
  12. arrange user interfaces, etc. **Resources** are **data containers**. They don't
  13. do anything on their own: instead, nodes use the data contained in resources.
  14. Anything Godot saves or loads from disk is a resource. Be it a scene (a .tscn or
  15. an .scn file), an image, a script... Here are some ``Resource`` examples:
  16. :ref:`Texture <class_Texture>`, :ref:`Script <class_Script>`, :ref:`Mesh
  17. <class_Mesh>`, :ref:`Animation <class_Animation>`, :ref:`AudioStream
  18. <class_AudioStream>`, :ref:`Font <class_Font>`, :ref:`Translation
  19. <class_Translation>`.
  20. When the engine loads a resource from disk, **it only loads it once**. If a copy
  21. of that resource is already in memory, trying to load the resource again will
  22. return the same copy every time. As resources only contain data, there is no need
  23. to duplicate them.
  24. Every object, be it a Node or a Resource, can export properties. There are many
  25. types of Properties, like String, integer, Vector2, etc., and any of these types
  26. can become a resource. This means that both nodes and resources can contain
  27. resources as properties:
  28. .. image:: img/nodes_resources.png
  29. External vs built-in
  30. --------------------
  31. There are two ways to save resources. They can be:
  32. 1. **External** to a scene, saved on the disk as individual files.
  33. 2. **Built-in**, saved inside the \*.tscn or the \*.scn file they're attached to.
  34. To be more specific, here's a :ref:`Texture <class_Texture>`
  35. in a :ref:`Sprite <class_Sprite>` node:
  36. .. image:: img/spriteprop.png
  37. Clicking the resource preview allows us to view and edit the resource's properties.
  38. .. image:: img/resourcerobi.png
  39. The path property tells us where the resource comes from. In this case, it comes
  40. from a PNG image called ``robi.png``. When the resource comes from a file like
  41. this, it is an external resource. If you erase the path or this path is empty,
  42. it becomes a built-in resource.
  43. The switch between built-in and external resources happens when you save the
  44. scene. In the example above, if you erase the path \`"res://robi.png"\` and
  45. save, Godot will save the image inside the .tscn scene file.
  46. .. note::
  47. Even if you save a built-in resource, when you instance a scene multiple
  48. times, the engine will only load one copy of it.
  49. Loading resources from code
  50. ---------------------------
  51. There are two ways to load resources from code. First, you can use the ``load()`` function anytime:
  52. .. tabs::
  53. .. code-tab:: gdscript GDScript
  54. func _ready():
  55. var res = load("res://robi.png") # Godot loads the Resource when it reads the line.
  56. get_node("sprite").texture = res
  57. .. code-tab:: csharp
  58. public override void _Ready()
  59. {
  60. var texture = (Texture)GD.Load("res://robi.png"); // Godot loads the Resource when it reads the line.
  61. var sprite = (Sprite)GetNode("sprite");
  62. sprite.Texture = texture;
  63. }
  64. You can also ``preload`` resources. Unlike ``load``, this function will read the
  65. file from disk and load it at compile-time. As a result, you cannot call preload
  66. with a variable path: you need to use a constant string.
  67. .. tabs::
  68. .. code-tab:: gdscript GDScript
  69. func _ready():
  70. var res = preload("res://robi.png") # Godot loads the resource at compile-time
  71. get_node("sprite").texture = res
  72. .. code-tab:: csharp
  73. // 'preload()' is unavailable in C Sharp.
  74. Loading scenes
  75. --------------
  76. Scenes are also resources, but there is a catch. Scenes saved to disk are
  77. resources of type :ref:`PackedScene <class_PackedScene>`. The
  78. scene is packed inside a resource.
  79. To get an instance of the scene, you have to use the
  80. :ref:`PackedScene.instance() <class_PackedScene_method_instance>` method.
  81. .. tabs::
  82. .. code-tab:: gdscript GDScript
  83. func _on_shoot():
  84. var bullet = preload("res://bullet.tscn").instance()
  85. add_child(bullet)
  86. .. code-tab:: csharp
  87. private PackedScene _bulletScene = (PackedScene)GD.Load("res://bullet.tscn");
  88. public void OnShoot()
  89. {
  90. Node bullet = _bulletScene.Instance();
  91. AddChild(bullet);
  92. }
  93. This method creates the nodes in the scene's hierarchy, configures them, and
  94. returns the root node of the scene. You can then add it as a child of any other
  95. node.
  96. The approach has several advantages. As the :ref:`PackedScene.instance()
  97. <class_PackedScene_method_instance>` function is fast, you can create new
  98. enemies, bullets, effects, etc. without having to load them again from disk each
  99. time. Remember that, as always, images, meshes, etc. are all shared between the
  100. scene instances.
  101. Freeing resources
  102. -----------------
  103. When a ``Resource`` is no longer in use, it will automatically free itself.
  104. Since, in most cases, Resources are contained in Nodes, when you free a node,
  105. the engine frees all the resources it owns as well if no other node uses them.
  106. Creating your own resources
  107. ---------------------------
  108. Like any Object in Godot, users can also script Resources. Resource scripts
  109. inherit the ability to freely translate between object properties and serialized
  110. text or binary data (/*.tres, /*.res). They also inherit the reference-counting
  111. memory management from the Reference type.
  112. This comes with many distinct advantages over alternative data
  113. structures, such as JSON, CSV, or custom TXT files. Users can only import these
  114. assets as a :ref:`Dictionary <class_Dictionary>` (JSON) or as a
  115. :ref:`File <class_File>` to parse. What sets Resources apart is their
  116. inheritance of :ref:`Object <class_Object>`, :ref:`Reference <class_Reference>`,
  117. and :ref:`Resource <class_Resource>` features:
  118. - They can define constants, so constants from other data fields or objects are not needed.
  119. - They can define methods, including setter/getter methods for properties. This allows for abstraction and encapsulation of the underlying data. If the Resource script's structure needs to change, the game using the Resource need not also change.
  120. - They can define signals, so Resources can trigger responses to changes in the data they manage.
  121. - They have defined properties, so users know 100% that their data will exist.
  122. - Resource auto-serialization and deserialization is a built-in Godot Engine feature. Users do not need to implement custom logic to import/export a resource file's data.
  123. - Resources can even serialize sub-Resources recursively, meaning users can design even more sophisticated data structures.
  124. - Users can save Resources as version-control-friendly text files (\*.tres). Upon exporting a game, Godot serializes resource files as binary files (\*.res) for increased speed and compression.
  125. - Godot Engine's Inspector renders and edits Resource files out-of-the-box. As such, users often do not need to implement custom logic to visualize or edit their data. To do so, double-click the resource file in the FileSystem dock or click the folder icon in the Inspector and open the file in the dialog.
  126. - They can extend **other** resource types besides just the base Resource.
  127. .. warning::
  128. Resources and Dictionaries are both passed by reference, but only Resources are
  129. reference-counted. This means that if a Dictionary is passed between objects and
  130. the first object is deleted, all other objects' references to the Dictionary will
  131. be invalidated. Conversely, Resources will not be freed from memory until *all* the
  132. objects are deleted.
  133. .. tabs::
  134. .. code-tab:: gdscript GDScript
  135. extends Node
  136. class MyObject:
  137. extends Object
  138. var dict = {}
  139. func _ready():
  140. var obj1 = MyObject.new()
  141. var obj2 = MyObject.new()
  142. obj1.dict.greeting = "hello"
  143. obj2.dict = obj1.dict # 'obj2.dict' now references 'obj1's Dictionary.
  144. obj1.free() # 'obj1' is freed and the Dictionary too!
  145. print(obj2.dict.greeting) # Error! 'greeting' index accessed on null instance!
  146. # To avoid this, we must manually duplicate the Dictionary.
  147. obj1 = MyObject.new()
  148. obj1.dict.greeting = "hello"
  149. obj2.dict = obj1.dict.duplicate() # Now we are passing a copy, not a reference.
  150. obj1.free() # obj2's Dictionary still exists.
  151. print(obj2.dict.greeting) # Prints 'hello'.
  152. Godot makes it easy to create custom Resources in the Inspector.
  153. 1. Create a plain Resource object in the Inspector. This can even be a type that derives Resource, so long as your script is extending that type.
  154. 2. Set the ``script`` property in the Inspector to be your script.
  155. The Inspector will now display your Resource script's custom properties. If one edits
  156. those values and saves the resource, the Inspector serializes the custom properties
  157. too! To save a resource from the Inspector, click the Inspector's tools menu (top right),
  158. and select "Save" or "Save As...".
  159. If the script's language supports :ref:`script classes <doc_scripting_continued_class_name>`,
  160. then it streamlines the process. Defining a name for your script alone will add it to
  161. the Inspector's creation dialog. This will auto-add your script to the Resource
  162. object you create.
  163. Let's see some examples.
  164. .. tabs::
  165. .. code-tab:: gdscript GDScript
  166. # bot_stats.gd
  167. extends Resource
  168. export(int) var health
  169. export(Resource) var sub_resource
  170. export(Array, String) var strings
  171. func _init(p_health = 0, p_sub_resource = null, p_strings = []):
  172. health = p_health
  173. sub_resource = p_sub_resource
  174. strings = p_strings
  175. # bot.gd
  176. extends KinematicBody
  177. export(Resource) var stats
  178. func _ready():
  179. # Uses an implicit, duck-typed interface for any 'health'-compatible resources.
  180. if stats:
  181. print(stats.health) # Prints '10'.
  182. .. code-tab:: csharp
  183. // BotStats.cs
  184. using System;
  185. using Godot;
  186. namespace ExampleProject {
  187. public class BotStats : Resource
  188. {
  189. [Export]
  190. public int Health { get; set; }
  191. [Export]
  192. public Resource SubResource { get; set; }
  193. [Export]
  194. public String[] Strings { get; set; }
  195. public BotStats(int health = 0, Resource subResource = null, String[] strings = null)
  196. {
  197. Health = health;
  198. SubResource = subResource;
  199. Strings = strings ?? new String[0];
  200. }
  201. }
  202. }
  203. // Bot.cs
  204. using System;
  205. using Godot;
  206. namespace ExampleProject {
  207. public class Bot : KinematicBody
  208. {
  209. [Export]
  210. public Resource Stats;
  211. public override void _Ready()
  212. {
  213. if (Stats != null && Stats is BotStats botStats) {
  214. GD.Print(botStats.Health); // Prints '10'.
  215. }
  216. }
  217. }
  218. }
  219. .. note::
  220. Resource scripts are similar to Unity's ScriptableObjects. The Inspector
  221. provides built-in support for custom resources. If desired though, users
  222. can even design their own Control-based tool scripts and combine them
  223. with an :ref:`EditorPlugin <class_EditorPlugin>` to create custom
  224. visualizations and editors for their data.
  225. Unreal Engine 4's DataTables and CurveTables are also easy to recreate with
  226. Resource scripts. DataTables are a String mapped to a custom struct, similar
  227. to a Dictionary mapping a String to a secondary custom Resource script.
  228. .. tabs::
  229. .. code-tab:: gdscript GDScript
  230. # bot_stats_table.gd
  231. extends Resource
  232. const BotStats = preload("bot_stats.gd")
  233. var data = {
  234. "GodotBot": BotStats.new(10), # Creates instance with 10 health.
  235. "DifferentBot": BotStats.new(20) # A different one with 20 health.
  236. }
  237. func _init():
  238. print(data)
  239. .. code-tab:: csharp
  240. using System;
  241. using Godot;
  242. public class BotStatsTable : Resource
  243. {
  244. private Godot.Dictionary<String, BotStats> _stats = new Godot.Dictionary<String, BotStats>();
  245. public BotStatsTable()
  246. {
  247. _stats["GodotBot"] = new BotStats(10); // Creates instance with 10 health.
  248. _stats["DifferentBot"] = new BotStats(20); // A different one with 20 health.
  249. GD.Print(_stats);
  250. }
  251. }
  252. Instead of just inlining the Dictionary values, one could also, alternatively...
  253. 1. Import a table of values from a spreadsheet and generate these key-value pairs, or...
  254. 2. Design a visualization within the editor and create a simple plugin that adds it
  255. to the Inspector when you open these types of Resources.
  256. CurveTables are the same thing, except mapped to an Array of float values
  257. or a :ref:`Curve <class_Curve>`/:ref:`Curve2D <class_Curve2D>` resource object.
  258. .. warning::
  259. Beware that resource files (\*.tres/\*.res) will store the path of the script
  260. they use in the file. When loaded, they will fetch and load this script as an
  261. extension of their type. This means that trying to assign a subclass, i.e. an
  262. inner class of a script (such as using the ``class`` keyword in GDScript) won't
  263. work. Godot will not serialize the custom properties on the script subclass properly.
  264. In the example below, Godot would load the ``Node`` script, see that it doesn't
  265. extend ``Resource``, and then determine that the script failed to load for the
  266. Resource object since the types are incompatible.
  267. .. tabs::
  268. .. code-tab:: gdscript GDScript
  269. extends Node
  270. class MyResource:
  271. extends Resource
  272. export var value = 5
  273. func _ready():
  274. var my_res = MyResource.new()
  275. # This will NOT serialize the 'value' property.
  276. ResourceSaver.save("res://my_res.tres", my_res)
  277. .. code-tab:: csharp
  278. using System;
  279. using Godot;
  280. public class MyNode : Node
  281. {
  282. public class MyResource : Resource
  283. {
  284. [Export]
  285. public int Value { get; set; } = 5;
  286. }
  287. public override void _Ready()
  288. {
  289. var res = new MyResource();
  290. // This will NOT serialize the 'Value' property.
  291. ResourceSaver.Save("res://MyRes.tres", res);
  292. }
  293. }