Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Crimmy on Mon 29/09/2014 14:09:31

Title: Opening GUI on held mouse click?
Post by: Crimmy on Mon 29/09/2014 14:09:31

Hey guys. I'm coding for my game, and at the suggestion of Babar I'm simplifying the control scheme :P
I'm trying to figure out how to set it so that holding down the left mouse click opens up a GUI verbcoin (which has the options Look and Interact)

This is what I've got, under the 'function on_mouse_click(MouseButton button) {' command (I've commented out the original eMouseLeft script)

Code (ags) Select

else if (mouse.IsButtonDown(eMouseLeft)){
    SetTimer(1, 40);
    if (IsTimerExpired(1)) {
      gVerbcoin.Visible = true;
      mouse.Mode = eModeInteract;
      mouse.UseModeGraphic(eModePointer);
    }
    else {
       SetTimer(1, 0);
       ProcessClick(mouse.x, mouse.y, eModeWalkto);
    }
  }

And I've written '  int timer=0;' at the top of the global script to set the value prior.

At the moment it just goes straight to the else command. I haven't got timer commands to work yet, so it's very likely that.
Title: Re: Opening GUI on held mouse click?
Post by: Intense Degree on Mon 29/09/2014 15:30:40
The problem with that code is that you only check whether the timer is expired once, straight after setting it and therefore it will never have expired.

You need to perform the check in the repeatedly_execute function in global script to ensure you check whether the timer has expired every game loop, rather than just once.
Title: Re: Opening GUI on held mouse click?
Post by: Khris on Mon 29/09/2014 22:23:13
You're approaching this from the wrong angle.
With stuff like that, it is helpful to list what is supposed to happen when, and how to detect it.

on_mouse_click is not suitable for this, you need to use mouse.IsButtonDown() in repeatedly_execute().

Code (ags) Select
int click_x, click_y;
bool left_mouse_was_pressed;
int verbcoin_frame_delay = 10, verbcoin_frame_count;

  // this inside repeatedly_execute
  if (mouse.IsButtonDown(eMouseLeft)) {
    if (!left_mouse_was_pressed) {
      // user has just started pressing down left mouse button
      verbcoin_frame_count = 0;
      // store click's room coordinates
      click_x = mouse.x + GetViewportX();
      click_y = mouse.y + GetViewportY();
    }
    else {
      // user is holding down button
      verbcoin_frame_count++;
      if (verbcoin_frame_count == verbcoin_frame_delay) {
        // user has been pressing left mouse button for 10 frames
        // show verbcoin, but only if click was started over active location
        if (GetLocationType(click_x - GetViewportX(), click_y - GetViewportY()) != eLocationNothing)
          gVerbCoin.Visible = true;
      }
    }
  }
  else {
    if (left_mouse_was_pressed) {
      // user has just let go of left mouse button
      // possibly other code here, or on_event/eEventGUIMouseUp takes over
    }
  }
  left_mouse_was_pressed = mouse.IsButtonDown(eMouseLeft); // store current state for next frame
}


Not tested, might not work at all.
Title: Re: Opening GUI on held mouse click?
Post by: Crimmy on Tue 30/09/2014 14:58:43
Hm. It works at first (I put a cEgo.Say command under the '!left_mouse_was_pressed'), but the verbcoin won't open and the command for when the left mouse button is released isn't working yet.
I think the verbcoin won't open because I may not have an 'active area' - I'm holding it down on a character and it doesn't work (maybe it refers to something else?).
The final else, for removing the left mouse button, I'm pretty unsure about. :|

Thanks so far for the help, this has been a lot more work than I anticipated
edit: (Obviously Khris' work)