node_alternatives.rst 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. .. _doc_node_alternatives:
  2. When and how to avoid using nodes for everything
  3. ================================================
  4. Nodes are cheap to produce, but even they have their limits. A project may
  5. have tens of thousands of nodes all doing things. The more complex their
  6. behavior though, the larger the strain each one adds to a project's
  7. performance.
  8. Godot provides more lightweight objects for creating APIs which nodes use.
  9. Be sure to keep these in mind as options when designing how you wish to build
  10. your project's features.
  11. 1. :ref:`Object <class_Object>`: The ultimate lightweight object, the original
  12. Object must use manual memory management. With that said, it isn't too
  13. difficult to create one's own custom data structures, even node structures,
  14. that are also lighter than the :ref:`Node <class_Node>` class.
  15. - **Example:** See the :ref:`Tree <class_Tree>` node. It supports a high level
  16. of customization for a table of content with an arbitrary number of
  17. rows and columns. The data that it uses to generate its visualization
  18. though is actually a tree of :ref:`TreeItem <class_TreeItem>` Objects.
  19. - **Advantages:** Simplifying one's API to smaller scoped objects helps improve
  20. its accessibility and improve iteration time. Rather than working with the
  21. entire Node library, one creates an abbreviated set of Objects from which
  22. a node can generate and manage the appropriate sub-nodes.
  23. .. note::
  24. One should be careful when handling them. One can store an Object
  25. into a variable, but these references can become invalid without warning.
  26. For example, if the object's creator decides to delete it out of nowhere,
  27. this would trigger an error state when one next accesses it.
  28. 2. :ref:`RefCounted <class_RefCounted>`: Only a little more complex than Object.
  29. They track references to themselves, only deleting loaded memory when no
  30. further references to themselves exist. These are useful in the majority of
  31. cases where one needs data in a custom class.
  32. - **Example:** See the :ref:`FileAccess <class_FileAccess>` object. It functions
  33. just like a regular Object except that one need not delete it themselves.
  34. - **Advantages:** same as the Object.
  35. 3. :ref:`Resource <class_Resource>`: Only slightly more complex than RefCounted.
  36. They have the innate ability to serialize/deserialize (i.e. save and load)
  37. their object properties to/from Godot resource files.
  38. - **Example:** Scripts, PackedScene (for scene files), and other types like
  39. each of the :ref:`AudioEffect <class_AudioEffect>` classes. Each of these
  40. can be saved and loaded, therefore they extend from Resource.
  41. - **Advantages:** Much has
  42. :ref:`already been said <doc_resources>`
  43. on :ref:`Resource <class_Resource>`'s advantages over traditional data
  44. storage methods. In the context of using Resources over Nodes though,
  45. their main advantage is in Inspector-compatibility. While nearly as
  46. lightweight as Object/RefCounted, they can still display and export
  47. properties in the Inspector. This allows them to fulfill a purpose much
  48. like sub-Nodes on the usability front, but also improve performance if
  49. one plans to have many such Resources/Nodes in their scenes.