Asset - Extension Mouse Double Click

csanyk

Member
Mouse Double Click is a free extension that provides a simple to implement double-click detection function.

There are two functions:
  • mouse_doubleclick_init() creates an instance variable used to check for timing the two click events. Put this in the Create event for your instance.
  • mouse_check_doubleclick() checks to see if the mouse button has been clicked, and if so whether there was an earlier click recorded within the time threshold specified in the function call. Put this in the step or mouse events of your instance.
Can work with left or right mouse buttons. You can determine the delay that will trigger a double click detection.

Since this was so simple to implement, and since I felt like GM:S ought to have the capability to detect double-clicks built into it already, this one is free:)

Marketplace Asset
Full Documentation

Please review it if you give it a try!
 
Last edited:
J

JapanGamer29

Guest
Hello, I can't get this to work and don't understand what I'm doing wrong.

I have an object (obj_restart), which is a button in a room.

In its Create event, I put:
GML:
mouse_doubleclick_init();
In its Left-Pressed event, I put:
GML:
if mouse_check_doubleclick(mb_left, 250000)
{
    // DOUBLE-CLICK - RETURN TO MENU
    room_goto(rm_main_menu);
}
else
{
    // SINGLE CLICK - RESTART ROOM
    room_restart();
}
I have no other code in the object. I don't get any errors, but only the single-click works. Am I wrong to put it in the Left-Pressed event?

If it matters, when I imported the extension, I got this compatibility report:
Code:
Converting GML script: mouse_check_doubleclick.gml
Converted description /// @description mouse_check_doubleclick_timer(mouse_button, delay_microseconds)
/// @param mouse_button
/// @param  delay_microseconds
I'm using GMS 2.2.5.378
 

csanyk

Member
Hello, I can't get this to work and don't understand what I'm doing wrong.

I have an object (obj_restart), which is a button in a room.

In its Create event, I put:
GML:
mouse_doubleclick_init();
In its Left-Pressed event, I put:
GML:
if mouse_check_doubleclick(mb_left, 250000)
{
    // DOUBLE-CLICK - RETURN TO MENU
    room_goto(rm_main_menu);
}
else
{
    // SINGLE CLICK - RESTART ROOM
    room_restart();
}
I have no other code in the object. I don't get any errors, but only the single-click works. Am I wrong to put it in the Left-Pressed event?

If it matters, when I imported the extension, I got this compatibility report:
Code:
Converting GML script: mouse_check_doubleclick.gml
Converted description /// @description mouse_check_doubleclick_timer(mouse_button, delay_microseconds)
/// @param mouse_button
/// @param  delay_microseconds
I'm using GMS 2.2.5.378
Hi @JapanGamer29, thanks for your question.

I had to send my laptop with GMS installed on it in for warranty repair and it's not back this week, so I am a bit limited in what I can do at this moment as far as trying to test your code. But, I think the problem is easy enough to see. You have a logic error in your code, which causes the double click condition check to never return true.

Let's think about what happens the first time you click the mouse:

  1. Your Left-Pressed event triggers.
  2. It checks to see if the click was a double click, and returns false.
  3. In the false branch, it restarts the room.

Can you see the problem now? By restarting the room, you have just re-created and reset all of the instances in the room, which means that your obj_restart was just created anew, and so this means it has just run its Create() Event again, which means that the mouse_doubleclick_init() script has run again, which means that it has initialized itself to having detected 0 clicks in the past 250000 microseconds, which means that the next click will be treated as a single click, which means that the room is going to restart again...

So how to fix this?

There are a few different approaches I think you could try.
  1. You could make the obj_restart() button persistent, but persistent objects have their own issues, which you would then need to handle.
  2. You could try storing the mouse_doubleclick variables in global scope instead of at the instance scope. The extension is not set up to work that way, but if you look inside the codefiles you can see exactly how the code is written, and you could create your own version of it that stores the data in globals. You'll also need to modify the Create() event in obj_restart to have it check to see whether the global version of the doubleclick timing variable exists or not, and not reset it if it does exist. Then, when the room restarts, the global variables should not be reset, and it should be able to detect when a left mouse press on the newly (re)created obj_restart was performed within 1/4 second of the last time someone left-pressed on an instance of obj_restart.
  3. Possibly something else.
It seems to me that both of the above approaches are "messy" though, and have limitations and issues that could cause you further trouble down the road if you tried to implement them.

But I hope that you can see that it matters what the consequences of the actions that the program takes as a result of detecting the first click, and that what you've programmed it to do is causing the object to be incapable of detecting a double-click. In other words, it's not the extension's fault; it's how you're using it. You'll need to re-think the approach you've taken and see if there's a better way to implement the logic in your restart object.
 
J

JapanGamer29

Guest
You have a logic error in your code, which causes the double click condition check to never return true.
Ahh! Of course! By removing the room_restart() command, and putting the double click check in the Step event instead, I can see now that the extension works well.

Thank you for taking the time to respond. I hope you get your laptop back in working order soon! :)
 
Top