node_alternatives.rst 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 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:: One should be careful when handling them. One can store an Object
  24. into a variable, but these references can become invalid without warning.
  25. For example, if the object's creator decides to delete it out of nowhere,
  26. this would trigger an error state when one next accesses it.
  27. 2. :ref:`Reference <class_Reference>`: Only a little more complex than Object.
  28. They track references to themselves, only deleting loaded memory when no
  29. further references to themselves exist. These are useful in the majority of
  30. cases where one needs data in a custom class.
  31. - **Example:** See the :ref:`File <class_File>` object. It functions
  32. just like a regular Object except that one need not delete it themselves.
  33. - **Advantages:** same as the Object.
  34. 3. :ref:`Resource <class_Resource>`: Only slightly more complex than Reference.
  35. They have the innate ability to serialize/deserialize (i.e. save and load)
  36. their object properties to/from Godot resource files.
  37. - **Example:** Scripts, PackedScene (for scene files), and other types like
  38. each of the :ref:`AudioEffect <class_AudioEffect>` classes. Each of these
  39. can be save and loaded, therefore they extend from Resource.
  40. - **Advantages:** Much has
  41. :ref:`already been said <doc_resources>`
  42. on :ref:`Resource <class_Resource>`'s advantages over traditional data
  43. storage methods. In the context of using Resources over Nodes though,
  44. their main advantage is in Inspector-compatibility. While nearly as
  45. lightweight as Object/Reference, they can still display and export
  46. properties in the Inspector. This allows them to fulfill a purpose much
  47. like sub-Nodes on the usability front, but also improve performance if
  48. one plans to have many such Resources/Nodes in their scenes.