scenes_versus_scripts.rst 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. Scripts can be registered 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. This way, 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. With a registered script, the scripted type instead becomes a creation option
  59. like the other nodes and resources in the system.
  60. The creation dialog even has a search bar to look up the type by
  61. name.
  62. There are two systems for registering types:
  63. - :ref:`Custom Types <doc_making_plugins>`
  64. - Editor-only. Typenames are not accessible at runtime.
  65. - Does not support inherited custom types.
  66. - An initializer tool. Creates the node with the script. Nothing more.
  67. - Editor has no type-awareness of the script or its relationship
  68. to other engine types or scripts.
  69. - Allows users to define an icon.
  70. - Works for all scripting languages because it deals with Script resources in abstract.
  71. - Set up using :ref:`EditorPlugin.add_custom_type <class_EditorPlugin_method_add_custom_type>`.
  72. - :ref:`Script Classes <doc_gdscript_basics_class_name>`
  73. - Editor and runtime accessible.
  74. - Displays inheritance relationships in full.
  75. - Creates the node with the script, but can also change types
  76. or extend the type from the editor.
  77. - Editor is aware of inheritance relationships between scripts,
  78. script classes, and engine C++ classes.
  79. - Allows users to define an icon.
  80. - Engine developers must add support for languages manually (both name exposure and
  81. runtime accessibility).
  82. - Godot 3.1+ only.
  83. - The Editor scans project folders and registers any exposed names for all
  84. scripting languages. Each scripting language must implement its own
  85. support for exposing this information.
  86. Both methodologies add names to the creation dialog, but script classes, in
  87. particular, also allow for users to access the typename without loading the
  88. script resource. Creating instances and accessing constants or static methods
  89. is viable from anywhere.
  90. With features like these, one may wish their type to be a script without a
  91. scene due to the ease of use it grants users. Those developing plugins or
  92. creating in-house tools for designers to use will find an easier time of things
  93. this way.
  94. On the downside, it also means having to use largely imperative programming.
  95. Performance of Script vs PackedScene
  96. ------------------------------------
  97. One last aspect to consider when choosing scenes and scripts is execution speed.
  98. As the size of objects increases, the scripts' necessary size to create and
  99. initialize them grows much larger. Creating node hierarchies demonstrates this.
  100. Each Node's logic could be several hundred lines of code in length.
  101. The code example below creates a new ``Node``, changes its name, assigns a
  102. script to it, sets its future parent as its owner so it gets saved to disk along
  103. with it, and finally adds it as a child of the ``Main`` node:
  104. .. tabs::
  105. .. code-tab:: gdscript GDScript
  106. # Main.gd
  107. extends Node
  108. func _init():
  109. var child = Node.new()
  110. child.name = "Child"
  111. child.script = preload("Child.gd")
  112. child.owner = self
  113. add_child(child)
  114. .. code-tab:: csharp
  115. using System;
  116. using Godot;
  117. public class Main : Resource
  118. {
  119. public Node Child { get; set; }
  120. public Main()
  121. {
  122. Child = new Node();
  123. Child.Name = "Child";
  124. Child.Script = ResourceLoader.Load<Script>("child.gd");
  125. Child.Owner = this;
  126. AddChild(Child);
  127. }
  128. }
  129. Script code like this is much slower than engine-side C++ code. Each instruction
  130. makes a call to the scripting API which leads to many "lookups" on the back-end
  131. to find the logic to execute.
  132. Scenes help to avoid this performance issue. :ref:`PackedScene
  133. <class_PackedScene>`, the base type that scenes inherit from, defines resources
  134. that use serialized data to create objects. The engine can process scenes in
  135. batches on the back-end and provide much better performance than scripts.
  136. Conclusion
  137. ----------
  138. In the end, the best approach is to consider the following:
  139. - If one wishes to create a basic tool that is going to be re-used in several
  140. different projects and which people of all skill levels will likely use
  141. (including those who don't label themselves as "programmers"), then chances
  142. are that it should probably be a script, likely one with a custom name/icon.
  143. - If one wishes to create a concept that is particular to their game, then it
  144. should always be a scene. Scenes are easier to track/edit and provide more
  145. security than scripts.
  146. - If one would like to give a name to a scene, then they can still sort of do
  147. this in 3.1 by declaring a script class and giving it a scene as a constant.
  148. The script becomes, in effect, a namespace:
  149. .. tabs::
  150. .. code-tab:: gdscript GDScript
  151. # game.gd
  152. class_name Game # extends Reference, so it won't show up in the node creation dialog
  153. extends Reference
  154. const MyScene = preload("my_scene.tscn")
  155. # main.gd
  156. extends Node
  157. func _ready():
  158. add_child(Game.MyScene.instance())