So I am trying to connect my "selector" with my "map" so that when I select a tile I can create a visual indicator. I may also implement the tile coordinates into movement further down the line. However, the problem I have is that the tile coordinates that I get are relative to the camera position, so that the top left of the screen is always (0,0). However, I want (0,0) to always be the first placed tile in the tilemap regardless of where my camera moves. Is there a way to get tile coordinates based on global position rather than local position of screen?

extends TileMap

signal selectShip(globalPos)

func _input(event):
	if event is InputEventMouseButton:
		if event.button_index == MOUSE_BUTTON_LEFT and event.is_pressed():
			var global_clicked = event.position
			var tile_pos = local_to_map(global_clicked)
			emit_signal("selectShip",tile_pos)

This is the code that I've been working with so far. I haven't found any type of global_to_map() and I can convert the coordinates into a global position, but I can't figure out how I find the tile coordinate not relative to screen position.

  • xRegnarokx
    I thinks this will only work if your tilemap sits at position (0,0), so if your global coordinates are the same as your local ones. If you want to make sure, you should convert your mouse coordinates from global to local before passing them to the local_to_map function.

    so assuming that you are in the script of your tilemap, just change:

    var globalClicked = get_global_mouse_position()

    to

    var localClicked = get_global_mouse_position().to_local()

    that way you are sure that the mouse position is relative to your tilemap, even if that does not start in the upper left corner of your window.

I managed to solve this issue by changing my code to this.

func _input(event):
	if event is InputEventMouseButton:
		if event.button_index == MOUSE_BUTTON_LEFT and event.is_pressed():
			var globalClicked = get_global_mouse_position()
			var tileSnap = local_to_map(globalClicked)
			var tilePos = map_to_local(tileSnap)
			emit_signal("gridPos",tilePos)

I changed it to global_mouse_position, and then converted it local_to_map, then back again map_to_local. Not sure if this is the best way to accomplish it, but it did what I wanted.

    xRegnarokx
    I thinks this will only work if your tilemap sits at position (0,0), so if your global coordinates are the same as your local ones. If you want to make sure, you should convert your mouse coordinates from global to local before passing them to the local_to_map function.

    so assuming that you are in the script of your tilemap, just change:

    var globalClicked = get_global_mouse_position()

    to

    var localClicked = get_global_mouse_position().to_local()

    that way you are sure that the mouse position is relative to your tilemap, even if that does not start in the upper left corner of your window.

      xRegnarokx

      actually, I think I made a mistake and it needs to be
      to_local(get_global_mouse_position())

      same principle applies, though