instancing.rst 9.5 KB

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