If you use a tilemap, and you should, then the tilemap has expressions to help you.
PositionToTileX(positionx)
PositionToTileY(positiony)
Convert an X or Y layout co-ordinate in to the corresponding tile number in the tilemap. For example, this can be used to get the tile position under the mouse.
SnapX(positionx)
SnapY(positiony)
Snap an X or Y layout co-ordinate to the nearest tile. This also returns a layout co-ordinate, but aligned to the nearest tile in the tilemap.
TileToPositionX(indexx)
TileToPositionY(indexy)
Convert a tile position to layout co-ordinates. For example, this can be used to position a Sprite object on top of a given tile.
Using snap.
When a box is placed.
Set the position of the box to
x = tilemap.SnapX(box.X)
y = tilemap.SnapY(box.Y)
Using the other two.
When a box is placed.
Set the position of the box to
x = TileToPositionX(PositionToTileX(box.X))
y = TileToPositionY(PositionToTileY(box.Y))
If you dont use a tilemap.
When a box is placed.
Set the position of the box to
x = (round(box.X/32)) * 32
y = (round(box.Y/32)) * 32
But, that will not be exact.