Check if mouse is clicking on/hovering over a hotspot and/or object

Started by EnoRed, Fri 16/04/2010 21:50:54

Previous topic - Next topic

EnoRed

I was wondering what code I would need to use to check if the mouse cursor is either clicking on or is in the vicinity of a hotspot, object, character and anything else that isn't a walkable surface (or is completely empty). I'm trying to create a two-click interface from the default template where the right click will simply look at an object or hotspot and the left click will perform all the main mouse modes all in one, depending on what the player clicks on. Here's the code I have so far:

Quotefunction on_mouse_click(MouseButton button) {
  if (IsGamePaused() == 1) {
    // Game is paused, so do nothing (ie. don't allow mouse click)
  }
else if (button == eMouseLeft){
    ProcessClick(mouse.x, mouse.y, eModeWalkto );
  }
  else if (button == eMouseRight){
    ProcessClick(mouse.x, mouse.y, eModeLookat);
}

Right click works fine obviously, since it's pretty simple, however left clicking can only perform the WalkTo function. I want it so that the WalkTo function will be the default for if the player only clicks on walkable and/or empty areas, but it will react in whatever way I want it to in the individual room's script towards individual objects, characters and hotspots. Being pretty new at this (I've only just learnt most of the basics...) I'm sure this is pretty simple, but then that's why I'm posting in the first place.

Alternatively, if someone can link me to a template for a two-click interface that works (I've only found ones for older AGS versions) then that might be a nice shortcut, but I'd prefer an answer to this question instead if that's possible. Thanks in advance.

Ghost

GetLocationType should serve you well, you can read it up  here. Basically it returns the object type the user clicked on. Depending on that, you can implement any functionality you want- like always calling the "talk to" action when the user clicks on a character. Most two-click interfaces I know use this method, and many SCUMM-types too.

A simple methods would be something like this:

if (GetLocationType(mouse.x, mouse.x) == eLocationHotspot)
{
 ProcessClick(mouse.x, mouse.y, eModeInteract);
}

Note that with some additional scripting you can easily store whatever the user clicked on in a matching temp variable, and call the objects' own scripts instead of using ProcessClick. That's already a bit advanced though, and won't give you many advantages  :-\

Dualnames

Quote from: EnoRed on Fri 16/04/2010 21:50:54
Alternatively, if someone can link me to a template for a two-click interface that works (I've only found ones for older AGS versions) then that might be a nice shortcut, but I'd prefer an answer to this question instead if that's possible. Thanks in advance.

Well, there is : Control Modes Module.
Worked on Strangeland, Primordia, Hob's Barrow, The Cat Lady, Mage's Initiation, Until I Have You, Downfall, Hunie Pop, and every game in the Wadjet Eye Games catalogue (porting)

Khris

I'd do this:

Code: ags
function on_mouse_click(MouseButton button) {
  if (IsGamePaused() == 1) {
    // Game is paused, so do nothing (ie. don't allow mouse click)
  }
  else if (button == eMouseLeft){
    if (GetLocationType(mouse.x, mouse.y) != eLocationNothing)
      ProcessClick(mouse.x, mouse.y, eModeInteract);
    else
      ProcessClick(mouse.x, mouse.y, eModeWalkto);
  }
  else if (button == eMouseRight){
    ProcessClick(mouse.x, mouse.y, eModeLookat);
}


Then handle all events using the interact with ... event.

EnoRed

Thanks a lot for your help guys. I've managed to get a relatively simple two-click interface made, with your help, and this has definitely helped me understand some elements of coding with AGS much better. I'm still taking a rather slapdash, trial and error approach to trying to get things right, but hopefully this sort of thing should become second nature once I learn more.

Ghost

Getting your hands dirty with some coding is the best way to learn. Glad you made it!

EnoRed

I'll take this moment to also ask about this too. This is how the function I've used looks in the global script:

Quotefunction on_mouse_click(MouseButton button) {
  if (IsGamePaused() == 1) {
    // Game is paused, so do nothing (ie. don't allow mouse click)
  }
  else if (button == eMouseLeft){
    if (GetLocationType(mouse.x, mouse.y) != eLocationNothing)
      ProcessClick(mouse.x, mouse.y, eModeInteract);
    else
      ProcessClick(mouse.x, mouse.y, eModeWalkto);
  }
  else if ((button == eMouseRight) && (mouse.Mode != eModeUseinv)){
    ProcessClick(mouse.x, mouse.y, eModeLookat);
}
  else if ((button == eMouseRight) && (mouse.Mode == eModeUseinv)){
    mouse.Mode=eModeWalkto;}
}

I added in the part at the bottom, which I've also italicized. Basically, if the player doesn't have an inventory item selected, and thus the cursor is in the default, all-purpose mode, then the player will look when clicking the left mouse button. However, if they do have an inventory item, it will instead change the cursor back into the default (in this case simply "Walkto").

This works fine, however it doesn't work this way in the inventory screen; whenever I right click there, I can only look at the objects. As in if I select an item then right click, or I try to select a different item instead, nothing happens and I'm stuck with the item until I leave the inventory screen and right click. How do I make the right clicking to remove the item function work inside the inventory screen?

SMF spam blocked by CleanTalk