scenes_versus_scripts.rst 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. .. _doc_scenes_versus_scripts:
  2. When to use scenes versus scripts
  3. =================================
  4. We've already covered how scenes and scripts are different. Scripts
  5. define an engine class extension with imperative code, scenes with
  6. declarative code.
  7. Each system's capabilities are different as a result.
  8. Scenes can define how an extended class initializes, but not what its
  9. behavior actually is. Scenes are often used in conjunction with a script so
  10. that the scene acts as an extension of the scripts declarative code.
  11. Anonymous types
  12. ---------------
  13. It *is* possible to completely define a scenes' contents using a script alone.
  14. This is, in essence, what the Godot Editor does, only in the C++ constructor
  15. of its objects.
  16. But, choosing which one to use can be a dilemma. Creating script instances
  17. is identical to creating in-engine classes whereas handling scenes requires
  18. a change in API:
  19. .. tabs::
  20. .. code-tab:: gdscript GDScript
  21. const MyNode = preload("my_node.gd")
  22. const MyScene = preload("my_scene.tscn")
  23. var node = Node.new()
  24. var my_node = MyNode.new() # Same method call
  25. var my_scene = MyScene.instance() # Different method call
  26. var my_inherited_scene = MyScene.instance(PackedScene.GEN_EDIT_STATE_MAIN) # Create scene inheriting from MyScene
  27. .. code-tab:: csharp
  28. using System;
  29. using Godot;
  30. public class Game : Node
  31. {
  32. public readonly Script MyNodeScr = (Script)ResourceLoader.Load("MyNode.cs");
  33. public readonly PackedScene MySceneScn = (PackedScene)ResourceLoader.Load("MyScene.tscn");
  34. public Node ANode;
  35. public Node MyNode;
  36. public Node MyScene;
  37. public Node MyInheritedScene;
  38. public Game()
  39. {
  40. ANode = new Node();
  41. MyNode = new MyNode(); // Same syntax
  42. MyScene = MySceneScn.Instance(); // Different. Instantiated from a PackedScene
  43. MyInheritedScene = MySceneScn.Instance(PackedScene.GenEditState.Main); // Create scene inheriting from MyScene
  44. }
  45. }
  46. Also, scripts will operate a little slower than scenes due to the
  47. speed differences between engine and script code. The larger and more complex
  48. the node, the more reason there is to build it as a scene.
  49. Named types
  50. -----------
  51. In some cases, a user can register a script as a new type within the editor
  52. itself. This displays it as a new type in the node or resource creation dialog
  53. with an optional icon. In these cases, the user's ability to use the script
  54. is much more streamlined. Rather than having to...
  55. 1. Know the base type of the script they would like to use.
  56. 2. Create an instance of that base type.
  57. 3. Add the script to the node.
  58. 1. (Drag-n-drop method)
  59. 1. Find the script in the FileSystem dock.
  60. 2. Drag and drop the script onto the node in the Scene dock.
  61. 2. (Property method)
  62. 1. Scroll down to the bottom of the Inspector to find the ``script`` property and select it.
  63. 2. Select "Load" from the dropdown.
  64. 3. Select the script from the file dialog.
  65. With a registered script, the scripted type instead becomes a creation option
  66. like the other nodes and resources in the system. One need not do any of the
  67. above work. The creation dialog even has a search bar to look up the type by
  68. name.
  69. There are two systems for registering types...
  70. - :ref:`Custom Types <doc_making_plugins>`
  71. - Editor-only. Typenames are not accessible at runtime.
  72. - Does not support inherited custom types.
  73. - An initializer tool. Creates the node with the script. Nothing more.
  74. - Editor has no type-awareness of the script or its relationship
  75. to other engine types or scripts.
  76. - Allows users to define an icon.
  77. - Works for all scripting languages because it deals with Script resources in abstract.
  78. - Set up using :ref:`EditorPlugin.add_custom_type <class_EditorPlugin_method_add_custom_type>`.
  79. - :ref:`Script Classes <doc_scripting_continued_class_name>`
  80. - Editor and runtime accessible.
  81. - Displays inheritance relationships in full.
  82. - Creates the node with the script, but can also change types
  83. or extend the type from the editor.
  84. - Editor is aware of inheritance relationships between scripts,
  85. script classes, and engine C++ classes.
  86. - Allows users to define an icon.
  87. - Engine developers must add support for languages manually (both name exposure and
  88. runtime accessibility).
  89. - Godot 3.1+ only.
  90. - The Editor scans project folders and registers any exposed names for all
  91. scripting languages. Each scripting language must implement its own
  92. support for exposing this information.
  93. Both methodologies add names to the creation dialog, but script classes, in
  94. particular, also allow for users to access the typename without loading the
  95. script resource. Creating instances and accessing constants or static methods
  96. is viable from anywhere.
  97. With features like these, one may wish their type to be a script without a
  98. scene due to the ease of use it grants users. Those developing plugins or
  99. creating in-house tools for designers to use will find an easier time of things
  100. this way.
  101. On the downside, it also means having to use largely imperative programming.
  102. Performance of Script vs PackedScene
  103. ------------------------------------
  104. One last aspect to consider when choosing scenes and scripts is execution speed.
  105. As the size of objects increases, the scripts' necessary size to create and
  106. initialize them grows much larger. Creating node hierarchies demonstrates this.
  107. Each Node's logic could be several hundred lines of code in length.
  108. The code example below creates a new ``Node``, changes its name, assigns a
  109. script to it, sets its future parent as its owner so it gets saved to disk along
  110. with it, and finally adds it as a child of the ``Main`` node:
  111. .. tabs::
  112. .. code-tab:: gdscript GDScript
  113. # Main.gd
  114. extends Node
  115. func _init():
  116. var child = Node.new()
  117. child.name = "Child"
  118. child.script = preload("Child.gd")
  119. child.owner = self
  120. add_child(child)
  121. .. code-tab:: csharp
  122. using System;
  123. using Godot;
  124. public class Main : Resource
  125. {
  126. public Node Child { get; set; }
  127. public Main()
  128. {
  129. Child = new Node();
  130. Child.Name = "Child";
  131. Child.Script = ResourceLoader.Load<Script>("child.gd");
  132. Child.Owner = this;
  133. AddChild(Child);
  134. }
  135. }
  136. Script code like this is much slower than engine-side C++ code. Each instruction
  137. makes a call to the scripting API which leads to many "lookups" on the back-end
  138. to find the logic to execute.
  139. Scenes help to avoid this performance issue. :ref:`PackedScene
  140. <class_PackedScene>`, the base type that scenes inherit from, defines resources
  141. that use serialized data to create objects. The engine can process scenes in
  142. batches on the back-end and provide much better performance than scripts.
  143. Conclusion
  144. ----------
  145. In the end, the best approach is to consider the following:
  146. - If one wishes to create a basic tool that is going to be re-used in several
  147. different projects and which people of all skill levels will likely use
  148. (including those who don't label themselves as "programmers"), then chances
  149. are that it should probably be a script, likely one with a custom name/icon.
  150. - If one wishes to create a concept that is particular to their game, then it
  151. should always be a scene. Scenes are easier to track/edit and provide more
  152. security than scripts.
  153. - If one would like to give a name to a scene, then they can still sort of do
  154. this in 3.1 by declaring a script class and giving it a scene as a constant.
  155. The script becomes, in effect, a namespace:
  156. .. tabs::
  157. .. code-tab:: gdscript GDScript
  158. # game.gd
  159. extends Reference
  160. class_name Game # extends Reference, so it won't show up in the node creation dialog
  161. const MyScene = preload("my_scene.tscn")
  162. # main.gd
  163. extends Node
  164. func _ready():
  165. add_child(Game.MyScene.instance())