Path-finding to interactable but solid tiles

Godot Version

4.4.1

Question

`Hello! I’m brand new to Godot so sorry if this is worded amateurish

I’m making a top-down point and click game with grid-based movement, and I have been following this video for the path-finding system using AstarGrid2D: https://www.youtube.com/watch?v=DkAmGxRuCk4, and it has been very helpful for the most part.

Though he coded the obstacle by using a custom data that set certain tiles solid, and in that way the player would not move if clicked on them. I wish to have certain interactive objects in the map that could be approached when clicked on, but still have the path-finding avoid it instead of passing through it.

Do you have any suggestion on how to approach this? Thank you in advance!`

When you click on a tile you need to check if its interactive, if yes you need to find the closest approachable tile and use pathfinding to walk towards it

1 Like

Hey I managed to make it work using your line of thoughts! Clicking on a solid tile would now route the player to one of the the adjacent grids. It’s a bit clunky but is serviceable, thanks a lot for the comment!

No problem. In general when faced with a problem in programming: Try to break it down in simpler problems

1 Like

Here’s the solution I’m using for future references:

	var mouse_coordinate = tile_map.local_to_map(get_global_mouse_position())
	var target_adjacent : Array = tile_map.get_surrounding_cells(mouse_coordinate)
	var is_adjacent_solid : Array
	var adjacent_grids_to_player_distance : Array
	var solid_index = 0
	
	if astar_grid.is_point_solid(mouse_coordinate) == true:
		for adjacent_cords in target_adjacent:
			is_adjacent_solid.append(astar_grid.is_point_solid(adjacent_cords))
		
		for remove_solid in is_adjacent_solid:
			if remove_solid == true:
				target_adjacent.remove_at(solid_index) 
			else:
				solid_index += 1
		
		for adjacent_cords in target_adjacent:
			adjacent_grids_to_player_distance.append(tile_map.map_to_local(adjacent_cords).distance_to(get_global_position()))
		
		var closest_adjacent_tile = adjacent_grids_to_player_distance.find(adjacent_grids_to_player_distance.min())
		
		mouse_coordinate = target_adjacent.get(closest_adjacent_tile)

Basically what happens when clicking on a solid tile:

  1. Stores the coordinates of the four adjacent tiles into an array
  2. Checks if there is any solids in the adjacent tiles, stores the bool result in a different array
  3. The two arrays are aligned, so using the bool array can delete the solids from the adjacent tiles array
  4. Calculate the remaining tiles’ distances to player, and find the closest one and route there

Put this down before the tutorial script calls get_id_path() , replace the destination in get_id_path() with variable mouse_coordinate, the rest is done by Retrobright’s code from the video

Probably not be the best way to do it, but if it helps anyone else that’ll be good

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.