"It is illegal to fall out of *choice statement; you must *goto or *finish before the end of the indented block"

I know this issue has been comented in a lot of posts around here, but I still can’t have my through this error. I’ve encountered it a few times before and managed to get away, but now I can’t fix it and I don’t know what is causing it.

Today, I thought of develop a combat system for my Middle Ages era game, instead of writing the story. I created a new project on Choicescript IDE, so I could code it without Fing up something in the main code.

First, I created a simple system, based on a *rand command, and that worked. Second, I put some variables to it, like strength and agility to determine how well the character could swing an attack, or block one from the enemy. That also worked, then I went to the final phase which would be the implementation of weapon types. That worked… kinda, until it didn’t. A few bugs appeared out of nowhere and then I got this *choice bug, that I can’t fix.

As far as I know, there isn’t any *if without a *goto in the end. Now I don’t know what to do, please help.

Here’s a sample of the “bugged” code:

*choice
#swerve
*if agility <=1
*rand die 0 100
*if die <=35
*set defense 0
*goto defenseshortsword0
*else
*set defense 1
*goto defenseshortsword2
*elseif agility =2
*rand die 0 100
*if die <=30
*set defense 0
*goto defenseshortsword0
*else
*set defense 1
*goto defenseshortsword2
*elseif agility <=4
*rand die 0 100
*if die <=25
*set defense 0
*goto defenseshortsword0
*else
*set defense 1
*goto defenseshortsword2
#block
*if strength <= 1
*rand die 0 100
*if die <=85
*set defense 0
*goto defenseshortsword0
*else
*set defense 1
*goto defenseshortsword2
*if strength =2
*rand die 0 100
*if die <=75
*set defense 0
*goto defenseshortsword0
*else
*set defense 1
*goto defenseshortsword2
*if strength <=4
*rand die 0 100
*if die <=70
*set defense 0
*goto defenseshortsword0
*else
*set defense 1
*goto defenseshortsword2

If you can show us the indents, it would help us to see what your code is doing. Use the below tool to format your code:

image

You can see these tools by hitting the edit button (the pencil looking icon) below your post.

4 Likes

Ooh, here’s the issue, I bet. Should that be “>=”?

I believe there’s no provision for agility being > 4, so the code is falling out without a *goto.

And I also think there’s no final *else in that sequence–it goes *if agility <= 1; *elseif agility =2; *elseif agility <= 4, and that’s it. But it’s hard to be wholly sure without seeing the proper indentation.

2 Likes

I’d recommend using a *fake_choice statement instead of a *choice, but I don’t know if that would hinder your chances at learning how to correctly use choices

Oh, forget it. @Gower already beat me at it

1 Like

I believe I once saw the code of a game, and the author didn’t use an *else at the end of their if/elseif/else blocks, and it didn’t threw any error

It could be me imagining things tho

*choice
    #swerve
        *if agility <= 1
            *rand die 0 100
            *if die <= 35
                *set defense 0
                *goto defenseshortsword0
            *else
                *set defense 1
                *goto defenseshortsword2
        *if agility = 2
            *rand die 0 100
            *if die <= 30
                *set defense 0
                *goto defenseshortsword0
            *else
                *set defense 1
                *goto defenseshortsword2
        *if agility >= 4
            *rand die 0 100
            *if die <= 25
                *set defense 0
                *goto defenseshortsword0
            *else
                *set defense 1
                *goto defenseshortsword2
    #block
        *if strength <= 1
            *rand die 0 100
            *if die <= 85
                *set defense 0
                *goto defenseshortsword0
            *else
                *set defense 1
                *goto defenseshortsword2
        *if strength = 2
            *rand die 0 100
            *if die <= 75
                *set defense 0
                *goto defenseshortsword0
            *else
                *set defense 1
                *goto defenseshortsword2
        *if strength >= 4
            *rand die 0 100
            *if die <= 70
                *set defense 0
                *goto defenseshortsword0
            *else
                *set defense 1
                *goto defenseshortsword2

This should work, feel free to copy and paste it. Let me know if you are still having issues. Also, there is no need to use the *elseif command if there are no overlapping variables, the *if command will work just fine.

Hmm… Now that I look at it, what happens if strength or agility are equal to 3? Wouldn’t that throw an error?

Could work, but each different result leads to a different *label. I can try to make a single label to make it work.

Problem is, code goes up all the way throughout to 10, and that already has a code >=10. I just put a small version of it.

I’m copy pasting (with proper indentation) of the full thingy.

*choice
    #swerve
        *if agility <=1
            *rand die 0 100
            *if die <=35
                *set defense 0
                *goto defenseshortsword0
            *else
                *set defense 1
                *goto defenseshortsword2
            *elseif agility =2
            *rand die 0 100
            *if die <=30
                *set defense 0
                *goto defenseshortsword0
            *else
                *set defense 1
                *goto defenseshortsword2
            *elseif agility <=4
            *rand die 0 100
            *if die <=25
                *set defense 0
                *goto defenseshortsword0
            *else
                *set defense 1
                *goto defenseshortsword2
            *elseif agility <=6
            *rand die 0 100
            *if die <=15
                *set defense 0
                *goto defenseshortsword0
            *else
                *set defense 1
                *goto defenseshortsword2            
            *elseif agility <=8
            *rand die 0 100
            *if die <=10
                *set defense 0
                *goto defenseshortsword0
            *else
                *set defense 1
                *goto defenseshortsword2
            *elseif agility =9
            *rand die 0 100
            *if die <=5
                *set defense 0
                *goto defenseshortsword0
            *else
                *set defense 1
                *goto defenseshortsword2
            *elseif agility >=10
            *rand die 0 100
            *if die <2
                *set defense 0
                *goto defenseshortsword0
            *else
                *set defense 1
                *goto defenseshortsword2
    #block
            *if strength <= 1
            *rand die 0 100
            *if die <=85
                *set defense 0
                *goto defenseshortsword0
            *else
                *set defense 1
                *goto defenseshortsword2
            *if strength =2
            *rand die 0 100
            *if die <=75
                *set defense 0
                *goto defenseshortsword0            
            *else
                *set defense 1
                *goto defenseshortsword2
            *if strength <=4
            *rand die 0 100
            *if die <=70
                *set defense 0
                *goto defenseshortsword0
            *else
                *set defense 1
                *goto defenseshortsword2
            *if strength <=6
            *rand die 0 100
            *if die <=60
                *set defense 0
                *goto defenseshortsword0
            *else
                *set defense 1
                *goto defenseshortsword2
            *if strength <=8  
            *rand die 0 100
            *if die <=55
                *set defense 0
                *goto defenseshortsword0
            *else
                *set defense 1
                *goto defenseshortsword2
            *if strength =9
            *rand die 0 100
            *if die <=50
                *set defense 0
                *goto defenseshortsword0
            *else
                *set defense 1
                *goto defenseshortsword2
        *if strength >=10
            *rand die 0 100
            *if die <=45
                *set defense 0
                *goto defenseshortsword0
            *else
                *set defense 1
                *goto defenseshortsword2

If agility is not less than or equal to 1 in that first *if, there is nowhere for the code to go–no *goto.

If you draw a line down from *if agility <=1, it goes down to the #block section. Are you sure that *elseif agility = 2 shouldn’t be dedented?

1 Like

It looks like I was wrong in my initial diagnosis. @Gower is right, the *elseif indentation is most likely the only cause. From what I can see, there is no need to change anything else. I also don’t see how your agility or strength could result in an error, it would just go to *elseif agility <= 4. That being said, make sure there is a space between the equal sign and the number.

1 Like

In addition to the dedenting, a final *else is needed. You cannot end in *elseif.

So, I’ve just tested a little snippet of code, and there’s not an error if you don’t put an *else at the end of an if/elseif/else block.

For example:

*create var 0
*rand var 1 4

*if (var = 1)
  *goto reeves
*elseif (var = 2)
  *goto pitt
*elseif (var = 3)
  *goto affleck

*label reeves
John Wick is my favorite movie!
*goto yea

*label pitt
Do you know Joe Black?
*goto yea

*label affleck
Christian Bale was a better Batman
*goto nay

*label yea
You won!
*ending

*label nay
You lost!
*ending

In that piece of code, CS would go to *label reeves if *rand var resulted in 4, but you could fix that putting the “affleck” label at the top of the if/elseif block

I know that this use of *ifs and *elseifs could lead to bugs, but if you code it in the right way, I don’t see why you need an *else

This is just the opinion of a lazy coder, though :stuck_out_tongue:

Useless Trivia

Devon Connell used if/elseif like this in SoH 1

Let me amend my statement then:

You best not let me catch you ending in *elseif! Not on my watch!

1 Like

Hello everyone.

It worked!

I put a *fake_choice and a common *goto label at the end of every line and it appears to have fixed.

*label defensespear
*fake_choice
    #swerve
        *if agility <=1
            *rand die 0 100
            *if die <=45
                *set defense 0
                *goto defensespear1
            *else
                *set defense 1
                *goto defensespear1
        *if agility =2
            *rand die 0 100
            *if die <=35
                *set defense 0
                *goto defensespear1
            *else
                *set defense 1
                *goto defensespear1
        *if agility <=4
            *rand die 0 100
            *if die <=30
                *set defense 0
                *goto defensespear1
            *else
                *set defense 1
                *goto defensespear1
        *if agility <=6
            *rand die 0 100
            *if die <=20
                *set defense 0
                *goto defensespear1
            *else
                *set defense 1
                *goto defensespear1
        *if agility <=8
            *rand die 0 100
            *if die <=15
                *set defense 0
                *goto defensespear1
            *else
                *set defense 1
                *goto defensespear1
        *if agility =9
            *rand die 0 100
            *if die <=10
                *set defense 0
                *goto defensespear1
            *else
                *set defense 1
                *goto defensespear1
        *if agility >=10
            *rand die 0 100
            *if die <=7
                *set defense 0
                *goto defensespear1
            *else
                *set defense 1
                *goto defensespear1
    #block
        *if strength <=1
            *rand die 0 100
            *if die <=50
                *set defense 0
                *goto defensespear1
            *else
                *set defense 1
                *goto defensespear1
        *if strength =2
            *rand die 0 100
            *if die <=40
                *set defense 0
                *goto defensespear1
            *else
                *set defense 1
                *goto defensespear1
        *if strength <=4
            *rand die 0 100
            *if die <=35
                *set defense 0
                *goto defensespear1
            *else
                *set defense 1
                *goto defensespear1
        *if strength <=6
            *rand die 0 100
            *if die <=25
                *set defense 0
                *goto defensespear1
            *else
                *set defense 1
                *goto defensespear1
        *if strength <=8
            *rand die 0 100
            *if die <=20
                *set defense 0
                *goto defensespear1
            *else
                *set defense 1
                *goto defensespear1
        *if strength =9
            *rand die 0 100
            *if die <=15
                *set defense 0
                *goto defensespear1
            *else
                *set defense 1
                *goto defensespear1
        *if strength >=10
            *rand die 0 100
            *if die <=10
                *set defense 0
                *goto defensespear1
            *else
                *set defense 1
                *goto defensespear1
            
*label defensespear1
*if defense = 0
    You took damage.
    *set health -2
    *page_break
    *if health <=0
        *goto die
    *else
        *goto defensespear2
*else
    You took no damage.
    *page_break
    *goto spear

Thanks everyone for the help!
(forum don’t let me tag everyone in this conversation)

3 Likes

7 posts were split to a new topic: Falling out of a choice statement