Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: slufan on Fri 22/11/2019 16:51:29

Title: Problem with If and else [SOLVED]
Post by: slufan on Fri 22/11/2019 16:51:29
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) Select

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) Select

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) Select

else
  Unhandled();

and:
Code (ags) Select

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
Title: Re: Problem with If and else
Post by: Crimson Wizard on Fri 22/11/2019 17:14:05
Hey.
Your mistake is that you begin every condition with plain "if":

Code (ags) Select

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) Select

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.
Title: Re: Problem with If and else
Post by: eri0o on Fri 22/11/2019 17:19:21
Also, on guiscript.asc, those lonely else without it's own if should not even compile.
Title: Re: Problem with If and else
Post by: slufan on Sat 23/11/2019 09:53:42
Ok, thats it... thank you Crimson Wizard and eri0o.