Problem with If and else [SOLVED]

Started by slufan, Fri 22/11/2019 16:51:29

Previous topic - Next topic

slufan

Hello (once again)

I'm having any problems with the "If" and "else" statements. I think I'm not using them correctly and I'm going mad. I have this codes:

Code in GlobalScript.asc
Code: ags
 
function cMiniSpider_AnyClick()
{ 
  // WALK TO
  if (UsedAction(eMA_WalkTo)) {
    Go();
  }
  // TALK TO
  else if (UsedAction(eGA_TalkTo)) {
    if (cSpider.Room == 30){
      cSpider.Say("Hola.");
      cMiniSpider.Say("Hola, señor.");
      cSpider.Say("Soy una araña.");
      cMiniSpider.Say("Lo se, yo tambien pero mas pequeña.");
    }
  
    if (cSpider.Room == 40){
      cSpider.Say("¿Sabes por donde hay que seguir? ");
      cMiniSpider.Say("No.");
    }
  
    if (cSpider.Room == 90){
      cSpider.Say("¿Te resulta familiar este lago?");
      cMiniSpider.Say("Si, ya estamos muy cerca.");
    }

    else
      Unhandled();
  }
}


Code in guiscript.asc
Code: ags

function Unhandled(int door_script) {
  // unhandled USE INV
  else if (UsedAction(eGA_UseInv)) player.Say("No puedo usarlo con eso.");   

  // unhandled TALK TO
    else if (UsedAction(eGA_TalkTo)) {
      if (type==2) player.Say("No tengo nada que decirle ahora mismo.");
      else player.Say("No tengo nada que decir.");
    }
}


The problem is that when I start talking everything is going fine, but always ends with "No tengo nada que decirle ahora mismo.", no matter in what room are the character. I have try:
Code: ags

else
  Unhandled();

and:
Code: ags

else {
  Unhandled();
{


with the same result, says what he has to say, but alwasy ends with "No tengo nada que decirle ahora mismo."

Any idea?

Thanks in advance

Crimson Wizard

#1
Hey.
Your mistake is that you begin every condition with plain "if":

Code: ags

if (cSpider.Room == 30){
<...>
if (cSpider.Room == 40){
<...>
if (cSpider.Room == 90){
<...>
else
<...>


In such case every "if" is checked separately, and "else" only pairs with the last "if". The logic goes like this:
- check if it's room 30, and if yes then do something;
- check if it's room 40, and if yes then do something;
- check if it's room 90, if yes do one thing, if not do another.
So for any room except 90 you will be calling Unhandled().

What you need is to use "if/else if/else" combination instead:
Code: ags

if (cSpider.Room == 30){
<...>
else if (cSpider.Room == 40){
<...>
else if (cSpider.Room == 90){
<...>
else

Here all those conditions are bundled and checked as a single group, so it's always only one of them that matches.

eri0o

Also, on guiscript.asc, those lonely else without it's own if should not even compile.

slufan

Ok, thats it... thank you Crimson Wizard and eri0o.

SMF spam blocked by CleanTalk