I would like to draw a square in the _draw() function, using the grid from a Tilemap node, but I'm having trouble pointing to its start and end.When I drag from the left side to the right bottom, the beginning of the drawing is good, but the end is one tile line too short.And when I start drawing in the opposite direction, then the beginning starts one tile line away, but the end is fine.

func _process(delta):
	if Input.is_action_just_pressed("lpm"):
		draw_rec = map_to_world($".".world_to_map(get_global_mouse_position())) 
	update()

func _draw():

if draw_rec:
	draw_rect(Rect2(draw_rec,map_to_world($".".world_to_map(get_global_mouse_position()))  -draw_rec),
	Color(0.4,0.6,1,0.5),true)

map_to_world() always returns top left corner of a tile. So add 1 to the larger x and larger y tile coordinate before transforming them to world coordinates.

a year later