Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Priabudiman on Fri 03/07/2015 16:30:25

Title: [SOLVED] How many "Else" can you use?
Post by: Priabudiman on Fri 03/07/2015 16:30:25
im working on this condition with no avail :

Code (ags) Select
function repeatedly_execute()
{
    if(IsTimerExpired(1)) // ... it's time to decrease the health
  {
    ChangeHealth(-5);
    if(GetHealth() > 0)  // ... we're still alive
    {
      SetTimer(1,120);  // just start counting again
    }
    else  // player has died
    {
      Display("You Died...");
   
    else
  {
   (GetHealth()< 20)
    Display("ouch");
  }


This one error and gave me parse error.

I also tried this one :

Code (ags) Select
function repeatedly_execute()
{
    if(IsTimerExpired(1)) // ... it's time to decrease the health
  {
    ChangeHealth(-5);
    if(GetHealth() > 0)  // ... we're still alive
    {
      SetTimer(1,120);  // just start counting again
    }
    else  // player has died
    {
      Display("You Died...");
   
    if (GetHealth()< 20)
  {
    Display("ouch");
  }


This one is running the game but the action didn't run.

what i want is, i want to create an action where the player's health is at 20, he say something.
Title: Re: How many "Else" can you use?
Post by: Gilbert on Fri 03/07/2015 16:34:53
Quickly skimming the codes, seems that there is a missing } at line 13.
Title: Re: How many "Else" can you use?
Post by: Priabudiman on Fri 03/07/2015 16:36:32
It seems that the error is in the if statement placement.

I change the last statement placement to be in the first line and it works fine, but will it repeated again when the game restarted? or should i save it somehow so it will always react like that when player health is 20?

function repeatedly_execute()
{
    if(IsTimerExpired(1)) // ... it's time to decrease the health
  {
    ChangeHealth(-5);
   
if (GetHealth()== 20) // Player Health at exatly 20
  {
    Display("ouch");
  }   
   
    if(GetHealth() > 0)  // ... we're still alive
    {
      SetTimer(1,120);  // just start counting again
    }
    else  // player has died
    {
      Display("You Died...");

Title: Re: How many "Else" can you use?
Post by: Billbis on Fri 03/07/2015 17:17:36
So many missing "}" it hurts my eyes. :-D
Do close each one you open.

(http://imgs.xkcd.com/comics/(.png) (https://xkcd.com/859/)
Title: Re: How many "Else" can you use?
Post by: Crimson Wizard on Fri 03/07/2015 17:21:33
Also, try to use indentation. That helps immensly at later stages.

What is indentation: it is when you keep things related to same "block" of code at the same margin:

It lets you see where your function/loop/if/else blocks start and where they end pretty easily.
You will also see which commands are inside the particular "if" and which are not at the very first glance:

Code (ags) Select

function repeatedly_execute()
{
    if(IsTimerExpired(1)) // ... it's time to decrease the health
    {
        ChangeHealth(-5);
        if (GetHealth()== 20) // Player Health at exatly 20
        {
            Display("ouch");
        }   
   
        if(GetHealth() > 0)  // ... we're still alive
        {
            SetTimer(1,120);  // just start counting again
        }
        else  // player has died
        {
            Display("You Died...");
Title: Re: How many "Else" can you use?
Post by: ollj on Fri 03/07/2015 17:26:22
you can always use a binary-tree-case structure for speed.


int case;

if (case&4){//4

if (case&2){ //2 

if (case & 1){//1  //case==7
}else{//!1  //case==6
}

}else{//!2

if (case & 1){//1  //case==5
}else{//!1  //case==4
}

}

}else{//!4 

if (case&2){ //2

if (case & 1){//1  //case==3
}else{//!1  //case==2
}

}else{//!2 

if (case & 1){//1  //case==1
}else{//!1    //case==0
}

}

//and this structure can be doubled recursively, but its nearly impossible to find a missing { or } in a tree with >64 branches.
Title: Re: How many "Else" can you use?
Post by: Cassiebsg on Fri 03/07/2015 17:27:46
also, remember that you can also use as many "else if" as you need, between the first IF and the last ELSE...
Title: Re: How many "Else" can you use?
Post by: Priabudiman on Fri 03/07/2015 17:30:18
Quote from: Billbis on Fri 03/07/2015 17:17:36
So many missing "}" it hurts my eyes. :-D
Do close each one you open.

(http://imgs.xkcd.com/comics/(.png) (https://xkcd.com/859/)

Right away Sir! :D

And thank you all for the explanations!

@crimsonwizard, : ill make sure that i will do that next when i check and edit my codes! that is really helpful, i can understand that one, haha.. for my own good!

@cassiebsg : ill open the good book of F1 right away.. maybe i will need a bit of time to understand that one, hopefully it simple enough :D
Title: Re: How many "Else" can you use?
Post by: Crimson Wizard on Fri 03/07/2015 17:32:41
Quote from: ollj on Fri 03/07/2015 17:26:22
you can always use a binary-tree-case structure for speed.

ollj, please, try to understand the question better before posting?
What you posted above is barely related to question. I am not even sure it may be applicable in the case (where the conditions check  different values).
Last but not least, the question was asked by a scripting newbie, who probably will be confused by the technique you are suggesting.

(I doubt I would use something like that myself... it has a low human-readability)
Title: Re: [SOLVED] How many "Else" can you use?
Post by: selmiak on Fri 03/07/2015 18:37:38
Quote from: Cassiebsg on Fri 03/07/2015 17:27:46
also, remember that you can also use as many "else if" as you need, between the first IF and the last ELSE...

absolutely, you can use else only once, otherwise you need else if (condition)
you could put another if /else into the else, but that is bad practise as you should use the condition for that if in the else if clause.

(btw, XKCD is cool, there is always some truth to it!