Can I check if a cell is being used in a tilemap using my mouse position

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

So I already have placement down and I have three tilemaps: grass, dirt and stone. They have different textures and currently I’m trying to use the dirt tilemap. I want to compare the position of my mouse to the cell and see if it is being used (by used I mean painted).

GameManager.mousetile = GameManager.Dirt.local_to_map(get_global_mouse_position())
GameManager.localPosition = GameManager.Dirt.map_to_local(GameManager.mousetile)
GameManager.worldPosition =

global_position = GameManager.worldPosition

What I have in my GameManager script:

	var usedCells = Dirt.get_used_cells(0)
	for i in usedCells:
		if worldPosition == i:
			isUsing = true

:bust_in_silhouette: Reply From: ShatteredReality

Hi, local_to_map takes a local position, not a global one, like from get_global_mouse_position(). You should call get_local_mouse_position() on GameManager.Dirt.

Thanks for the response, however that part doesn’t seem to the problem. I already have a grid-based placement system but it seems to really bug out when i change it to get_local_mouse_position(). I’m not sure if that’s because of my messy code (which it probably is) but I don’t think I have to change it. It’s my fault that I didn’t properly specify where the issue was. The problem is that in if worldPosition == i since the array (Dirt.get_used_cells(0)) is a vector2i whereas my worldPosition variable is a vector2, it cannot seem to compare the two values if they match or not, maybe I set it up wrongly.

ShinRamen | 2023-06-01 20:18

Well, if Dirt is located at 0,0 then local and global positions should be the same. But you shouldn’t be using world coordinates but rather cell coordinates. You have the cell coordinate stored in GameManager.mousetile.

ShatteredReality | 2023-06-01 20:51