instancing.rst 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 add a ``PhysicsMaterial`` by clicking on the down
  61. arrow and selecting "New PhysicsMaterial".
  62. .. image:: img/instancing_physicsmat1.png
  63. Then, expand the material by clicking on it, and set the ``Bounce`` property
  64. to ``1``.
  65. .. image:: img/instancing_physicsmat2.png
  66. Press "Play" and notice that all of the instanced balls are now
  67. much more bouncy. Because the instanced balls are based on the saved scene,
  68. changes to that scene will affect all instances.
  69. You can also adjust individual instances. Set the bounce value back to ``0``
  70. and then in the ``Main`` scene, select one of the instanced balls. Resources
  71. like ``PhysicsMaterial`` are shared between instances by default, so we need
  72. to make it unique. Click on the down arrow and select "Make Unique". Set its
  73. ``Bounce`` to ``1`` and press "Play".
  74. .. image:: img/instancing_property.png
  75. Notice that a grey "revert" button appears next to the adjusted property. When
  76. this button is present, it means you modified a property in the
  77. instanced scene to override its value in the saved scene. Even
  78. if that property is modified in the original scene, the custom value
  79. will remain. Pressing the revert button will restore the property to the
  80. value in the saved scene.
  81. Conclusion
  82. ----------
  83. Instancing can be useful when you want to create many copies of the
  84. same object. It is also possible to create instances in code by using
  85. GDScript, see :ref:`doc_instancing_continued`.