[SOLVED] How many "Else" can you use?

Started by Priabudiman, Fri 03/07/2015 16:30:25

Previous topic - Next topic

Priabudiman

im working on this condition with no avail :

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

Gilbert

Quickly skimming the codes, seems that there is a missing } at line 13.

Priabudiman

#2
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?

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


Billbis

#3
So many missing "}" it hurts my eyes. :-D
Do close each one you open.


Crimson Wizard

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

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

ollj

#5
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.

Cassiebsg

also, remember that you can also use as many "else if" as you need, between the first IF and the last ELSE...
There are those who believe that life here began out there...

Priabudiman

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.



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

Crimson Wizard

#8
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)

selmiak

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!

SMF spam blocked by CleanTalk