conditional statements under on_mouse_click function [SOLVED!]

Started by jfarwyke, Tue 05/05/2009 06:41:08

Previous topic - Next topic

jfarwyke

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
Code: ags
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:
Code: ags
      if (mouse.Mode==eModeTalkto) 
  player.FaceLocation(mouse.x, mouse.y);
  if (mouse.Mode==eModeTalkto)
      cPup.Say(" "); 

into
Code: ags
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.

Trent R

Code: ags
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
To give back to the AGS community, I can get you free, full versions of commercial software. Recently, Paint Shop Pro X, and eXPert PDF Pro 6. Please PM me for details.


Current Project: The Wanderer
On Hold: Hero of the Rune

GarageGothic

I think this line

Code: ags
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:

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


and see if it helps.

jfarwyke

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.

GarageGothic

#4
I'd suggest nesting the code like this:


Code: ags
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.

Hudders

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. ;)

jfarwyke

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.

GarageGothic

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.

SMF spam blocked by CleanTalk