# Introduction to ChoiceScript

A basic guide to the ChoiceScript programming language. Please post on the ChoiceScript google group if you have questions about this document.

## What is ChoiceScript?

ChoiceScript is a simple programming language for writing multiple-choice games (MCGs) like Choice of the Dragon. Writing games with ChoiceScript is easy and fun, even for authors with no programming experience.

## Trying it out

To begin, download the ChoiceScript source from GitHub, extract the zip file, and open the `web/index.html` file. The game will immediately begin.

(You can also use the ChoiceScript github page to browse our files, file bugs, or receive notifications when we update ChoiceScript.)

## Your First Scene: `*choice` and `*finish`

Here's a simple scene written in ChoiceScript. You can find it in `web/mygame/startup.txt`.

`

      Your majesty, your people are starving in the streets, and threaten revolution.
      Our enemies to the west are weak, but they threaten soon to invade.  What will you do?

      *choice
        #Make pre-emptive war on the western lands.
          If you can seize their territory, your kingdom will flourish.  But your army's
          morale is low and the kingdom's armory is empty.  How will you win the war?
          *choice
            #Drive the peasants like slaves; if we work hard enough, we'll win.
              Unfortunately, morale doesn't work like that.  Your army soon turns against you
              and the kingdom falls to the western barbarians.
              *finish
            #Appoint charismatic knights and give them land, peasants, and resources.
              Your majesty's people are eminently resourceful.  Your knights win the day,
              but take care: they may soon demand a convention of parliament.
              *finish
            #Steal food and weapons from the enemy in the dead of night.
              A cunning plan.  Soon your army is a match for the westerners; they choose
              not to invade for now, but how long can your majesty postpone the inevitable?
              *finish
        #Beat swords to plowshares and trade food to the westerners for protection.
          The westerners have you at the point of a sword.  They demand unfair terms
          from you.
          *choice
            #Accept the terms for now.
              Eventually, the barbarian westerners conquer you anyway, destroying their
              bread basket, and the entire region starves.
              *finish
            #Threaten to salt our fields if they don't offer better terms.
              They blink.  Your majesty gets a fair price for wheat.
              *finish
        #Abdicate the throne. I have clearly mismanaged this kingdom!
          The kingdom descends into chaos, but you manage to escape with your own hide.
          Perhaps in time you can return to restore order to this fair land.
          *finish

`

As you can see, the `*choice` command presents the user with a list of options; the result of choosing each option appears indented right below the option (in an "indented block").

If you go to play this scene, you'll first be presented with three options:

1. Make pre-emptive war on the western lands.
2. Beat swords to plowshares and trade food to the westerners for protection.
3. Abdicate the throne. I have clearly mismanaged this kingdom!

If you choose option #1, you get to choose how to win the war. If you choose option #2, you may decide how to negotiate with the westerners. If you choose option #3, the scene ends with no additional choices.

As you can see, there's a lot you can do with just the `*choice` command and the `*finish` command. Indeed, using only these two commands and a lot of time, you could develop an entire "Choose Your Own Adventure" book!

## Go On, Play with It!

Try opening up `web/mygame/startup.txt` in a simple text editor like Notepad or TextEdit. If you change the text, save the file, and refresh the `web/mygame/index.html` page in your browser, you should be able to see the effect of your changes immediately.

## Indentation

Note that indentation in ChoiceScript is mandatory. Without those spaces for indentation, we would have no way to tell the difference between options nested within other choices and options on the main menu. You can indent blocks using spaces or with the Tab character (but not both in the same file). You can use any number of spaces you want, but you must be consistent. Code like this is not allowed: `

    *choice
        #Hold 'em.
            He calls; you win!
            *finish
          #Fold 'em.
            Better luck next time.
            *finish

` Option 1 has four spaces, but Option 2 has six spaces; since these don't line up, ChoiceScript will display an error message if you try to write scenes like this.

## Reusing Code: Goto and Label

ChoiceScript provides a way to jump around in a scene besides just making choices. You can use the `*goto` command to jump to any line in the scene, as long as you first put a `*label` on the line you want to reach.

`

      What kind of animal will you be?
      *choice
        #Lion
          *goto claws
        #Tiger
          *label claws
          In that case, you'll have powerful claws and a mighty roar!
          *finish
        #Elephant
          Well, elephants are interesting animals, too.
          *finish

`

When we reach the line `*goto claws`, we automatically jump to the line `*label claws`. You may create as many labels as you like, and use `*goto` to reach any of them.

Note that every indented (nested) block must conclude with either a `*finish` command (which ends the scene) or a `*goto` line which jumps to another line in the scene.

(You can also reuse code with the `*goto_scene` command, described later in this document.)

## Setting and Checking Variables

In ChoiceScript, you can use variables to make scenes and decisions more interesting than a "Choose Your Own Adventure" book.

To use a variable, you must begin by defining it and setting it, like this:

`

      *temp leadership
      *set leadership 20

`

TODO: Discuss `*create` vs. `*temp`. We will probably remove the `*create` command in the future, replacing it with something in `mygame.js`; we should document what the new thing will be. (The basic idea is that `*temp` variables only last for the current scene, whereas permanent variables persist through the entire game.)

Once a variable has been set, you can check the value of the variable like this:

`

      #Run for class president
        *if leadership > 15
          You win the election.
          *finish
        You lose the election.
        *finish

`

In this case, leadership is just set to 20, so the player is sure to win the election. But you can choose to give the player a different amount of leadership depending on the player's earlier choices. Using variables, the player's earlier leadership choices can have an effect on the story later in the game.

You can also add leadership points to the current number of leadership points, like this:

`

      *set leadership +20

`

This would add 20 points to the player's current leadership score. It's the same thing as writing `*set leadership leadership+20`. You can also subtract points with "-", multiply with "*" or divide with "/".

If you need to use multiple operators at once (e.g. you need to do both division and addition), you must use parentheses, like this: `*set honesty (leadership + manners)*2`. You may not omit the paretheses, even though it's perfectly understandable arithmetic: `*set honesty leadership + manners / 2`.

You can also show the player's current leadership score by using `${}` (a dollar sign followed by curly braces), like this:

`

      Your leadership score is: ${leadership}

`

By the way, variables aren't just for numbers. You can also put text in a variable by using quotation marks:

`

      *set lover_name "Jamie"

`

### Using `*else` and `*elseif` to Improve Readability

We can rewrite the leadership example above to use the `*else` command; this will make it easier to read.

`

      #Run for class president
        *if leadership > 15
          You win the election.
          *finish
        *else
          You lose the election.
          *finish

`

This does exactly the same thing as before, but using `*else` makes it clearer that only one of these two options is possible, just by indenting the code.

You can also use the `*elseif` command to define three possible branches, like this:

`

      #Run for class president
        *if leadership > 25
          You win the election by a landslide!
          *finish
        *elseif leadership > 15
          You win the election, but just barely.
          *finish
        *else
          You lose the election.
          *finish

`

## What Happens When We `*finish`?

When we `*finish`, we move on to the next scene in the game. This is defined in a file called `mygame.js`. Here's an example:

      // Specify the list of scenes here, separated by commas, with no final comma

      nav = new SceneNavigator([
          "startup"
          ,"animal"
          ,"variables"
          ,"ending"
          ,"death"

      ]);

      // Specify the default starting stats here

      stats = {
          leadership: 50
          ,strength: 50
      };

      // Specify the stats to use in debug mode

      debugStats = {
          leadership: 50
          ,strength: 50
      };

      // or just use defaults
      // debugStats = stats

The first section defines the scene "navigator," which describes how we move from scene to scene. If you `*finish` in the "startup" scene, we'll move right ahead to the "animal" scene, then the "variables" scene. Finally, we reach the ending scene. Here's an example ending scene:

      This is the last scene!  The game is over!

      *ending

That final `*ending` command instructs the game to insert a "Play Again" button at the end of the scene. If you choose to "Play Again", the game will begin again at the "startup" scene.

WARNING: mygame.js is likely to change considerably very soon. It's currently the absolute minimum amount of code that could possibly work; we'd like it to be in a nicer format that looks more like ChoiceScript and less like JavaScript.

(Note that Choice of the Dragon doesn't even have a `mygame.js` file; that feature was developed after CotD was released. You can see something similar in its `index.html` file.)

You're not required to use `*finish` to move on to the next scene; you can also jump to any scene in the game using `*goto_scene`. Here's an example:

      #Lift weights
        *if strength > 15
          You lift the weights.
          *finish
        You drop the weights and hurt yourself badly.  You never recover.

        *goto_scene death

When this happens, we jump directly to the death scene. This allows you to provide a standard "death" message without copying and pasting all over the game.

## Examples

Here some example scenes from Choice of the Dragon. Please don't copy their code without explicit permission from Choice of Games.

- startup
- queenpolitics

## Next: Other Commands and Tricks

## TODO: Troubleshooting Guide

## TODO: Hacker's Guide

## Questions?

Please post on the ChoiceScript google group if you have questions about this document.