Debian Codegraph @

doc/classes/AStar2D.xml

raw symbols SCIP
1 <?xml version="1.0" encoding="UTF-8" ?>
2 <class name="AStar2D" inherits="RefCounted" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd">
3 <brief_description>
4 An implementation of A* for finding the shortest path between two vertices on a connected graph in 2D space.
5 </brief_description>
6 <description>
7 An implementation of the A* algorithm, used to find the shortest path between two vertices on a connected graph in 2D space.
8 See [AStar3D] for a more thorough explanation on how to use this class. [AStar2D] is a wrapper for [AStar3D] that enforces 2D coordinates.
9 </description>
10 <tutorials>
11 <link title="Grid-based Navigation with AStarGrid2D Demo">https://godotengine.org/asset-library/asset/2723</link>
12 </tutorials>
13 <methods>
14 <method name="_compute_cost" qualifiers="virtual const">
15 <return type="float" />
16 <param index="0" name="from_id" type="int" />
17 <param index="1" name="to_id" type="int" />
18 <description>
19 Called when computing the cost between two connected points.
20 Note that this function is hidden in the default [AStar2D] class.
21 </description>
22 </method>
23 <method name="_estimate_cost" qualifiers="virtual const">
24 <return type="float" />
25 <param index="0" name="from_id" type="int" />
26 <param index="1" name="end_id" type="int" />
27 <description>
28 Called when estimating the cost between a point and the path's ending point.
29 Note that this function is hidden in the default [AStar2D] class.
30 </description>
31 </method>
32 <method name="_filter_neighbor" qualifiers="virtual const">
33 <return type="bool" />
34 <param index="0" name="from_id" type="int" />
35 <param index="1" name="neighbor_id" type="int" />
36 <description>
37 Called when neighboring enters processing and if [member neighbor_filter_enabled] is [code]true[/code]. If [code]true[/code] is returned the point will not be processed.
38 Note that this function is hidden in the default [AStar2D] class.
39 </description>
40 </method>
41 <method name="add_point">
42 <return type="void" />
43 <param index="0" name="id" type="int" />
44 <param index="1" name="position" type="Vector2" />
45 <param index="2" name="weight_scale" type="float" default="1.0" />
46 <description>
47 Adds a new point at the given position with the given identifier. The [param id] must be 0 or larger, and the [param weight_scale] must be 0.0 or greater.
48 The [param weight_scale] is multiplied by the result of [method _compute_cost] when determining the overall cost of traveling across a segment from a neighboring point to this point. Thus, all else being equal, the algorithm prefers points with lower [param weight_scale]s to form a path.
49 [codeblocks]
50 [gdscript]
51 var astar = AStar2D.new()
52 astar.add_point(1, Vector2(1, 0), 4) # Adds the point (1, 0) with weight_scale 4 and id 1
53 [/gdscript]
54 [csharp]
55 var astar = new AStar2D();
56 astar.AddPoint(1, new Vector2(1, 0), 4); // Adds the point (1, 0) with weight_scale 4 and id 1
57 [/csharp]
58 [/codeblocks]
59 If there already exists a point for the given [param id], its position and weight scale are updated to the given values.
60 </description>
61 </method>
62 <method name="are_points_connected" qualifiers="const">
63 <return type="bool" />
64 <param index="0" name="id" type="int" />
65 <param index="1" name="to_id" type="int" />
66 <param index="2" name="bidirectional" type="bool" default="true" />
67 <description>
68 Returns whether there is a connection/segment between the given points. If [param bidirectional] is [code]false[/code], returns whether movement from [param id] to [param to_id] is possible through this segment.
69 </description>
70 </method>
71 <method name="clear">
72 <return type="void" />
73 <description>
74 Clears all the points and segments.
75 </description>
76 </method>
77 <method name="connect_points">
78 <return type="void" />
79 <param index="0" name="id" type="int" />
80 <param index="1" name="to_id" type="int" />
81 <param index="2" name="bidirectional" type="bool" default="true" />
82 <description>
83 Creates a segment between the given points. If [param bidirectional] is [code]false[/code], only movement from [param id] to [param to_id] is allowed, not the reverse direction.
84 [codeblocks]
85 [gdscript]
86 var astar = AStar2D.new()
87 astar.add_point(1, Vector2(1, 1))
88 astar.add_point(2, Vector2(0, 5))
89 astar.connect_points(1, 2, false)
90 [/gdscript]
91 [csharp]
92 var astar = new AStar2D();
93 astar.AddPoint(1, new Vector2(1, 1));
94 astar.AddPoint(2, new Vector2(0, 5));
95 astar.ConnectPoints(1, 2, false);
96 [/csharp]
97 [/codeblocks]
98 </description>
99 </method>
100 <method name="disconnect_points">
101 <return type="void" />
102 <param index="0" name="id" type="int" />
103 <param index="1" name="to_id" type="int" />
104 <param index="2" name="bidirectional" type="bool" default="true" />
105 <description>
106 Deletes the segment between the given points. If [param bidirectional] is [code]false[/code], only movement from [param id] to [param to_id] is prevented, and a unidirectional segment possibly remains.
107 </description>
108 </method>
109 <method name="get_available_point_id" qualifiers="const">
110 <return type="int" />
111 <description>
112 Returns the next available point ID with no point associated to it.
113 </description>
114 </method>
115 <method name="get_closest_point" qualifiers="const">
116 <return type="int" />
117 <param index="0" name="to_position" type="Vector2" />
118 <param index="1" name="include_disabled" type="bool" default="false" />
119 <description>
120 Returns the ID of the closest point to [param to_position], optionally taking disabled points into account. Returns [code]-1[/code] if there are no points in the points pool.
121 [b]Note:[/b] If several points are the closest to [param to_position], the one with the smallest ID will be returned, ensuring a deterministic result.
122 </description>
123 </method>
124 <method name="get_closest_position_in_segment" qualifiers="const">
125 <return type="Vector2" />
126 <param index="0" name="to_position" type="Vector2" />
127 <description>
128 Returns the closest position to [param to_position] that resides inside a segment between two connected points.
129 [codeblocks]
130 [gdscript]
131 var astar = AStar2D.new()
132 astar.add_point(1, Vector2(0, 0))
133 astar.add_point(2, Vector2(0, 5))
134 astar.connect_points(1, 2)
135 var res = astar.get_closest_position_in_segment(Vector2(3, 3)) # Returns (0, 3)
136 [/gdscript]
137 [csharp]
138 var astar = new AStar2D();
139 astar.AddPoint(1, new Vector2(0, 0));
140 astar.AddPoint(2, new Vector2(0, 5));
141 astar.ConnectPoints(1, 2);
142 Vector2 res = astar.GetClosestPositionInSegment(new Vector2(3, 3)); // Returns (0, 3)
143 [/csharp]
144 [/codeblocks]
145 The result is in the segment that goes from [code]y = 0[/code] to [code]y = 5[/code]. It's the closest position in the segment to the given point.
146 </description>
147 </method>
148 <method name="get_id_path">
149 <return type="PackedInt64Array" />
150 <param index="0" name="from_id" type="int" />
151 <param index="1" name="to_id" type="int" />
152 <param index="2" name="allow_partial_path" type="bool" default="false" />
153 <description>
154 Returns an array with the IDs of the points that form the path found by AStar2D between the given points. The array is ordered from the starting point to the ending point of the path.
155 If [param from_id] point is disabled, returns an empty array (even if [code]from_id == to_id[/code]).
156 If [param from_id] point is not disabled, there is no valid path to the target, and [param allow_partial_path] is [code]true[/code], returns a path to the point closest to the target that can be reached.
157 [b]Note:[/b] When [param allow_partial_path] is [code]true[/code] and [param to_id] is disabled the search may take an unusually long time to finish.
158 [codeblocks]
159 [gdscript]
160 var astar = AStar2D.new()
161 astar.add_point(1, Vector2(0, 0))
162 astar.add_point(2, Vector2(0, 1), 1) # Default weight is 1
163 astar.add_point(3, Vector2(1, 1))
164 astar.add_point(4, Vector2(2, 0))
165
166 astar.connect_points(1, 2, false)
167 astar.connect_points(2, 3, false)
168 astar.connect_points(4, 3, false)
169 astar.connect_points(1, 4, false)
170
171 var res = astar.get_id_path(1, 3) # Returns [1, 2, 3]
172 [/gdscript]
173 [csharp]
174 var astar = new AStar2D();
175 astar.AddPoint(1, new Vector2(0, 0));
176 astar.AddPoint(2, new Vector2(0, 1), 1); // Default weight is 1
177 astar.AddPoint(3, new Vector2(1, 1));
178 astar.AddPoint(4, new Vector2(2, 0));
179
180 astar.ConnectPoints(1, 2, false);
181 astar.ConnectPoints(2, 3, false);
182 astar.ConnectPoints(4, 3, false);
183 astar.ConnectPoints(1, 4, false);
184 long[] res = astar.GetIdPath(1, 3); // Returns [1, 2, 3]
185 [/csharp]
186 [/codeblocks]
187 If you change the 2nd point's weight to 3, then the result will be [code][1, 4, 3][/code] instead, because now even though the distance is longer, it's "easier" to get through point 4 than through point 2.
188 </description>
189 </method>
190 <method name="get_point_capacity" qualifiers="const">
191 <return type="int" />
192 <description>
193 Returns the capacity of the structure backing the points, useful in conjunction with [method reserve_space].
194 </description>
195 </method>
196 <method name="get_point_connections">
197 <return type="PackedInt64Array" />
198 <param index="0" name="id" type="int" />
199 <description>
200 Returns an array with the IDs of the points that form the connection with the given point.
201 [codeblocks]
202 [gdscript]
203 var astar = AStar2D.new()
204 astar.add_point(1, Vector2(0, 0))
205 astar.add_point(2, Vector2(0, 1))
206 astar.add_point(3, Vector2(1, 1))
207 astar.add_point(4, Vector2(2, 0))
208
209 astar.connect_points(1, 2, true)
210 astar.connect_points(1, 3, true)
211
212 var neighbors = astar.get_point_connections(1) # Returns [2, 3]
213 [/gdscript]
214 [csharp]
215 var astar = new AStar2D();
216 astar.AddPoint(1, new Vector2(0, 0));
217 astar.AddPoint(2, new Vector2(0, 1));
218 astar.AddPoint(3, new Vector2(1, 1));
219 astar.AddPoint(4, new Vector2(2, 0));
220
221 astar.ConnectPoints(1, 2, true);
222 astar.ConnectPoints(1, 3, true);
223
224 long[] neighbors = astar.GetPointConnections(1); // Returns [2, 3]
225 [/csharp]
226 [/codeblocks]
227 </description>
228 </method>
229 <method name="get_point_count" qualifiers="const">
230 <return type="int" />
231 <description>
232 Returns the number of points currently in the points pool.
233 </description>
234 </method>
235 <method name="get_point_ids">
236 <return type="PackedInt64Array" />
237 <description>
238 Returns an array of all point IDs.
239 </description>
240 </method>
241 <method name="get_point_path">
242 <return type="PackedVector2Array" />
243 <param index="0" name="from_id" type="int" />
244 <param index="1" name="to_id" type="int" />
245 <param index="2" name="allow_partial_path" type="bool" default="false" />
246 <description>
247 Returns an array with the points that are in the path found by AStar2D between the given points. The array is ordered from the starting point to the ending point of the path.
248 If [param from_id] point is disabled, returns an empty array (even if [code]from_id == to_id[/code]).
249 If [param from_id] point is not disabled, there is no valid path to the target, and [param allow_partial_path] is [code]true[/code], returns a path to the point closest to the target that can be reached.
250 [b]Note:[/b] This method is not thread-safe; it can only be used from a single [Thread] at a given time. Consider using [Mutex] to ensure exclusive access to one thread to avoid race conditions.
251 Additionally, when [param allow_partial_path] is [code]true[/code] and [param to_id] is disabled the search may take an unusually long time to finish.
252 </description>
253 </method>
254 <method name="get_point_position" qualifiers="const">
255 <return type="Vector2" />
256 <param index="0" name="id" type="int" />
257 <description>
258 Returns the position of the point associated with the given [param id].
259 </description>
260 </method>
261 <method name="get_point_weight_scale" qualifiers="const">
262 <return type="float" />
263 <param index="0" name="id" type="int" />
264 <description>
265 Returns the weight scale of the point associated with the given [param id].
266 </description>
267 </method>
268 <method name="has_point" qualifiers="const">
269 <return type="bool" />
270 <param index="0" name="id" type="int" />
271 <description>
272 Returns whether a point associated with the given [param id] exists.
273 </description>
274 </method>
275 <method name="is_point_disabled" qualifiers="const">
276 <return type="bool" />
277 <param index="0" name="id" type="int" />
278 <description>
279 Returns whether a point is disabled or not for pathfinding. By default, all points are enabled.
280 </description>
281 </method>
282 <method name="remove_point">
283 <return type="void" />
284 <param index="0" name="id" type="int" />
285 <description>
286 Removes the point associated with the given [param id] from the points pool.
287 </description>
288 </method>
289 <method name="reserve_space">
290 <return type="void" />
291 <param index="0" name="num_nodes" type="int" />
292 <description>
293 Reserves space internally for [param num_nodes] points. Useful if you're adding a known large number of points at once, such as points on a grid.
294 </description>
295 </method>
296 <method name="set_point_disabled">
297 <return type="void" />
298 <param index="0" name="id" type="int" />
299 <param index="1" name="disabled" type="bool" default="true" />
300 <description>
301 Disables or enables the specified point for pathfinding. Useful for making a temporary obstacle.
302 </description>
303 </method>
304 <method name="set_point_position">
305 <return type="void" />
306 <param index="0" name="id" type="int" />
307 <param index="1" name="position" type="Vector2" />
308 <description>
309 Sets the [param position] for the point with the given [param id].
310 </description>
311 </method>
312 <method name="set_point_weight_scale">
313 <return type="void" />
314 <param index="0" name="id" type="int" />
315 <param index="1" name="weight_scale" type="float" />
316 <description>
317 Sets the [param weight_scale] for the point with the given [param id]. The [param weight_scale] is multiplied by the result of [method _compute_cost] when determining the overall cost of traveling across a segment from a neighboring point to this point.
318 </description>
319 </method>
320 </methods>
321 <members>
322 <member name="neighbor_filter_enabled" type="bool" setter="set_neighbor_filter_enabled" getter="is_neighbor_filter_enabled" default="false">
323 If [code]true[/code] enables the filtering of neighbors via [method _filter_neighbor].
324 </member>
325 </members>
326 </class>