handling_compatibility_breakages.rst 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. .. _doc_handling_compatibility_breakages:
  2. Handling compatibility breakages
  3. ================================
  4. .. TODO: Elaborate on types of compatibility and procedure.
  5. So you've added a new parameter to a method, changed the return type,
  6. changed the type of a parameter, or changed its default value,
  7. and now the automated testing is complaining about compatibility breakages?
  8. Breaking compatibility should be avoided, but when necessary there are systems in place
  9. to handle this in a way that makes the transition as smooth as possible.
  10. A practical example
  11. -------------------
  12. .. TODO: Add example that showcases more details like original default arguments etc.
  13. These changes are taken from `pull request #88047 <https://github.com/godotengine/godot/pull/88047>`_, which added
  14. new pathing options to ``AStarGrid2D`` and other AStar classes.
  15. Among other changes, these methods were modified in ``core/math/a_star_grid_2d.h``:
  16. .. code-block:: cpp
  17. Vector<Vector2> get_point_path(const Vector2i &p_from, const Vector2i &p_to);
  18. TypedArray<Vector2i> get_id_path(const Vector2i &p_from, const Vector2i &p_to);
  19. To:
  20. .. code-block:: cpp
  21. Vector<Vector2> get_point_path(const Vector2i &p_from, const Vector2i &p_to, bool p_allow_partial_path = false);
  22. TypedArray<Vector2i> get_id_path(const Vector2i &p_from, const Vector2i &p_to, bool p_allow_partial_path = false);
  23. This meant adding new compatibility method bindings to the file, which should be in the ``protected`` section of
  24. the code, usually placed next to ``_bind_methods()``:
  25. .. code-block:: cpp
  26. #ifndef DISABLE_DEPRECATED
  27. TypedArray<Vector2i> _get_id_path_bind_compat_88047(const Vector2i &p_from, const Vector2i &p_to);
  28. Vector<Vector2> _get_point_path_bind_compat_88047(const Vector2i &p_from, const Vector2i &p_to);
  29. static void _bind_compatibility_methods();
  30. #endif
  31. They should start with a ``_`` to indicate that they are internal, and end with ``_bind_compat_`` followed by the PR number
  32. that introduced the change (``88047`` in this example). These compatibility methods need to be implemented in a dedicated file,
  33. like ``core/math/a_star_grid_2d.compat.inc`` in this case:
  34. .. code-block:: cpp
  35. /**************************************************************************/
  36. /* a_star_grid_2d.compat.inc */
  37. /**************************************************************************/
  38. /* This file is part of: */
  39. /* GODOT ENGINE */
  40. /* https://godotengine.org */
  41. /**************************************************************************/
  42. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  43. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  44. /* */
  45. /* Permission is hereby granted, free of charge, to any person obtaining */
  46. /* a copy of this software and associated documentation files (the */
  47. /* "Software"), to deal in the Software without restriction, including */
  48. /* without limitation the rights to use, copy, modify, merge, publish, */
  49. /* distribute, sublicense, and/or sell copies of the Software, and to */
  50. /* permit persons to whom the Software is furnished to do so, subject to */
  51. /* the following conditions: */
  52. /* */
  53. /* The above copyright notice and this permission notice shall be */
  54. /* included in all copies or substantial portions of the Software. */
  55. /* */
  56. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  57. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  58. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  59. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  60. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  61. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  62. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  63. /**************************************************************************/
  64. #ifndef DISABLE_DEPRECATED
  65. #include "core/variant/typed_array.h"
  66. TypedArray<Vector2i> AStarGrid2D::_get_id_path_bind_compat_88047(const Vector2i &p_from_id, const Vector2i &p_to_id) {
  67. return get_id_path(p_from_id, p_to_id, false);
  68. }
  69. Vector<Vector2> AStarGrid2D::_get_point_path_bind_compat_88047(const Vector2i &p_from_id, const Vector2i &p_to_id) {
  70. return get_point_path(p_from_id, p_to_id, false);
  71. }
  72. void AStarGrid2D::_bind_compatibility_methods() {
  73. ClassDB::bind_compatibility_method(D_METHOD("get_id_path", "from_id", "to_id"), &AStarGrid2D::_get_id_path_bind_compat_88047);
  74. ClassDB::bind_compatibility_method(D_METHOD("get_point_path", "from_id", "to_id"), &AStarGrid2D::_get_point_path_bind_compat_88047);
  75. }
  76. #endif // DISABLE_DEPRECATED
  77. Unless the change in compatibility is complex, the compatibility method should simply call the modified method directly,
  78. instead of duplicating that method. Make sure to match the default arguments for that method (in the example above this would be ``false``).
  79. This file should always be placed next to the original file, and have ``.compat.inc`` at the end instead of ``.cpp`` or ``.h``.
  80. Next, this should be included in the ``.cpp`` file we're adding compatibility methods to, so ``core/math/a_star_grid_2d.cpp``:
  81. .. code-block:: cpp
  82. #include "a_star_grid_2d.h"
  83. #include "a_star_grid_2d.compat.inc"
  84. #include "core/variant/typed_array.h"
  85. And finally, the changes reported by the API validation step should be added to the relevant validation file. Because this was
  86. done during the development of 4.3, this would be ``misc/extension_api_validation/4.2-stable.expected`` (including changes not shown in
  87. this example):
  88. .. code-block:: text
  89. GH-88047
  90. --------
  91. Validate extension JSON: Error: Field 'classes/AStar2D/methods/get_id_path/arguments': size changed value in new API, from 2 to 3.
  92. Validate extension JSON: Error: Field 'classes/AStar2D/methods/get_point_path/arguments': size changed value in new API, from 2 to 3.
  93. Validate extension JSON: Error: Field 'classes/AStar3D/methods/get_id_path/arguments': size changed value in new API, from 2 to 3.
  94. Validate extension JSON: Error: Field 'classes/AStar3D/methods/get_point_path/arguments': size changed value in new API, from 2 to 3.
  95. Validate extension JSON: Error: Field 'classes/AStarGrid2D/methods/get_id_path/arguments': size changed value in new API, from 2 to 3.
  96. Validate extension JSON: Error: Field 'classes/AStarGrid2D/methods/get_point_path/arguments': size changed value in new API, from 2 to 3.
  97. Added optional "allow_partial_path" argument to get_id_path and get_point_path methods in AStar classes.
  98. Compatibility methods registered.
  99. The instructions for how to add to that file are at the top of the file itself.
  100. If you get a "Hash changed" error for a method, it means that the compatibility binding is missing or incorrect.
  101. Such lines shouldn't be added to the ``.expected`` file, but fixed by binding the proper compatibility method.
  102. And that's it! You might run into a bit more complicated cases, like rearranging arguments,
  103. changing return types, etc., but this covers the basic on how to use this system.
  104. For more information, see `pull request #76446 <https://github.com/godotengine/godot/pull/76446>`_.