Creating and then placing blocks on the grid

Hello guys,

so, I have this problem. It might be simple, but Im totally lost. Im developing mobile game and lets say that I want to do this:

  • if player pressed on the white block, I want game to create that white block on the mouse position, and then follow the mouse while the left mouse button is pressed and when left mouse button is released, stop following it and snap to closest grid (red squares, see my professional image below)

I started working on it, and first problem which happened was, that game centered on mouse not only the new block it created, but also that block which was supposed to act like “button” for creating new block. I solved it by this:

  • I created new sprite with the same picture of that white block (so it looks exactly as new created blocks) and with different name.

So now when I press on that white block (button) it creates new block, centers it on the mouse and thats it, It works, because of button has different name. (If there is a way I can do this whole process with just one sprite, and not with two, using a button and then actual object, help is appreciated). But new problem appeared:

  • I pressed on that button, game created new block, I dragged it to place I want it to be and then released mouse button. No problems here. But problem is this - when I press on that button again to create another block, it creates new block, centers its position on the mouse etc… BUT it also centers on the mouse the block I already placed somewhere before. LOL.

And obviously I dont want that. I want that old block to stay on its place. So now you probably already know the question … Please, is here someone who can help me with this? Someone who knows how to make it? I already tried some stuff, but no success so far (maybe because im relatively new to GDevelop)

With this mechanic, player will be able to create his own 2D vehicle (there will be more building pieces than just one square block, but right now Im trying to develop the building mechanic itself) and then use it in game. Im just mentioning it in case it might be important somehow.
Thanks for any help and have a nice day (also I hope my english is understandable haha)

Hi, without knowing your events it is not really possible to give you a solution that matches what you have set up so far. One way to avoid that all instances of your object are moved again is to use boolean object variables. Here is a messy example that works but which could be probably optimized:

The object variable “placed” is false when the object is created and set to true when the mouse button is released. The object can only be moved when the variable is false, though.

Btw, in the example the sprite to create the block is different from the block. If I would use the same sprite I would - as you have done - create two objects (a button and the blocks) because that’s easy to do and also helps to separate UI and game objects.

1 Like

Thank you for your help. I managed to do it and also to do some other things, but one strange problem appeared. In my events you can see what I did. I used your solution and modified it, so I can move already placed blocks and delete them if they are not on the grid. I also managed to solve another problem … well … partially. When I started moving with block (the one already placed or the one that is newly created, it doesnt matter) and I moved over another block, it also started moving with the mouse, which is, ofcourse, not wanted. And as I said, I solved it, but there is still some weird problem with it. If I drag the block with mouse, and move it slowly, everything is working as intended, no other blocks start moving with mouse when I drag the first block above them. BUT if I move fast with mouse, it somehow breaks it. If I pick any block and move fast over another one, it picks the next one too. Im asking - how? Why it works only if I move with my mouse slowly (like really slowly) and not if I move fast? Its kinda annoying

Link to video of what is exactly happening:

You are right that’s annoying. I watched the video and I applied the draggable-behavior to the block-sprites in my example and I cannot reproduce the problem. For me everything works, no matter how fast or slow I move a placed block over others. I would suggest to build a small example project from scratch to see if you have the same problem again.

Tried what you have suggested and I have exactly the same problem even with new project. I dont know if Im doing something wrong, or it is some GDevelop bug. Can you send me how your events looks like? Or you have exactly same events as I? Im really confused … why it is not working when I move fast with my mouse. This exact thing was happening before i put “trigger once” condition in “moving objects” section. That makes sense to me. But now, when there is “trigger once” I really dont understand what is wrong. If left mouse button is down and is on the block, trigger once. So why it triggers another time while the mouse button is stil down?

Probably I will be able to solve it with one more sprite. Like one sprite for button, one sprite for when dragging with mouse, and one sprite for the object placed on the grid. I guess that will work, but that means that I will have to make 3 sprites for every building block I want to have in my game. I dont know, will this solution affect my game performance somehow?

I tried recreating your project and it acted the the same way. I think what’s happening is when you move the mouse fast, it turns the “cursor/touch is on object” false and starts the whole process over grabbing up othwr objects in the process… I’m no longer at my PC but I think it just needs a different conditions. (The mouse is moving faster than the sprite)

Tip: I like to use the “play sound” action to debug stuff. If you place a play sound action on your events you can hear them refire like the ringing of a bell when you shake the cursor.

1 Like

I have just added the draggable behavior and the ‘snap to grid’ extension to the block-object. My events look like this (not much different from the previous) and I don’t have this problem. When I pick my placed blocks and drag them to another position in this example they don’t snap to the grid again though.

1 Like

If you’re using the drag behavior, you don’t need to also manually move it. I think the actions are conflicting. Try removing the change position.

1 Like

Thank you guys, i will try it.

This project uses just two different sprites:


Download project here