instancing.rst 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. .. _doc_instancing:
  2. Instancing
  3. ==========
  4. Rationale
  5. ---------
  6. Having a scene and throwing nodes into it might work for small projects, but as
  7. a project grows, you will naturally add more and more nodes and it can quickly
  8. become unmanageable. To solve this, Godot allows a project to be separated into
  9. several scenes. This, however, does not work the same way as in other game
  10. engines. In fact, it's quite different, so please do not skip this tutorial!
  11. To recap: A scene is a collection of nodes organized as a tree, where
  12. they can have only one single node as the tree root.
  13. .. image:: img/tree.png
  14. Recall that a scene can be created and saved to disk. You can create and save
  15. as many scenes as you desire.
  16. .. image:: img/instancingpre.png
  17. Afterwards, while editing any scene, you can instance other scenes as part of
  18. it:
  19. .. image:: img/instancing.png
  20. In the above picture, Scene B was added to Scene A as an instance. It may seem
  21. weird at first, but by the end of this tutorial it should make complete sense.
  22. Instancing, step by step
  23. ------------------------
  24. To learn how to do instancing, let's start with 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' option:
  28. .. image:: img/importproject.png
  29. Simply 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 "container.tscn". The ball
  33. scene is just a ball with physics, while the container scene has a nicely
  34. shaped collision, so balls can be dropped in there.
  35. .. image:: img/ballscene.png
  36. .. image:: img/contscene.png
  37. Open the container scene, and then select the root node:
  38. .. image:: img/controot.png
  39. Afterwards, push the link shaped button. This is the instancing button!
  40. .. image:: img/continst.png
  41. Select the ball scene (ball.tscn). The ball should appear at the origin (0,0)
  42. which is at the top-left of the container scene. Drag the ball to the center of
  43. the scene, like this:
  44. .. image:: img/continstanced.png
  45. Press Play and Voila!
  46. .. image:: img/playinst.png
  47. The instanced ball should fall somewhere to the bottom of the pit before coming
  48. to rest.
  49. A little more
  50. -------------
  51. You can create as many instances as you desire within a scene. Just try instancing
  52. more balls or duplicating them (via Ctrl-D or the duplicate button):
  53. .. image:: img/instmany.png
  54. Then try running the scene again:
  55. .. image:: img/instmanyrun.png
  56. Cool, huh? This is how instancing works.
  57. Editing instances
  58. -----------------
  59. Select one of the many copies of the balls and go to the property
  60. editor. Let's make it bounce a lot more, so look for the Bounce
  61. parameter and set it to 1.0:
  62. .. image:: img/instedit.png
  63. Grey "revert" button will appear. When
  64. this button is present, it means we modified a property in the
  65. instanced scene to override a specific value in this instance. Even
  66. if that property is modified in the original scene, the custom value
  67. will always overwrite it. Pressing the revert button will restore the
  68. property to the original value that came from the scene.
  69. Conclusion
  70. ----------
  71. Instancing seems handy, but there is more to it than meets the eye!
  72. The next part of the instancing tutorial should cover the rest..