what_are_godot_classes.rst 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. .. _doc_what_are_godot_classes:
  2. Godot scenes and scripts are classes
  3. ====================================
  4. In Godot, scripts and scenes can both be the equivalent of classes in an
  5. Object-Oriented programming language. The main difference is that scenes are
  6. `declarative code <https://en.wikipedia.org/wiki/Declarative_programming>`_,
  7. while scripts can contain `imperative code
  8. <https://en.wikipedia.org/wiki/Imperative_programming>`_.
  9. As a result, many best practices in Godot boil down to applying Object-Oriented
  10. design principles to the scenes, nodes, or script that make up your game.
  11. This guide explains how scripts and scenes work in the engine's core, to help
  12. you get a sense of how Godot works under the hood, and to help you better
  13. understand where some of this series' best practices come from.
  14. Making sense of classes in Godot
  15. --------------------------------
  16. Godot Engine provides built-in classes like :ref:`Node <class_Node>`.
  17. User-created types are not technically classes. Instead, they are resources that
  18. tell the engine a sequence of initializations to perform on one of the engine's
  19. built-in classes.
  20. Godot's internal classes have methods that register a class's data with a
  21. :ref:`ClassDB <class_ClassDB>`. This database provides runtime access to class
  22. information. ``ClassDB`` contains information about classes like:
  23. - properties
  24. - methods
  25. - constants
  26. - signals
  27. This ``ClassDB`` is what Objects check against when performing an operation like
  28. accessing a property or calling a method. ``ClassDB`` checks the database's
  29. records and the records of the Object's base types to see if the Object supports
  30. the operation.
  31. On the engine's side, every class defines a static ``_bind_methods()`` function
  32. that describes what C++ content it registers to the database and how. When you
  33. use the engine, you can extend the methods, properties, and signals available from
  34. the ``ClassDB`` by attaching a :ref:`Script <class_Script>` to your node.
  35. Objects check their attached script before the database. This is why scripts can
  36. override built-in methods. If a script defines a ``_get_property_list()`` method,
  37. Godot appends that data to the list of properties the Object fetches from the
  38. ClassDB. The same is true for other declarative code.
  39. Even scripts that don't inherit from a built-in type, i.e. scripts that don't
  40. start with the ``extends`` keyword, implicitly inherit from the engine's base
  41. :ref:`Reference <class_Reference>` class. This allows the Object to defer
  42. to the script's content where the engine logic deems appropriate.
  43. .. note::
  44. As a result, you can instance scripts without the ``extends`` keyword
  45. from code, but you cannot attach them to a :ref:`Node <class_Node>`
  46. Scripting performances and PackedScene
  47. --------------------------------------
  48. As the size of Objects increases, the scripts' necessary size to create them
  49. grows much, much larger. Creating node hierarchies demonstrates this. Each
  50. individual Node's logic could be several hundred lines of code in length.
  51. Let's see a simple example of creating a single ``Node`` as a child. The code
  52. below creates a new ``Node``, changes its name, assigns a script to it, sets its
  53. future parent as its owner so it gets saved to disk along with it, and finally
  54. adds it as a child of the ``Main`` node:
  55. .. tabs::
  56. .. code-tab:: gdscript GDScript
  57. # Main.gd
  58. extends Node
  59. func _init():
  60. var child = Node.new()
  61. child.name = "Child"
  62. child.script = preload("Child.gd")
  63. child.owner = self
  64. add_child(child)
  65. .. code-tab:: csharp
  66. using System;
  67. using Godot;
  68. namespace ExampleProject
  69. {
  70. public class Main : Resource
  71. {
  72. public Node Child { get; set; }
  73. public Main()
  74. {
  75. Child = new Node();
  76. Child.Name = "Child";
  77. Child.Script = (Script)ResourceLoader.Load("child.gd");
  78. Child.Owner = this;
  79. AddChild(Child);
  80. }
  81. }
  82. }
  83. Script code like this is much slower than engine-side C++ code. Each change
  84. makes a separate call to the scripting API which leads to many "look-ups" on the
  85. back-end to find the logic to execute.
  86. Scenes help to avoid this performance issue. :ref:`PackedScene
  87. <class_PackedScene>`, the base type that scenes inherit from, are resources that
  88. use serialized data to create objects. The engine can process scenes in batches
  89. on the back-end and provide much better performance than scripts.
  90. Scenes and scripts are objects
  91. ------------------------------
  92. Why is any of this important to scene organization? Because scenes *are*
  93. objects. One often pairs a scene with a scripted root node that makes use of the
  94. sub-nodes. This means that the scene is often an extension of the script's
  95. declarative code.
  96. The content of a scene helps to define:
  97. - What nodes are available to the script
  98. - How they are organized
  99. - How are they initialized
  100. - What signal connections they have with each other
  101. Many Object-Oriented principles which apply to written code *also* apply to
  102. scenes.
  103. The scene is *always an extension of the script attached to its root node*. You
  104. can see all the nodes it contains as part of a single class.
  105. Most of the tips and techniques explained in this series will build on this.