Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: jfarwyke on Tue 05/05/2009 06:41:08

Title: conditional statements under on_mouse_click function [SOLVED!]
Post by: jfarwyke on Tue 05/05/2009 06:41:08
Me again with another problem...
I have these two mouse modes that I want to animate views when clicked. The Talkto refers to the speaking view of course, and the Stare refers to the Thinking view. I only want the thinking view to show when I click on objects or characters
function on_mouse_click(MouseButton button) {
  // called when a mouse button is clicked. button is either LEFT or RIGHT
  if (IsGamePaused() == 1) {
    // Game is paused, so do nothing (ie. don't allow mouse click)
  }
  else if (button == eMouseLeft) {
    ProcessClick(mouse.x, mouse.y, mouse.Mode );
      if (mouse.Mode==eModeTalkto)
  player.FaceLocation(mouse.x, mouse.y);
  if (mouse.Mode==eModeTalkto)
      cPup.Say(" ");
        if ((mouse.Mode==eModeStare)&&(GetLocationType(mouse.x, mouse.y) == eLocationCharacter)||
      (GetLocationType(mouse.x, mouse.y) == eLocationObject)) {
  cPup.FaceLocation(mouse.x, mouse.y);
  cPup.Think("               ");
      ate -= 1; 
        if (ate == 0) {
      btnIconStare.Enabled = false;
        mouse.SelectNextMode();
   }
   }
        }

The problem is that every left click on an object/character seems to initiate the eModeStare View whether that mode is selected or not. I know the problem is with my code, as I'm still trying to get the hang of conditional statements and haven't quite got it yet as you can see. I've worked over this for days and days now. Also, if I condense the two ifs:
      if (mouse.Mode==eModeTalkto)
  player.FaceLocation(mouse.x, mouse.y);
  if (mouse.Mode==eModeTalkto)
      cPup.Say(" ");

into
if (mouse.Mode==eModeTalkto) {
player.FaceLocation(mouse.x, mouse.y);
cPup.Say(" ");

it doesn't seem to run the eModeStare script at all.

Can someone tell me what I'm doing wrong?
Thanks in advance.
Title: Re: conditional statements under on_mouse_click function
Post by: Trent R on Tue 05/05/2009 07:35:41
function on_mouse_click(MouseButton button) {
  // called when a mouse button is clicked. button is either LEFT or RIGHT
  if (IsGamePaused() == 1) {
    // Game is paused, so do nothing (ie. don't allow mouse click)
  }
 
  if (button == eMouseLeft) {
    ProcessClick(mouse.x, mouse.y, mouse.Mode );
    if (mouse.Mode==eModeTalkto)   player.FaceLocation(mouse.x, mouse.y);
    if (mouse.Mode==eModeTalkto)   cPup.Say(" ");
    if ((mouse.Mode==eModeStare)&&(GetLocationType(mouse.x, mouse.y) == eLocationCharacter)||
      (GetLocationType(mouse.x, mouse.y) == eLocationObject)) {
      cPup.FaceLocation(mouse.x, mouse.y);
      cPup.Think("               ");
      ate -= 1; 
      if (ate == 0) {
        btnIconStare.Enabled = false;
        mouse.SelectNextMode();
      }
    }
  }
}


Haven't changed anything except for the spacing. That will make your code a lot easier to read, and therefore debug. Going to bed right now, so I'll edit this post tomorrow with my input.

~Trent
Title: Re: conditional statements under on_mouse_click function
Post by: GarageGothic on Tue 05/05/2009 13:05:46
I think this line

if ((mouse.Mode==eModeStare) && (GetLocationType(mouse.x, mouse.y) == eLocationCharacter) || (GetLocationType(mouse.x, mouse.y) == eLocationObject)) {

could be problematic (will the OR pertain to the full previous statement or just the GetLocationType check?). Try adding a parenthesis like this:

if ((mouse.Mode==eModeStare) && ((GetLocationType(mouse.x, mouse.y) == eLocationCharacter) || (GetLocationType(mouse.x, mouse.y) == eLocationObject))) {

and see if it helps.
Title: Re: conditional statements under on_mouse_click function
Post by: jfarwyke on Tue 05/05/2009 14:23:52
Yes GarageGothic, the OR applies to the GetLocationType. Good call. The parenthesis worked perfectly. It seems that was the problem all along. And thanks Trent for the tip on cleaning up my code, it does make it easier to read. It was so messy because I did a lot of cutting and pasting.

Now as an addition, if I wanted to add an else statement that displays a message if the player uses the eModeStare on anything that isn't an object or character, basically a hotspot, would I place that at the very end of my code or would I place it after the GetLocationType statement? And would it even be an else, or an else if? Or would it just be another if, specifying eLocationHotspot? I'm pretty sure I've tried all these variations with poor results. Any suggestions?

Thanks a lot guys, you've saved my game again and educated me too.
Title: Re: conditional statements under on_mouse_click function
Post by: GarageGothic on Tue 05/05/2009 14:34:56
I'd suggest nesting the code like this:


if (button == eMouseLeft) {
    ProcessClick(mouse.x, mouse.y, mouse.Mode );
    if (mouse.Mode==eModeTalkto) {
      player.FaceLocation(mouse.x, mouse.y);
      cPup.Say(" ");
      }
    else if (mouse.Mode==eModeStare) {
      if ((GetLocationType(mouse.x, mouse.y) == eLocationCharacter) || (GetLocationType(mouse.x, mouse.y) == eLocationObject)) {
        cPup.FaceLocation(mouse.x, mouse.y);
        cPup.Think("               ");
        ate -= 1; 
        if (ate == 0) {
          btnIconStare.Enabled = false;
          mouse.SelectNextMode();
          }
        }
      else if (GetLocationType(mouse.x, mouse.y) == eLocationHotspot) {
        Display("You didn't click on a character or object");
        }
      }
    }


Edit: Fixed the indenting (I hope) and combined the two  "if (mouse.Mode==eModeTalkto)" statements.
Title: Re: conditional statements under on_mouse_click function
Post by: Hudders on Tue 05/05/2009 14:52:38
Quote from: jfarwyke on Tue 05/05/2009 14:23:52
Now as an addition, if I wanted to add an else statement that displays a message if the player uses the eModeStare on anything that isn't an object or character, basically a hotspot, would I place that at the very end of my code or would I place it after the GetLocationType statement? And would it even be an else, or an else if? Or would it just be another if, specifying eLocationHotspot? I'm pretty sure I've tried all these variations with poor results. Any suggestions?

If it were an "else", it would be activated no matter what you were clicking on, (even if you were clicking on nothing whatsoever), so long as it wasn't already covered in the if statement(s). As GarageGothic posted above, you should use an "else if" to cover the statement.

Just wanted to clarify that since you seemed to be confused by the difference. If I'm wrong, ignore me. ;)
Title: Re: conditional statements under on_mouse_click function
Post by: jfarwyke on Tue 05/05/2009 15:13:32
To GarageGothic, when I tried your code the Speaking View didn't work, so I changed the first else if to an if and it worked like a charm, thanks a lot.

To Hudders, you're absolutely right. I'm actually confused by a great deal. Perhaps I'd better go through the scripting tutorials again.

Thanks for the help guys and the fast responses. Now the only thing I have to figure out is how to add the 'SOLVED' to the subject header.
Title: Re: conditional statements under on_mouse_click function
Post by: GarageGothic on Tue 05/05/2009 15:26:31
Quote from: jfarwyke on Tue 05/05/2009 15:13:32when I tried your code the Speaking View didn't work, so I changed the first else if to an if and it worked like a charm

I think that was before I revised the code - I had overlooked that the two first "if (mouse.Mode)" statements both pertained to the eModeTalkto, so the second one (with "else if") never registered as true. The current version should work though.