Messengers should deliver their order message to the target unit, and avoiding obstacles doing so. Messengers are of type Unit (they take dmg and are blocked by obstacles).
Here's is a little help from our chatGPT friend:
Show me how to do simple path finding in godot 3.5 in 2D
Certainly! I can provide you with a simple example of how to implement pathfinding in Godot 3.5 in a 2D game. In this example, we'll be using the A* (A-star) algorithm for pathfinding.
Step 1: Set up the scene
- Create a new scene in Godot and add a TileMap node to it. This will be the map where the pathfinding will occur.
- Add a TileSet to the TileMap node and create two tiles: one for the walkable area and another for obstacles or unwalkable areas.
- Design your map by painting the tiles on the TileMap. Ensure you have both walkable and unwalkable areas to create a pathfinding scenario.
Step 2: Implement the pathfinding logic
- Create a new script for your main character or any other node that requires pathfinding.
- In the script, add the following code:
extends Node2D
var path = [] # The array to store the calculated path
func _ready():
var tilemap = $TileMap # Replace "TileMap" with the actual name of your TileMap node
var navigation = Navigation2D.new()
add_child(navigation)
navigation.navpoly_add_tilemap(tilemap)
func _input(event):
if event is InputEventMouseButton and event.button_index == BUTTON_LEFT and event.pressed:
var target = get_global_mouse_position()
var start = position
var navigation = $Navigation2D # Replace "Navigation2D" with the actual name of your Navigation2D node
path = navigation.get_simple_path(start, target)
if path.size() > 0:
path.pop_front()
follow_path()
func follow_path():
if path.size() > 0:
var next_point = path.front()
var direction = next_point - position
direction = direction.normalized()
position += direction * speed * delta
if position.distance_to(next_point) < 1:
path.pop_front()
follow_path()
Let's go through the code step by step:
- In the
_ready() function, we create a new Navigation2D node and add it as a child of our main character node. We also call navpoly_add_tilemap() to initialize the navigation data based on the TileMap's tileset.
- In the
_input() function, we handle the left mouse button click event. When the button is pressed, we calculate a new path from the current position to the mouse's position using get_simple_path(). If a valid path is found, we remove the starting point from the path and call follow_path() to start moving along the path.
- The
follow_path() function moves the character along the path by continuously updating the position based on the direction to the next point in the path. If the character reaches a point close enough to the next point, we remove it from the path and recursively call follow_path() to continue moving.
Step 3: Test the pathfinding
- Attach the script to your main character node or the node that will perform the pathfinding.
- Run the scene and click somewhere on the map with the left mouse button.
- The character should start moving towards the clicked position, following the calculated path.
That's it! You now have a basic implementation of pathfinding using the A* algorithm in Godot 3.5 for a 2D game. Feel free to modify and expand upon this example to suit your specific game requirements.
Messengers should deliver their order message to the target unit, and avoiding obstacles doing so. Messengers are of type Unit (they take dmg and are blocked by obstacles).
Here's is a little help from our chatGPT friend:
Show me how to do simple path finding in godot 3.5 in 2D
Certainly! I can provide you with a simple example of how to implement pathfinding in Godot 3.5 in a 2D game. In this example, we'll be using the A* (A-star) algorithm for pathfinding.
Step 1: Set up the scene
Step 2: Implement the pathfinding logic
Let's go through the code step by step:
_ready()function, we create a newNavigation2Dnode and add it as a child of our main character node. We also callnavpoly_add_tilemap()to initialize the navigation data based on the TileMap's tileset._input()function, we handle the left mouse button click event. When the button is pressed, we calculate a new path from the current position to the mouse's position usingget_simple_path(). If a valid path is found, we remove the starting point from the path and callfollow_path()to start moving along the path.follow_path()function moves the character along the path by continuously updating the position based on the direction to the next point in the path. If the character reaches a point close enough to the next point, we remove it from the path and recursively callfollow_path()to continue moving.Step 3: Test the pathfinding
That's it! You now have a basic implementation of pathfinding using the A* algorithm in Godot 3.5 for a 2D game. Feel free to modify and expand upon this example to suit your specific game requirements.