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