Else statement shouldn't be executed, is executed anyway

Started by Georgeqgreg, Thu 29/03/2012 16:09:59

Previous topic - Next topic

Georgeqgreg

There's got to be something really stupid here that I'm doing, or maybe I'm just bad at looking at things. (Please don't be TOO hard on me!) But whatever it is, I've done something wrong, and now my code is doing something I don't want it to do.

Anyway, to be more specific, I am using the 9verbs template to power my game. In accordance with it, I have a section of code that looks like this:

Code: ags
function hDoor_AnyClick()
{
if(UsedAction(eGA_LookAt)) {
StartCutscene(eSkipESCOnly);
player.FaceLocation(152, 65);
Wait(60);
player.FaceDirection(eDir_Down);
player.Say("I don't know why, but I think it's a door.");
EndCutscene();
}
if(UsedAction(eGA_Open)) {
  Unhandled();
}
if(UsedAction(eGA_Use)) {
  Unhandled();
}
else {
Unhandled();
}
} 


Yeah, I don't comment my code. My mindset is "Who besides me will ever look at it?" (Waitaminute...)

Anyway, what we have here is (supposed) to be as follows: When you look at the door, the character looks at the door, turns to the player and remarks on it, sarcastically. When you attempt to open it, right now it's unhandled, but that's a placeholder for when I put proper code for opening the door. Same goes for use. The else clause is for any miscellaneous action that I wouldn't expect you to do to a door. (Pick up? Give? Unlimited possibilities!)

Problem is, after remarking on the door when looking at it, the else statement is executed anyway, causing the character to remark, out-of-context "Nice door". Now, I'll just say that this little unhandled-case string will probably be replaced with some sort of error message in the final game. Part of my design philosophy is to always have a dialog for looking at anything! But there's my current headscratcher. Why exactly is the else statement being executed, and how can I stop it? As I've said, I'm probably stupid, but who knows? 

Khris

First of all, indent your code. That's invaluable anyway and even more so as long as you're making mistakes like this one.

Code: ags
function hDoor_AnyClick() {

  if(UsedAction(eGA_LookAt)) {
    StartCutscene(eSkipESCOnly);
    player.FaceLocation(152, 65);
    Wait(60);
    player.FaceDirection(eDir_Down);
    player.Say("I don't know why, but I think it's a door.");
    EndCutscene();
  }


  if(UsedAction(eGA_Open)) {
    Unhandled();
  }


  if(UsedAction(eGA_Use)) {
    Unhandled();
  }
  else {
    Unhandled();
  }
}


I have separated the discrete blocks by spaces.

What happens is, each of these three if-blocks is handled one after another, independently, and the third one checks for UsedAction(eGA_Use) which is false, then calls the code inside the else part.
To fix this you have to pull the three blocks together into a single one by putting an else directly before the 2nd and 3rd if.

Georgeqgreg

Ahh... thankyou. Just as I imagined. I'm an idiot and forgot the else statements. DOH! And yes, my code could stand to be better formatted. A LOT. I'm really bad with that. Sometimes the editor nicely indents the code for me, but sometimes it doesn't. Probably a PEBCAK there.  :D

SMF spam blocked by CleanTalk