Tilemap set_cellv() Obstacle problem for AStar

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By JeremiasDev

Hello, I created an AStar pathfinding with the video from GDQuest, then I did when you click on a character and then an object, in this case a tree. Then the character runs to the tree. But the tree is not recognized as an obstacle. I used in the script:

extends Node2D

onready var your_tile_map = get_parent (). get_node ("TileMap") # The path for your actual TileMap

func _ready ():
var grid_position = your_tile_map.world_to_map (self.global_position) # Converting position
your_tile_map.set_cellv (grid_position, 0) # Setting a collision tile

and in the script it creates a tile in the correct position of my tree, but the tile is not recognized as an obstacle for whatever reason. The ID is perfect, ID 0 stands for the object where there is an obstacle. So if I put the obstacle on the tree myself by hand then it works without problems but if I do not do that in the script.

Does anyone know what I mean and has a solution? if so it would be super nice!

:bust_in_silhouette: Reply From: Firty

I use a second tilemap to map collisions. Astar works with points and connections between those points, blocks that have an object cannot have points … To know this, just create an array containing the ids of the free blocks (it may be the opposite but I believe that you will have more blocks occupied than free) and pass a “for x in range” over the main tilemap identifying the id of each tile and setting it as free or busy in the second tilemap … Then just run the creation of Astar points on this secondary tilemap ignoring or Tiles blocked.