How can i only place nodes in a grid?

4.3.stable
i am making a game where you can place blocks to block the main characters path- and currently it all works however- it needs to be in a grid because currently the player stops walking even tho the block only might be a pixel or two up.
this is my code

the block placing code

func _block()
if Input.is_action_just_pressed(“place”):
var block = preload(“res://scenes/block.tscn”).instantiate()
self.add_sibling(block)
var temp_pos = get_global_mouse_position()
block.position = temp_pos

On Vector2 and Vector3 you can call snapped() to round your position to the next step you define:

For example:

block.position = get_global_mouse_position().snapped(Vector2(100,100))

2 Likes

thanks bro this helps a ton already-- how can i make the lil grey preview box be on the same position with the real box?? (in the scene the preview box is on 0, 0 and the real box is on 0, 0 on the packed scene) sorry if this is a stupid question im still kinda a beginner haha

also i know the video is fast i recorded it with the built in recorder

1 Like

I think you have to do the same thing for your preview box.

Oh and you might want to set global_position instead of position.
Position changes only the position in relation to the parent node.
Global Position is not affected by any parents position.
Just make sure that the node has been added as a child, for example to the scene tree, and then set world position. Otherwise the global position isn’t getting set.

2 Likes