Godot Version
4.2
Question
I am having issues with setting a tile in a AStargrid2d as “impassable”.
I am using the astargrid2d.set_point_solid(position, false) to set the point to not be included in navigation, but when navigating, the path returned is still including the impassable tiles.
Any help is appreciated!
My code is two pieces:
The setup of the grid:
extends Node
var grid_size: Vector2
var navigation_grid: AStarGrid2D = AStarGrid2D.new()
var cell_size: Vector2i = Vector2i(32,32)
var half_cell: Vector2i = Vector2i(16,16)
var current_tilemap: TileMap
var directions: Array[Vector2i] = [Vector2i.RIGHT, Vector2i.LEFT, Vector2i.UP, Vector2i.DOWN]
func _ready():
pass
## Setup of the size of the grid and the region of the grid based on the given TileMap
## Also updates the grid if there are changes (is_dirty = true)
func setup_astar_grid(map_tilemap: TileMap):
current_tilemap = map_tilemap
var map_tileset: TileSet = map_tilemap.tile_set
cell_size = map_tileset.tile_size
half_cell = map_tileset.tile_size / 2
navigation_grid.cell_size = map_tileset.tile_size
navigation_grid.region = map_tilemap.get_used_rect()
navigation_grid.diagonal_mode = AStarGrid2D.DIAGONAL_MODE_NEVER
navigation_grid.update()
navigation_grid.set_point_solid(Vector2i(0,1), false)
for layer in map_tilemap.get_layers_count():
configure_walkale_tiles(layer)
navigation_grid.update()
And then the actual function which parses all tilemap locations:
func configure_walkale_tiles(tilemap_layer: int):
for x in current_tilemap.get_used_rect().size.x:
for y in current_tilemap.get_used_rect().size.y:
var x_offset = current_tilemap.get_used_rect().position.x
var y_offset = current_tilemap.get_used_rect().position.y
var tile_position: Vector2i = Vector2i(x + x_offset,y + y_offset)
var tile_data = current_tilemap.get_cell_tile_data(tilemap_layer,tile_position)
if tile_data == null:
continue
if tile_data.get_custom_data("is_walkable") == false:
navigation_grid.set_point_solid(tile_position, false)
Let me know if anyone has experience with this!