what_are_godot_classes.rst 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. .. _doc_what_are_godot_classes:
  2. Applying object-oriented principles in Godot
  3. ============================================
  4. The engine offers two main ways to create reusable objects: scripts and scenes. Neither of these
  5. technically define classes under the hood.
  6. Still, many best practices using Godot involve applying object-oriented programming principles to
  7. the scripts and scenes that compose your game. That is why it's useful to understand how we can
  8. think of them as classes.
  9. This guide briefly explains how scripts and scenes work in the engine's core to help you understand
  10. how they work under the hood.
  11. How scripts work in the engine
  12. ------------------------------
  13. The engine provides built-in classes like :ref:`Node <class_Node>`. You can extend those to create
  14. derived types using a script.
  15. These scripts are not technically classes. Instead, they are resources that tell the engine a
  16. sequence of initializations to perform on one of the engine's built-in classes.
  17. Godot's internal classes have methods that register a class's data with a :ref:`ClassDB
  18. <class_ClassDB>`. This database provides runtime access to class information. ``ClassDB`` contains
  19. information about classes like:
  20. - Properties.
  21. - Methods.
  22. - Constants.
  23. - Signals.
  24. This ``ClassDB`` is what objects check against when performing an operation like accessing a
  25. property or calling a method. It checks the database's records and the object's base types' records
  26. to see if the object supports the operation.
  27. Attaching a :ref:`Script <class_Script>` to your object extends the methods, properties, and signals
  28. available from the ``ClassDB``.
  29. .. note::
  30. Even scripts that don't use the ``extends`` keyword implicitly inherit from the engine's base
  31. :ref:`RefCounted <class_RefCounted>` class. As a result, you can instantiate scripts without the
  32. ``extends`` keyword from code. Since they extend ``RefCounted`` though, you cannot attach them to
  33. a :ref:`Node <class_Node>`.
  34. Scenes
  35. ------
  36. The behavior of scenes has many similarities to classes, so it can make sense to think of a scene as
  37. a class. Scenes are reusable, instantiable, and inheritable groups of nodes. Creating a scene is
  38. similar to having a script that creates nodes and adds them as children using ``add_child()``.
  39. We often pair a scene with a scripted root node that makes use of the scene's nodes. As such,
  40. the script extends the scene by adding behavior through imperative code.
  41. The content of a scene helps to define:
  42. - What nodes are available to the script.
  43. - How they are organized.
  44. - How they are initialized.
  45. - What signal connections they have with each other.
  46. Why is any of this important to scene organization? Because instances of scenes *are* objects. As a
  47. result, many object-oriented principles that apply to written code also apply to scenes: single
  48. responsibility, encapsulation, and others.
  49. The scene is *always an extension of the script attached to its root node*, so you can interpret it
  50. as part of a class.
  51. Most of the techniques explained in this best practices series build on this point.