0 Favourites

# How do I make the player only able to place object on a grid

• 3 posts
• Hi, I am making a completely shameless Terraria/Starbound ripoff (eventually it will be less shameless, I promise) and something I can't figure out is how to make the player only able to place blocks on a grid. I've figured out how to constrain their reach using a line of sight behavior on the player's hitbox object, but they can still be placed anywhere in space instead of snapping to the same 32x32 grid the tiles are on. I'm sure there is a solution here but I need your help to find it!

• 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.