navigation_using_navigationpathqueryobjects.rst 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. .. _doc_navigation_using_navigationpathqueryobjects:
  2. Using NavigationPathQueryObjects
  3. ================================
  4. ``NavigationPathQueryObjects`` can be used together with ``NavigationServer.query_path()``
  5. to obtain a heavily **customized** navigation path including optional **meta data** about the path.
  6. This requires more setup compared to obtaining a normal NavigationPath but lets you tailor
  7. the pathfinding and provided path data to the different needs of a project.
  8. NavigationPathQueryObjects consist of a pair of objects, a ``NavigationPathQueryParameters`` object holding the customization options
  9. for the query and a ``NavigationPathQueryResult`` that receives (regular) updates with the resulting path and meta data from the query.
  10. 2D and 3D versions of ``NavigationPathQueryParameters`` are available as
  11. :ref:`NavigationPathQueryParameters2D<class_NavigationPathQueryParameters2D>` and
  12. :ref:`NavigationPathQueryParameters3D<class_NavigationPathQueryParameters3D>` respectively.
  13. 2D and 3D versions of ``NavigationPathQueryResult`` are available as
  14. :ref:`NavigationPathQueryResult2D<class_NavigationPathQueryResult2D>` and
  15. :ref:`NavigationPathQueryResult3D<class_NavigationPathQueryResult3D>` respectively.
  16. Both parameters and result are used as a pair with the ``NavigationServer.query_path()`` function.
  17. For the available customization options and their use see the class doc of the parameters.
  18. While not a strict requirement, both objects are intended to be created once in advance, stored in a
  19. persistent variable for the agent and reused for every followup path query with updated parameters.
  20. This reuse avoids performance implications from frequent object creation if a project
  21. has a large quantity of simultaneous agents that regularly update their paths.
  22. .. tabs::
  23. .. code-tab:: gdscript 2D GDScript
  24. # Prepare query objects.
  25. var query_parameters := NavigationPathQueryParameters2D.new()
  26. var query_result := NavigationPathQueryResult2D.new()
  27. func query_path(p_start_position: Vector2, p_target_position: Vector2, p_navigation_layers: int = 1) -> PackedVector2Array:
  28. if not is_inside_tree():
  29. return PackedVector2Array()
  30. query_parameters.map = get_world_2d().get_navigation_map()
  31. query_parameters.start_position = p_start_position
  32. query_parameters.target_position = p_target_position
  33. query_parameters.navigation_layers = p_navigation_layers
  34. NavigationServer2D.query_path(query_parameters, query_result)
  35. var path: PackedVector2Array = query_result.get_path()
  36. return path
  37. .. code-tab:: gdscript 3D GDScript
  38. # Prepare query objects.
  39. var query_parameters := NavigationPathQueryParameters3D.new()
  40. var query_result := NavigationPathQueryResult3D.new()
  41. func query_path(p_start_position: Vector3, p_target_position: Vector3, p_navigation_layers: int = 1) -> PackedVector3Array:
  42. if not is_inside_tree():
  43. return PackedVector3Array()
  44. query_parameters.map = get_world_3d().get_navigation_map()
  45. query_parameters.start_position = p_start_position
  46. query_parameters.target_position = p_target_position
  47. query_parameters.navigation_layers = p_navigation_layers
  48. NavigationServer3D.query_path(query_parameters, query_result)
  49. var path: PackedVector3Array = query_result.get_path()
  50. return path