Skip to content

Instantly share code, notes, and snippets.

@fcingolani
Last active July 17, 2022 15:08
Show Gist options
  • Save fcingolani/035e43f57abf72801ec2e774fb89ad06 to your computer and use it in GitHub Desktop.
Save fcingolani/035e43f57abf72801ec2e774fb89ad06 to your computer and use it in GitHub Desktop.
AStar2D Visualizer for Godot
  1. Create a Node2D.
  2. Attach AStart2DVisualizer.gd to it.
  3. Somewhere in your code pass your AStar2D instance to its .visualize(astar) method, i.e.: get_node(path_to_your_visualizer_node).visualize(astar)
  4. Then run your game and you should see something like this:

screenshot

extends Node2D
class_name AStar2DVisualizer
export(float) var point_radius = 6
export(float) var scale_multiplier = 16
export(Vector2) var offset = Vector2(0,0)
export(Color) var enabled_point_color = Color('00ff00')
export(Color) var disabled_point_color = Color('ff0000')
export(Color) var line_color = Color('0000ff')
export(float) var line_width = 2
var astar : AStar2D
func visualize(new_astar : AStar2D):
astar = new_astar
update()
func _point_pos(id):
return offset + astar.get_point_position(id) * scale_multiplier
func _draw():
if not astar:
return
for point in astar.get_points():
for other in astar.get_point_connections(point):
draw_line(_point_pos(point), _point_pos(other), line_color, line_width)
var point_color = disabled_point_color if astar.is_point_disabled(point) else enabled_point_color
draw_circle(_point_pos(point), point_radius, point_color)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment