Game Maker Community: Mouse_get_click Script - Game Maker Community

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Mouse_get_click Script Returns weather mouse is right/left clicked or hovering over an area.

#1 User is offline   Recreate 

  • Programmer
  • Group: GMC Member
  • Posts: 2636
  • Joined: 05-April 08

Post icon  Posted 25 August 2009 - 04:48 AM

Here is a little script i made for myself for a game i was making, i found it and fixed it up a little works great :whistle:
What it does is it checks weather the mouse is hovering over a certain area, if it is not, and if it it left clicked or right clicked.
//Syntax:mouse_get_click(x1,y1,x2,y2)

//Returns: 
//0 if nothing
//1 if hover
//2 if left clicked
//3 if right clicked
//
//argument0 = x1
//argument1 = y1
//argument2 = x2
//argument3 = y2
//
minx_x=argument0
min_y=argument1
max_x=argument2
max_y=argument3
tmp_x = mouse_x
tmp_y = mouse_y
if(tmp_x>minx_x && tmp_x<max_x &&  tmp_y>min_y && tmp_y<max_y)
{
	if mouse_button = mb_left
	  {  
		retval=2
	  }
	if mouse_button = mb_right
	  {
		retval=3
	  }
	if mouse_button = mb_none 
	  {
		retval=1
	  }
	  
}
else
{
retval=0 
}
return retval

Tested in GM6.1 and GM7.5
Can be useful for something..I suppose.
Well Enjoy and Comment about it and tell me any bugs you noticed.
Thanks
~ReCreate
0

#2 User is offline   BBGaming 

  • Programmer
  • Group: GMC Member
  • Posts: 2092
  • Joined: 11-June 06

Posted 25 August 2009 - 05:09 AM

Hmm... I love doing this:
return( (mouse_x>argument0 and mouse_x<argument2 and mouse_y>argument1 and mouse_y<argument3) * 
(1 + mouse_check_button(mb_left) + mouse_check_button(mb_right)*2) )

:whistle: Returns 4 if both buttons are pressed.

This post has been edited by B&B_Gaming: 25 August 2009 - 05:13 AM

0

#3 User is offline   Recreate 

  • Programmer
  • Group: GMC Member
  • Posts: 2636
  • Joined: 05-April 08

Posted 25 August 2009 - 05:34 AM

Woah :(
That looks messy... :whistle:
0

#4 User is offline   DZiW 

  • GMC Member
  • Group: GMC Member
  • Posts: 570
  • Joined: 21-December 04

Posted 25 August 2009 - 09:42 AM

Usually such checks are implemented using bit-settings to avoid messing (odd numbers).
0

#5 User is offline   Recreate 

  • Programmer
  • Group: GMC Member
  • Posts: 2636
  • Joined: 05-April 08

Posted 02 September 2009 - 03:23 AM

What do you mean?
0

#6 User is offline   brett14 

  • GMC Member
  • Group: GMC Member
  • Posts: 861
  • Joined: 02-January 08

Posted 02 September 2009 - 07:11 AM

//Syntax:mouse_get_click(
x1,y1,x2,y2)
//Returns:
//0 if nothing
//1 if hover
//2 if left clicked
//3 if right clicked
//
//argument0 = x1
//argument1 = y1
//argument2 = x2
//argument3 = y2
//
minx_x=argument0
min_y=argument1
max_x=argument2
max_y=argument3
tmp_x = mouse_x
tmp_y = mouse_y
if(tmp_x>minx_x && tmp_x<max_x &&  tmp_y>min_y && tmp_y<max_y)
{
	if mouse_button = mb_left
	  {  
		retval=2
	  }
	if mouse_button = mb_right
	  {
		retval=3
	  }
	if mouse_button = mb_none
	  {
		retval=1
	  }
	  
}
else
{
retval=0
}
return retval


Just to let you know, you could make it more efficient by doing this.
/*
Syntax:mouse_get_click(x1,y1,x2,y2)
Returns:
0 if nothing
1 if hover
2 if left clicked
3 if right clicked

argument0 = x1
argument1 = y1
argument2 = x2
argument3 = y2
*/

if(mouse_x>argument0 && mouse_x<argument1 &&  mouse_y>argument2 && mouse_y<argument3)
{
	if mouse_button = mb_left
	  {  
		return 2;
	  }
	if mouse_button = mb_right{
		return 3;
	  }
	if mouse_button = mb_none
	  {
		return 1;
	  }
	  
}
return 0;


You do not need to set a return value, then return it, just simple return the value, then the script exits and returns the desired value. It is also faster to check directly against argument0, then to set a variable and then check against the variable. Also as a good practice, if you are using variables that are only used in this one script, put the "var" statement at the top of it, making the variables last only until the end of the script. This will make it both faster, and reduce memory consumption.

--or even better, a switch statement--

/*
Syntax:mouse_get_click(x1,y1,x2,y2)
Returns:
0 if nothing
1 if hover
2 if left clicked
3 if right clicked

argument0 = x1
argument1 = y1
argument2 = x2
argument3 = y2
*/

if(mouse_x>argument0 && mouse_x<argument1 &&  mouse_y>argument2 && mouse_y<argument3){
	 switch(mouse_button){
	 case(mb_left):
	 return 2;
	 break;
	 case(mb_right):
	 return 3;
	 break;
	 case(mb_none):
	 return 1;
	 break;
	 }  
}
return 0;


BTW,
B&B_Gaming's solution would work perfect (even if a little messy), it is about the fastest your going to get, it doesn't even need any checking, just a simple equation.

This post has been edited by brett14: 02 September 2009 - 07:18 AM

0

#7 User is offline   Desert Dog 

  • GMC Member
  • Group: GMC Member
  • Posts: 3024
  • Joined: 26-August 08

Posted 02 September 2009 - 07:35 AM

If your going for 'efficiency', don't use and, since GML doesn't implement short-circuiting.
Just use multiple if statements, nested inside each other.
0

#8 User is offline   Recreate 

  • Programmer
  • Group: GMC Member
  • Posts: 2636
  • Joined: 05-April 08

Posted 02 September 2009 - 05:20 PM

Oh...i get it now...Thanks
0

#9 User is offline   dark_master4 

  • GMC Member
  • Group: GMC Member
  • Posts: 1034
  • Joined: 15-February 04

Posted 04 September 2009 - 01:00 PM

View PostDesert Dog, on Sep 2 2009, 03:38 AM, said:

If your going for 'efficiency', don't use and, since GML doesn't implement short-circuiting.
Just use multiple if statements, nested inside each other.

Do you mean that in a 3 checks "if" statement, even if the first 2 are not equal, GM will evaluate the last one?

What if you use "mine>(variable+=1)*4" as your last statement? And then your 2 first statements depend on "variable". If your 2 first statements aren't equal, you might get buggy, especially if this is called in a "while(1)" loop.

Edit: That means short-circuiting isn't always the best solution. Even though GM doesn't use it, you will never notice a lag by calling multiple AND in an IF statement. If you do, that means you're doing something wrong and your code needs optimizing.

This post has been edited by dark_master4: 04 September 2009 - 01:01 PM

0

#10 User is offline   Desert Dog 

  • GMC Member
  • Group: GMC Member
  • Posts: 3024
  • Joined: 26-August 08

Posted 06 September 2009 - 02:16 AM

Quote

Do you mean that in a 3 checks "if" statement, even if the first 2 are not equal, GM will evaluate the last one?

Not totally sure what you said, but I mean this.

if ((x==5) and (y==6) and (z==10))
{
//do stuff.
}

In gml, even if x does not == 5, it will still check if y==6 and z==10, even though you don't need to, because the expression is already false.

In c++, if x does not == 5, it will automatically cancel (short-circuit) the other checks.

So, to make the code run more efficiently, you would write it like this
if (x==5) // check if x==5 is true..
{
	 if (y==6) // only if the previous statement is true, do we check this statement...
	 {
			  if (z==10) // and only if the previous two expression are true, do we evaluate this one
			  {
			  
			  }
	 }
}


This will give you a (rather) marginal increase in speed.

Quote

What if you use "mine>(variable+=1)*4" as your last statement? And then your 2 first statements depend on "variable". If your 2 first statements aren't equal, you might get buggy, especially if this is called in a "while(1)" loop.

I'm not quite sure what your trying to say, here. Sorry, :/

Quote

Even though GM doesn't use it, you will never notice a lag by calling multiple AND in an IF statement. If you do, that means you're doing something wrong and your code needs optimizing.

I'm not saying to 'not' use and, only in GML, I'm saying using and isn't actually as optimal, as using nested statements, in GML. So, for a script, you may as well write it as nested statements. It 'does' run, more optimally. :P

This post has been edited by Desert Dog: 06 September 2009 - 04:29 AM

0

#11 User is offline   dark_master4 

  • GMC Member
  • Group: GMC Member
  • Posts: 1034
  • Joined: 15-February 04

Posted 06 September 2009 - 04:30 PM

View PostDesert Dog, on Sep 5 2009, 10:19 PM, said:

I'm not saying to 'not' use and, only in GML, I'm saying using and isn't actually as optimal, as using nested statements, in GML. So, for a script, you may as well write it as nested statements. It 'does' run, more optimally. :P

All right, but in any case, it's not a bad programming practice to use multiple and in an if statement. I get your point.

This post has been edited by dark_master4: 06 September 2009 - 04:30 PM

0

#12 User is offline   BBGaming 

  • Programmer
  • Group: GMC Member
  • Posts: 2092
  • Joined: 11-June 06

Posted 06 September 2009 - 04:33 PM

View PostDesert Dog, on Sep 5 2009, 09:19 PM, said:

In gml, even if x does not == 5, it will still check if y==6 and z==10, even though you don't need to, because the expression is already false.

In c++, if x does not == 5, it will automatically cancel (short-circuit) the other checks.

The reason this is is because it's not always that simple. Take this for example:

if x==5 or (y==6 and z==10)

While this may be easy for you to tell what does and doesn't need to be checked, for an interpreter it's not as clear. Devising a general set of rules that will work for all conditions would be complicated, and even after you get that the implementation may even result in a speed decrease because of what needs to be checked. Remember that GM has extremely loose syntax.

It's simpler to just leave the responsibility in the user's hands. If it's necessary you can simply split the conditions up like this:

if x==5 { if y==6 { if z==10 {

}}}
To implement your own short-circuiting and still save line space.
0

#13 User is offline   Desert Dog 

  • GMC Member
  • Group: GMC Member
  • Posts: 3024
  • Joined: 26-August 08

Posted 06 September 2009 - 10:55 PM

View Postdark_master4, on Sep 7 2009, 04:33 AM, said:

All right, but in any case, it's not a bad programming practice to use multiple and in an if statement. I get your point.


It isn't, but I can show you an example of a bad way of using and, which many people people do.
Using the last example, doing this in GML, is ok. Your not going to suffer any speed lose, really, so you can get away with doing it.
if ((x==5) and (y==6) and (z==10))
{
//do stuff.
}


However, new programmers, often have a set up like this,
if ((x==5) and (y==6) and (z==10))
{
//do stuff.
}
else
if ((x==5) and (y==6) and (z==11))
{
//do stuff.
}


Obviously, it is FAR superior to use nested if statements, in this case. Which is why, I think it's a good habit to get into, using nested statements. (although, lazy programmers like me, still use and.... :P)

B&B_Gaming said:

The reason this is is because it's not always that simple. Take this for example:

if x==5 or (y==6 and z==10)

While this may be easy for you to tell what does and doesn't need to be checked, for an interpreter it's not as clear. Devising a general set of rules that will work for all conditions would be complicated, and even after you get that the implementation may even result in a speed decrease because of what needs to be checked. Remember that GM has extremely loose syntax.

I'm not sure I understand you. The interpreter reads the expression (y==6 and z==10), and if either of those two statements are false, (I'm guessing this is c++, so...) it will short-circuit, and return the value 'false'. (which, if x==5 is false, means that the entire expression is false).
I thought Boolean logic, was something computers excelled at. [It's certainly not something I find easy!]

Quote

It's simpler to just leave the responsibility in the user's hands. If it's necessary you can simply split the conditions up like this:

if x==5 { if y==6 { if z==10 {

}}}
To implement your own short-circuiting and still save line space.

That's pretty neat, actually :P
But I'm, personally, not fussy about saving line space, since the more space there is, the more room it is to put comments.

I just had a thought, since GM8 is being written in C++, does that mean it will have short-circuiting?

This post has been edited by Desert Dog: 06 September 2009 - 11:07 PM

0

#14 User is offline   Petey_Pii 

  • GMC Member
  • Group: GMC Member
  • Posts: 769
  • Joined: 29-April 09

Posted 07 September 2009 - 03:47 AM

gm8 is being written in C++? pretty neat script btw
0

#15 User is offline   dark_master4 

  • GMC Member
  • Group: GMC Member
  • Posts: 1034
  • Joined: 15-February 04

Posted 07 September 2009 - 11:40 AM

View PostDesert Dog, on Sep 6 2009, 06:58 PM, said:

I just had a thought, since GM8 is being written in C++, does that mean it will have short-circuiting?

Who said that?

This post has been edited by dark_master4: 07 September 2009 - 11:41 AM

0

#16 User is offline   brett14 

  • GMC Member
  • Group: GMC Member
  • Posts: 861
  • Joined: 02-January 08

Posted 07 September 2009 - 04:46 PM

GM8's runner was originally going to be written in c++, however I'm almost positive that they stayed with delphi.
0

#17 User is offline   Desert Dog 

  • GMC Member
  • Group: GMC Member
  • Posts: 3024
  • Joined: 26-August 08

Posted 07 September 2009 - 09:33 PM

[quote name='dark_master']Who said that?[/quote]
My apologizes, I shouldn't have stated it as a fact. On the yoyogames blog, they specifically said they were looking into writing it in C++, and I had thought they did that for the first Beta's.
[quote name='brett14' post='3283677' date='Sep 8 2009, 04:49 AM']GM8's runner was originally going to be written in c++, however I'm almost positive that they stayed with delphi.[/quote]
I feel way behind the times. :) Could you please gimme a link to confirm this? I've tried digging around a little, but it's hard to tell who knows what their actually talking about. As far as I can see, it seems they are (were) re-writing the runner in C++, but leaving the interface written in Delphi. (which I didn't know about, I thought they were re-writing the whole thing)
0

#18 User is offline   reek 

  • GMC Member
  • Group: GMC Member
  • Posts: 53
  • Joined: 27-September 08

Posted 09 March 2010 - 06:44 PM

Thanks for the great script. I added a check for the middle button and also the mouse_wheel_up() and mouse_wheel_down():

/*
Syntax:mouse_get_click(x1,y1,x2,y2)

Returns:
0 if nothing
1 if hover
2 if left clicked
3 if right clicked
4 if middle button (scroll wheel) clicked
5 if wheel up
6 if wheel down

argument0 = x1
argument1 = y1
argument2 = x2
argument3 = y2
*/

if(mouse_x>argument0 && mouse_x<argument1 && mouse_y>argument2 && mouse_y<argument3)
{
if mouse_wheel_up() {return 5;break;}
if mouse_wheel_down() {return 6;break;}
switch(mouse_button)
{
case(mb_left):
return 2;
break;
case(mb_right):
return 3;
break;
case(mb_middle):
return 4;
break;
case(mb_none):
return 1;
break;
}
}
return 0;
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users