Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Snake on Thu 28/05/2009 15:22:28

Title: (SOLVED) Trouble with detecting whether the mouse is over a particular object
Post by: Snake on Thu 28/05/2009 15:22:28
Hello once again,

I've a GUI that switches on while holding down the right mouse button.

This works with no problem. Displaying an integer within it's label works fine as well.

But, I want to detect when the ouse is over particular objects and come on, but I'm not getting anywhere.

Here's the code:

if (Mouse.IsButtonDown(eMouseRight)==true){
 if (Object.GetAtScreenXY(mouse.x, mouse.y)==object[20]){
   GuyDescr.Text=String.Format("%d",cputankhp);
   gfloater.X=mouse.x;
   gfloater.Y=mouse.y-20;
   gfloater.Visible=true;
   }
 else {}
}


I've also tried GetLocationType but that isn't working for me as well.

What am I missing to include? Or am I even heading in the right direction?

Thanks in advance,

--Snake
Title: Re: Bit of trouble with detecting whether the mouse is over a particular object...
Post by: Khris on Thu 28/05/2009 15:54:25
What doesn't work?

Try:
  if (Mouse.IsButtonDown(eMouseRight)) {
    Object*o = Object.GetAtScreenXY(mouse.x, mouse.y);
    if (o != null) Display("%d", o.ID);
  }

This should display the ID of the object under the mouse.
Title: Re: Bit of trouble with detecting whether the mouse is over a particular object...
Post by: Snake on Thu 28/05/2009 17:21:45
Hmm, that's not displaying anything either... I wonder what's going on?


Sorry it took so long to reply. I had taken little Sampson Hinkleberry (my daughter, Sam) to Story Hour at the library.

//--EDIT--//

Wait a minute... I think I'm on to something...
Title: Re: Bit of trouble with detecting whether the mouse is over a particular object...
Post by: Snake on Thu 28/05/2009 17:46:33
I think I've found the problem but I don't know how to solve it.

It's definately not the code. It has something to do with the objects.

The code works for objects 1-10 but not for objetcs 11-20. There's no difference between any of these objects in the properties tab, since, I was thinking maybe it was something small like making them not clickable. But they are all the same.

Any ideas of what this could be?
Title: Re: Bit of trouble with detecting whether the mouse is over a particular object...
Post by: Khris on Thu 28/05/2009 17:48:56
Them being not clickable will indeed prevent them from being registered by Object.GetAtScreen.XY (which has its advantages but can also be confusing).
I'm not sure whether you said they're non-clickable but I can't think of any other explanation at the moment.
Title: Re: Bit of trouble with detecting whether the mouse is over a particular object...
Post by: GarageGothic on Thu 28/05/2009 18:00:48
Yes, I would also guess it's the Clickable property that is playing tricks on you. As a workaround, you could do something like this:

bool objclick[];
objclick = new bool[Room.ObjectCount];
int objcount;
while (objcount < Room.ObjectCount) {
 objclick[objcount] = object[objcount].Clickable;
 object[objcount].Clickable = true;
 objcount++;
 }

//PUT YOUR CODE HERE
//THEN, AFTER YOUR SCRIPT HAS FINISHED:

while (objcount > 0) {
 objcount--;
 object[objcount].Clickable = objclick[objcount];
 }


Unless the Clickable status of the object changes while in a room, you could optimize a bit by declaring the "bool objclick[];" array outside the function and store the Clickable status during the player enters room event. But I doubt you would experience any real slowdown from using my script, so it's not really worth the effort in my opinion.
Title: solved
Post by: Snake on Thu 28/05/2009 18:13:16
Heh - I knew it had to do with them being clickable. When I started making this game, which wasn't actually ever going to be anything (but of course it is now), for what I needed to happen I had to declare objects 11-20 unclickable in the Room_Load event.

I had forgotten about having to do this, or even why.

Problem solved, anyway. All I did was while the right mouse button was being held down, the objects would be clickable, and not when the button was released.

Works splendedly now, guys, thanks for putting up with my foolishness ;)