instancing.rst 9.2 KB

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