# How to make objects snap to grid

is there anyway to use the grid to snap objects to the base layer e.g. instead of the drag-able object being able to be dragged smoothly i want it so it snaps to grid every 25 pixels (i just dont know how to implement it) any help would be much appreciated

Search for a “Edit the grid” in the top bar of the Scene tab, you can set the grid size, color, and the “snap to grid” option

i have done that but when i preview my game it does snap to it the object just takes the position where you release left click

I think he’s asking on how to implement it in his game.

Ohhhh… hehe
Are you talking about the “Draggable Automatism”? Ok, here we go:
SnapToGrid.rar (5.1 KB)
The snap (in a 32x32 grid) is given by the code:

``Position.X() = 32*(round(Position.X()/32))``

So: First divide the position by 32 and round to get the integer for the grid, then multiply by 32 to get the old position rounded
Example: ```If position is 35: 35/32 = 1.09 ==> round(1.09) = 1 ==> new position = 32*1 = 32 If position is 70: 70/32 = 2.19 ==> round(2.19) = 2 ==> new position = 32*2 = 64```

Thanks for that ill have a play around with it and get back to you soon thanks for the help

i have done up to the line and now it just snaps to 25x25 which im after but its no longer dragable and i dont understand what to click on to input If position is 35: 35/32 = 1.09 ==> round(1.09) = 1 ==> new position = 321 = 32
If position is 70: 70/32 = 2.19 ==> round(2.19) = 2 ==> new position = 32
2 = 64

haha feel abit stupid now iv sorted it thanks just a blip in the code lol

Great!

Another formula (found it in Construct Classic example ‘Sprite Editor’) for the grid 10x10 is:
ObjectPosition=int(MouseX/10)*10, int(MouseY/10)*10
In GD it looks something like this:
No conditions,
Action:

So, if you want 25x25 grid, change 10 to 25.

Wow thanks didnt know there wer so many ways to get around things thanks every1 for the help

Is there anyway that after you release the mouse button the object moves back to its original position?

Thank You
InsertCoin25

I’m not sure what are you talking about, anyway it sounds easy, could you explain what do you mean? Drag the object (initially at X;Y), and put it again in X;Y when you’ve finished dragging it?

Say I have a sprite that I want to interact with another sprite. Sprite 1 can be dragged and Sprite 2 is stationary (not able to drag). If I drag Sprite 1 onto Sprite 2 and they cannot interact with one another, I want Sprite 1 to move back to its original position. So basically you had it right. Sorry should have explained better, but thank you for the reply.

InsertCoin25

Ok, just save the object position in two variables (Object_X and Object_Y for example) when you start dragging the object; finally, when you’ve finished dragging (and your conditions are met), set the object position = the saved variables
You have to ensure the position is saved only one time when you start dragging the object, for example with the conditions: “Object is beign dragged + Trigger once”. You can check if variables are saved only one time with the debugger (native platform)

Thank you for reply. I am having trouble trying to figure out how to save the X and Y coordinates into a variable. Also, would I need a Move to Position Action for the dragable sprite after I release the mouse/touch button? Or, does it go back to the X and Y coordinates on its own? Would you show me the code layout of this project, so I can get an idea of what I am missing or doing wrong.

Thank You
InsertCoin25

Sure, no problem, it’s short and easy
SnapToGrid.rar (5.75 KB)

The example is perfect. You made it look so easy . Thank you for the example, much appreciated.

InsertCoin25