Picking up an object and dragging it with the mouse

caesius6

Member
Hi there!

I'm running into a issue when I attempt to pick up an instance of an object with my mouse cursor and move it someplace else.

To preface, I'm making a game of checkers. I've created my room, tiled it as a checker board, and have one object: a red checker ("obj_red"). I've placed multiple instances of it on the board in the appropriate spaces. My first order of business was to get the game running and allow the player to pick up the piece and drop it somewhere else, never minding the rules of moving on a checkerboard. I'm going to post my code, my thought process of the logic, and hopefully someone can assist me in understanding what's happening.

In a step event for the red checker (obj_red) I've written:

GML:
while mouse_check_button(mb_left) = true {
   var checker = instance_position(mouse_x, mouse_y, obj_red);
    if (checker != noone) {
        checker.x = mouse_x;
        checker.y = mouse_y;
    }
}
My thought process here is this:
  • Is the left mouse button being held down?
    • If so, create a temporary variable named "checker" and assign it to the instance_position function, which from my understanding should return the ID of the instance colliding with my mouse position. Elsewise it will return "noone."
      • If it does not return "noone," set that instance's x and y to be my mouse x and y.
What I'm finding is the game does not pick up any of the red checkers.

To test and make sure there's a collision being detected, I changed the code to as follows:

GML:
while mouse_check_button(mb_left) = true {
    if place_meeting(mouse_x, mouse_y, obj_red)
        {
            show_message("Hello World.");
    }
}
Basically, instead of moving a checker, if there's a collision, pop up a dialogue box with "Hello World." This ended up proving true in that the intended effect happened to all but one of my red checker objects placed in the room. All but the top left one.

unknown.png

When pressing the LMB over the top left one, nothing would happen. Furthermore, nothing would happen afterwards if I was to click on any other checker that previously worked. My CPU temperature would also raise and the fan would get audibly louder. I found that selecting a different open window on my computer, then returning to the game would correct this. In addition to that happening when I pressed the LMB over that top left checker, the same would happen if I pressed LMB over anything that wasn't a red checker. I deleted the top left checker instance and added it back in only to find that the issue moved one checker over, to the second from the left on the top row. When I returned the code back to the "pick up and drop" the same thing would happen to the CPU temp and fan noise no matter which checker I tried to move, leading me to believe I can link the two outcomes. Though what that accomplishes... I'm not really sure.

So, with all that laid out, I'm curious as to why any of this is happening. I feel like the answer is right in front of me, but at the same time I'm a bit unsure of why that "freezing" issue is happening, and if it's a code issue why it only happens on that one instance of the red checker.

If it's any help I've attached a dxdiag export (well, images of it, I couldn't upload the text doc, but happy to copy and paste if that's necessary), and for reference I'm running the steam version of GMS2. The issue also persists on my Surface Pro if I attempt it there. As a precaution I've reinstalled GMS2 as well as trashed the project and started over, both not having any change in the outcome. Any help is appreciated!
 

Attachments

Let's Clone

Member
There's a few ways to do this (as is every endeavor with programming).

What I would do:
inside the create event of each checker piece I would have a boolean:
GML:
isSelected = false;
Then in the step event:

GML:
if (position_meeting(mouse_x, mouse_y, id))
    if (mouse_check_button_pressed(mb_left)
        isSelected = true;
       
if (isSelected) {
    x = mouse_x;
    y = mouse_y;
   
    if (mouse_ceck_button_released(mb_left)) {
        isSelected = false;
        // SNAP CHECKER PIECE TO GRID
        x = x div CELL_WIDTH * CELL_WIDTH;
        y = y div CELL_HEIGHT * CELL_HEIGHT;
    }
}
I haven't tested the code. But something to that effect will work.
 

caesius6

Member
Thank you! I'll give your code a go as soon as I can and let you know if it works. I haven't yet dove into creating a grid in the room and snapping pieces to it, so double appreciation for the lead in syntax for that part. I'll be sure to do some research on that.

Any idea about why one instance of the checker wasn't triggering the "show_message" prompt and was getting sort of...stuck? Part of me is thinking don't dwell on it, just use different code that works and move on, but another part of me is curious because it seems so peculiar. Perhaps I'm just over thinking it.
 

Nidoking

Member
You really need to understand what while loops do and what they don't. Namely, nothing else happens in your game while you're running a while loop. No inputs, no drawing, no steps, no calculations. Nothing.
 

caesius6

Member
You really need to understand what while loops do and what they don't. Namely, nothing else happens in your game while you're running a while loop. No inputs, no drawing, no steps, no calculations. Nothing.
I see! So are you saying that's what causing the issue I was having where it would "freeze?"

I was having success with the "show_message" function during the while loops, only not with a very specific instance of the object. All the other instances would let me click, see the message, and continue on as many times as I desired (or at least the dozen or so times I tried, I didn't go past that many attempts), but this one particular instance seemed to be having an issue.

I understand the dangers of a while loop now, and I see they aren't the correct action for this particular logic. As I move forward in using GML I may come across a time where the while loop will be beneficial, so I'm curious the reason for a particular instance giving me an issue. Is that something to do with the while loop, or would that be another issue that I should keep an eye out for?
 

Nidoking

Member
Presumably, that one instance would get stuck in the while loop and the others would not. In particular, none of those conditions are capable of changing during the execution of the loop, so either the loop happens forever or it never happens at all.
 

Let's Clone

Member
Grids are a lot easier to use than they initially seem. I use them in almost all of my tutorials (i teach how to make clones of classic game at Youtube: LetsClone).

I'm working on a ds_grid guide now, going over functionality and use cases. And I've been playing with isomorphic perspectives, so I might throw that in to the guide, or make a part 2 including it.
 
Top