instancing.rst 9.1 KB

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