instancing.rst 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. .. _doc_instancing:
  2. Creating instances
  3. ==================
  4. .. note::
  5. This tutorial refers to instancing scenes in the editor. To learn how
  6. to instance scenes from code, see :ref:`doc_nodes_and_scene_instances`.
  7. Godot's approach to *instancing* described below should not be confused with
  8. hardware instancing that can be used to render large amounts of similar
  9. objects quickly. See :ref:`doc_using_multimesh` instead.
  10. In the previous part, we saw that a scene is a collection of nodes organized in
  11. a tree structure, with a single node as its root. You can split your project
  12. into any number of scenes. This feature helps you break down and organize your
  13. game's different components.
  14. You can create as many scenes as you'd like and save them as files with the
  15. ``.tscn`` extension, which stands for "text scene". The ``label.tscn`` file from
  16. the previous lesson was an example. We call those files "Packed Scenes" as they
  17. pack information about your scene's content.
  18. Here's an example of a ball. It's composed of a :ref:`RigidBody2D
  19. <class_RigidBody2D>` node as its root named Ball, which allows the ball to fall
  20. and bounce on walls, a :ref:`Sprite2D <class_Sprite2D>` node, and a
  21. :ref:`CollisionShape2D <class_CollisionShape2D>`.
  22. .. image:: img/instancing_ball_scene.webp
  23. Once you have saved a scene, it works as a blueprint: you can reproduce it in other
  24. scenes as many times as you'd like. Replicating an object from a template like
  25. this is called **instancing**.
  26. .. image:: img/instancing_ball_instances_example.webp
  27. As we mentioned in the previous part, instanced scenes behave like a node: the
  28. editor hides their content by default. When you instance the Ball, you only see
  29. the Ball node. Notice also how each duplicate has a unique name.
  30. Every instance of the Ball scene starts with the same structure and properties
  31. as ``ball.tscn``. However, you can modify each independently, such as changing
  32. how they bounce, how heavy they are, or any property exposed by the source
  33. scene.
  34. In practice
  35. -----------
  36. Let's use instancing in practice to see how it works in Godot. We invite
  37. you to download the ball's sample project we prepared for you:
  38. `instancing_starter.zip <https://github.com/godotengine/godot-docs-project-starters/releases/download/latest-4.x/instancing_starter.zip>`_.
  39. Extract the archive on your computer. To import it, you need the Project Manager.
  40. The Project Manager is accessed by opening Godot, or if you already have Godot
  41. opened, click on :menu:`Project > Quit to Project List` (:kbd:`Ctrl + Shift + Q`, :kbd:`Ctrl + Option + Cmd + Q` on macOS)
  42. In the Project Manager, click the :button:`Import` button to import the project.
  43. .. image:: img/instancing_import_button.webp
  44. In the pop-up that appears navigate to the folder you extracted.
  45. Double-click the ``project.godot`` file to open it.
  46. .. image:: img/instancing_import_project_file.webp
  47. Finally, click the :button:`Import` button.
  48. .. image:: img/instancing_import_and_edit_button.webp
  49. A window notifying you that the project was last opened in an older Godot version
  50. may appear, that's not an issue. Click :button:`Ok` to open the project.
  51. The project contains two packed scenes: ``main.tscn``, containing walls against
  52. which the ball collides, and ``ball.tscn``. The Main scene should open
  53. automatically. If you're seeing an empty 3D scene instead of the main scene, click the 2D button at the top of the screen.
  54. .. image:: img/instancing_2d_scene_select.webp
  55. .. image:: img/instancing_main_scene.webp
  56. Let's add a ball as a child of the Main node. In the Scene dock, select the Main
  57. node. Then, click the link icon at the top of the scene dock. This button allows
  58. you to add an instance of a scene as a child of the currently selected node.
  59. .. image:: img/instancing_scene_link_button.webp
  60. Double-click the ball scene to instance it.
  61. .. image:: img/instancing_instance_child_window.webp
  62. The ball appears in the top-left corner of the viewport.
  63. .. image:: img/instancing_ball_instanced.webp
  64. Click on it and drag it towards the center of the view.
  65. .. image:: img/instancing_ball_moved.webp
  66. Play the game by pressing :kbd:`F5` (:kbd:`Cmd + B` on macOS). You should see it fall.
  67. Now, we want to create more instances of the Ball node. With the ball still
  68. selected, press :kbd:`Ctrl + D` (:kbd:`Cmd + D` on macOS) to call the duplicate
  69. command. Click and drag to move the new ball to a different location.
  70. .. image:: img/instancing_ball_duplicated.webp
  71. You can repeat this process until you have several in the scene.
  72. .. image:: img/instancing_main_scene_with_balls.webp
  73. Play the game again. You should now see every ball fall independently from one
  74. another. This is what instances do. Each is an independent reproduction of a
  75. template scene.
  76. Editing scenes and instances
  77. ----------------------------
  78. There is more to instances. With this feature, you can:
  79. 1. Change the properties of one ball without affecting the others using the
  80. :ui:`Inspector`.
  81. 2. Change the default properties of every Ball by opening the ``ball.tscn`` scene
  82. and making a change to the Ball node there. Upon saving, all instances of the
  83. Ball in the project will see their values update.
  84. .. note:: Changing a property on an instance always overrides values from the
  85. corresponding packed scene.
  86. Let's try this. Double-click ``ball.tscn`` in the FileSystem to open it.
  87. .. image:: img/instancing_ball_scene_open.webp
  88. In the Scene dock on the left, select the Ball node. Then, in the :ui:`Inspector` on the
  89. right, click on the :inspector:`PhysicsMaterial` property to expand it.
  90. .. image:: img/instancing_physics_material_expand.webp
  91. Set its Bounce property to ``0.5`` by clicking on the number field, typing ``0.5``,
  92. and pressing :kbd:`Enter`.
  93. .. image:: img/instancing_property_bounce_updated.webp
  94. Play the game by pressing :kbd:`F5` (:kbd:`Cmd + B` on macOS) and notice how all balls now bounce a lot
  95. more. As the Ball scene is a template for all instances, modifying it and saving
  96. causes all instances to update accordingly.
  97. Let's now adjust an individual instance. Head back to the Main scene by clicking
  98. on the corresponding tab above the viewport.
  99. .. image:: img/instancing_scene_tabs.webp
  100. Select one of the instanced Ball nodes and, in the :ui:`Inspector`, set its
  101. :inspector:`Gravity Scale` value to ``10``.
  102. .. image:: img/instancing_property_gravity_scale.png
  103. A grey "revert" button appears next to the adjusted property.
  104. .. image:: img/instancing_property_revert_icon.png
  105. This icon indicates you are overriding a value from the source packed scene.
  106. Even if you modify the property in the original scene, the value override will
  107. be preserved in the instance. Clicking the revert icon will restore the
  108. property to the value in the saved scene.
  109. Rerun the game and notice how this ball now falls much faster than the others.
  110. .. note::
  111. You may notice you are unable to change the values of the :inspector:`PhysicsMaterial`
  112. of the ball. This is because :inspector:`PhysicsMaterial` is a *resource*, and needs
  113. to be made unique before you can edit it in a scene that is linking to its
  114. original scene. To make a resource unique for one instance, right-click on
  115. the :inspector:`Physics Material` property in the :ui:`Inspector` and click :button:`Make Unique`
  116. in the context menu.
  117. Resources are another essential building block of Godot games we will cover
  118. in a later lesson.
  119. Scene instances as a design language
  120. ------------------------------------
  121. Instances and scenes in Godot offer an excellent design language, setting the
  122. engine apart from others out there. We designed Godot around this concept from
  123. the ground up.
  124. We recommend dismissing architectural code patterns when making games with
  125. Godot, such as Model-View-Controller (MVC) or Entity-Relationship diagrams.
  126. Instead, you can start by imagining the elements players will see in your game
  127. and structure your code around them.
  128. For example, you could break down a shooter game like so:
  129. .. image:: img/instancing_diagram_shooter.png
  130. You can come up with a diagram like this for almost any type of game. Each
  131. rectangle represents an entity that's visible in the game from the player's
  132. perspective. The arrows point towards the instantiator of each scene.
  133. Once you have a diagram, we recommend creating a scene for each element listed
  134. in it to develop your game. You'll use instancing, either by code or directly in
  135. the editor, to build your tree of scenes.
  136. Programmers tend to spend a lot of time designing abstract architectures and
  137. trying to fit components into it. Designing based on scenes makes development
  138. faster and more straightforward, allowing you to focus on the game logic itself.
  139. Because most game components map directly to a scene, using a design based on
  140. scene instantiation means you need little other architectural code.
  141. Here's the example of a scene diagram for an open-world game with tons of assets
  142. and nested elements:
  143. .. image:: img/instancing_diagram_open_world.png
  144. Imagine we started by creating the room. We could make a couple of different
  145. room scenes, with unique arrangements of furniture in them. Later, we could make
  146. a house scene that uses multiple room instances for the interior. We would
  147. create a citadel out of many instanced houses and a large terrain on which we
  148. would place the citadel. Each of these would be a scene instancing one or more sub-scenes.
  149. Later, we could create scenes representing guards and add them to the citadel.
  150. They would be indirectly added to the overall game world.
  151. With Godot, it's easy to iterate on your game like this, as all you need to do
  152. is create and instantiate more scenes. We designed the editor to be accessible
  153. to programmers, designers, and artists alike. A typical team development process
  154. can involve 2D or 3D artists, level designers, game designers, and animators,
  155. all working with the Godot editor.
  156. Summary
  157. -------
  158. Instancing, the process of producing an object from a blueprint, has many handy
  159. uses. With scenes, it gives you:
  160. - The ability to divide your game into reusable components.
  161. - A tool to structure and encapsulate complex systems.
  162. - A language to think about your game project's structure in a natural way.