• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Drag & Dropping

F

Floris18

Guest
Hello everyone,
I'm currently making a game where the objective is to make a sewer system with tubes. There is a limited amount of available. I'm sort of improving this game: https://www.crazygames.com/game/pipe-puzzle

My question now is: how do I program the drag and dropping of the tubes? I want to make it as follows: There is a stationary tube outside of the playing field and when pressed there is another tube created, slightly bigger but the same as the other one, and while still holding your left mouse button it can be dragged to a square in the grid.

My preference is drag and drop code but if that's not possible coding is also fine.

I hope I explained my problem well enough
 

Evanski

Raccoon Lord
Forum Staff
Moderator
personally it would be easier to use gml, but its possible with d&d it will just be more convoluted
 

NightFrost

Member
The concept of the mechanic would go something like this:

You have number of pipe section icons on screen. You could have a master controller object managing these but at your skill level it may be simpler to make each a separate object. Each of these objects holds in a variable the amount of pipe sections that can be grabbed from it. Each also check with mouse_check_button_pressed whether mouse is pressed down above them. When this happens, they check if their pipe section count is above zero. If so, they reduce it by one, create a new pipe section instance to be dragged .This would be a separate object, which will have drag detection code.

The draggable pipe section has code which constantly monitors mouse button with mouse_check_button. If so, they calculate their motion by taking the difference between previous and current mouse position and add it to their x/y positioning.

However, if the draggable pipe section notices via mouse_check_button_released that dragging ends, they check if they are over the game grid. This can be one by comparing pipe's coordinates to grid position with point_in_rectangle. If pipe is over the grid, it snaps to it by calculating its offset from grid's upper left corner and rounding its position to the grid cell. On the other hand if the pipe was released outside the game grid, it should destroy itself and increase the count in its origin icon back up by one.

The pipe sections placed onto the grid need to be draggable there too, so they need their own pressed-check as well. At this point it might be better to turn the pipe pieces to run as a state machine to more easily manage the different drag states they can be in.
 
F

Floris18

Guest
The concept of the mechanic would go something like this:

You have number of pipe section icons on screen. You could have a master controller object managing these but at your skill level it may be simpler to make each a separate object. Each of these objects holds in a variable the amount of pipe sections that can be grabbed from it. Each also check with mouse_check_button_pressed whether mouse is pressed down above them. When this happens, they check if their pipe section count is above zero. If so, they reduce it by one, create a new pipe section instance to be dragged .This would be a separate object, which will have drag detection code.

The draggable pipe section has code which constantly monitors mouse button with mouse_check_button. If so, they calculate their motion by taking the difference between previous and current mouse position and add it to their x/y positioning.

However, if the draggable pipe section notices via mouse_check_button_released that dragging ends, they check if they are over the game grid. This can be one by comparing pipe's coordinates to grid position with point_in_rectangle. If pipe is over the grid, it snaps to it by calculating its offset from grid's upper left corner and rounding its position to the grid cell. On the other hand if the pipe was released outside the game grid, it should destroy itself and increase the count in its origin icon back up by one.

The pipe sections placed onto the grid need to be draggable there too, so they need their own pressed-check as well. At this point it might be better to turn the pipe pieces to run as a state machine to more easily manage the different drag states they can be in.

Thank you sooo much!!!
I still have a question about the dragging of the pipe.
Coudl you maybe make a example if you have time because I don't know how to store the previous mouse position and the current.

Thanks in advance!
 

NightFrost

Member
There'd be various ways to implement mouse movement tracking, but since your use is just to drag single objects at a time, it should suffice to handle it inside the draggable pipe. First, when the pipe gets created it notes down mouse position to variables in Create event:
GML:
Prev_X = mouse_x;
Prev_Y = mouse_y;
Then in Step event, the pipe calculates difference between previous and current position, updates position by that difference, and saves current position as one to compare against during next Step:
GML:
x += mouse_x - Prev_X;
y += mouse_y - Prev_Y;
Prev_X = mouse_x;
Prev_Y = mouse_y;
As outlined previously, this repositioning should only run while mouse button is held down.
 
Top