instancing.rst 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. .. _doc_instancing:
  2. Instancing
  3. ==========
  4. Introduction
  5. ------------
  6. Creating a single scene and adding nodes into it might work for small
  7. projects, but as a project grows in size and complexity, the number of nodes
  8. can quickly become unmanageable. To address this, Godot allows a project
  9. to be separated into any number of scenes. This provides you with a powerful
  10. tool that helps you organize the different components of your game.
  11. In :ref:`doc_scenes_and_nodes` you learned that a scene is a collection of
  12. nodes organized in a tree structure, with a single node as the tree root.
  13. .. image:: img/tree.png
  14. You can create as many scenes as you like and save them to disk. Scenes
  15. saved in this manner are called "Packed Scenes" and have a ``.tscn`` filename
  16. extension.
  17. .. image:: img/instancingpre.png
  18. Once a scene has been saved, it can be instanced into another scene as
  19. if it were any other node.
  20. .. image:: img/instancing.png
  21. In the above picture, Scene B was added to Scene A as an instance.
  22. Instancing By Example
  23. ---------------------
  24. To learn how instancing works, let's start by downloading a sample
  25. project: :download:`instancing.zip <files/instancing.zip>`.
  26. Unzip this project anywhere you like. Then open Godot and add this project to
  27. the project manager using the 'Import' button:
  28. .. image:: img/instancing_import.png
  29. Browse to the folder you extracted and open the "project.godot" file you
  30. can find inside it. After doing this, the new project will appear on the list
  31. of projects. Edit the project by pressing the 'Edit' button.
  32. This project contains two scenes: "Ball.tscn" and "Main.tscn". The ball
  33. scene uses a :ref:`RigidBody2D <class_RigidBody2D>` to provide physics
  34. behavior while the main scene has a set of obstacles for the ball to
  35. collide with (using :ref:`StaticBody2D <class_StaticBody2D>`).
  36. .. image:: img/instancing_ballscene.png
  37. .. image:: img/instancing_mainscene.png
  38. Open the ``Main`` scene, and then select the root node:
  39. .. image:: img/instancing_mainroot.png
  40. We want to add an instance of the ``Ball`` scene as a child of ``Main``.
  41. Click the "link"-shaped button (its hover-text says "Instance a scene file
  42. as a Node.") and select the ``Ball.tscn`` file.
  43. .. image:: img/instancing_linkbutton.png
  44. The ball will be placed at the top-left corner of the screen area (this is
  45. ``(0, 0)`` in screen coordinates). Click and drag the ball somewhere near
  46. the top-center of the scene:
  47. .. image:: img/instancing_placeball.png
  48. Press "Play" and watch the ball fall to the bottom of the screen:
  49. .. image:: img/instancing_playbutton.png
  50. Multiple Instances
  51. ------------------
  52. You can add as many instances as you like to a scene, either by using the
  53. "Instance" button again, or by clicking on the ball instance and pressing
  54. "Duplicate" (Ctrl-D):
  55. .. image:: img/instancing_multiball.png
  56. Run the scene again and all of the balls will fall.
  57. .. image:: img/instancing_multiball.gif
  58. Editing instances
  59. -----------------
  60. Open the ``Ball`` scene and change the ``Bounce`` property in the Inspector
  61. to `1`. Press "Play" and notice that all of the instanced balls are now
  62. much more bouncy. Because the instanced balls are based on the saved scene,
  63. changes to that scene will affect all instances.
  64. You can also adjust individual instances. Set the bounce value back to ``0.5``
  65. and then in the ``Main`` scene, select one of the instanced balls. Set its
  66. ``Bounce`` to ``1`` and press "Play".
  67. .. image:: img/instancing_property.png
  68. Notice that a grey "revert" button appears next to the adjusted property. When
  69. this button is present, it means you modified a property in the
  70. instanced scene to override its value in the saved scene. Even
  71. if that property is modified in the original scene, the custom value
  72. will remain. Pressing the revert button will restore the property to the
  73. value in the saved scene.
  74. Conclusion
  75. ----------
  76. Instancing can be useful when you want to create many copies of the
  77. same object.