how to make object snap to cell?

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

i want it to snap to cell/ grid. cell size = 96x96
how should i do it?

onready var objec_for_fill = preload("res://objectforcell.tscn")

onready var sprite_node = get_parent().get_node("Sprite")

func areAllTilesInRectOfType(globalRect: Rect2, tileType: int) -> bool:
	var firstCellCoords: Vector2 = world_to_map(globalRect.position)
	var lastCellCoords: Vector2 = world_to_map(globalRect.end)
	for x in range(firstCellCoords.x,lastCellCoords.x + 1):
		for y in range(firstCellCoords.y,lastCellCoords.y + 1):
			if get_cell(x, y) != tileType:
				return false
	return true    



func _unhandled_input(event: InputEvent) -> void:
	
	sprite_node.global_position = get_global_mouse_position()
	if event.is_action_pressed("Lclick"):
		var spriteGlobalRect: Rect2 = sprite_node.global_transform.xform(sprite_node.get_rect())
		var tileTypeToCheckFor: int = 7
		if areAllTilesInRectOfType(spriteGlobalRect, tileTypeToCheckFor) :
			var test_obj = objec_for_fill.instance()
			test_obj.global_position = spriteGlobalRect.position + spriteGlobalRect.size/2
			add_child(test_obj)
			
			print("obj pos",test_obj.position, "mouse pos", spriteGlobalRect.position)
:bust_in_silhouette: Reply From: potatobanana

just put this

var mouse_pos = world_to_map(get_global_mouse_position())
sprite_node.global_position = map_to_world(mouse_pos)