Learning ChoiceScript: Part 4: Using Variables

Learning ChoiceScript

ChoiceScript is a scripting language from Choice of Games LLC for creating choose-you-own-adventure games.


 

Creating Variables

Like other scripting languages, ChoiceScript supports the the creation of variables. In ChoiceScript, to create a variable, use the command *create followed by the name of the variable and then its value.

Screenshot 2018-08-21 12.24.25

In ChoiceScript, variables are global. Once created they can be accessed in any file that is part of the project.

(For temporary variables, use *temp to create a value usable only within the file in which it was created.)

Using Variables

To show the value of a variables, enclose it in curly brackets and use a dollar sign. For example, when using a variable named “choices”, the code would look like the following: ${choices}.

Screenshot 2018-08-21 12.30.25

The values of variables can included within the text of a project in this way.

Screenshot 2018-08-21 12.31.37

Changing the Value of Variables

Similar to creating a variable, changing the value is possible through the *set command.

Screenshot 2018-08-21 12.34.41

Variables can be *set to new values and updated through using the operation and then the new value followed by the amount of value to change by. For example, to add one to a value, it would be “+1”. The same with subtracting by 10, “-10”.

Conditional Statements

The testing of values in ChoiceScript is also possible using the *if command.

Screenshot 2018-08-21 12.39.26

Possible Conditional Commands (copied from Important ChoiceScript Commands and Techniques):

  • Equal to: leadership = 40 (Is leadership equal to forty?)
  • Not equal to: leadership != 40 (Is leadership different from forty?)
  • Greater than: leadership >40 (Is leadership greater than forty?)
  • Less than: leadership <40 (Is leadership less than forty?)
  • Greater than OR equal to: leadership >=50 (Is leadership greater than or equal to fifty?)
  • Less than OR equal to: leadership <=40 (Is leadership less than or equal to forty?)
  • And: (leadership > 30) and (strength > 40)
  • Or: (leadership > 60) or (strength > 70)
  • Not: not(strength > 70)
  • Complex parentheses: ((leadership > 60) and (agility > 20)) or (strength > 80)

 

GitHub Gist Version:

You didn't survive the trip!
(You made ${choices} choices!)
*ending

view raw
badendings.txt
hosted with ❤ by GitHub

After a short delay, we enter the house.
(You made ${choices} choices!)
*ending

view raw
goodendings.txt
hosted with ❤ by GitHub

*if choices > 1
We've made many choices.
Finally, we arrived at the house.
Did we knock on the door or try to barge in without warning?
*choice
#Knock on the door
*set choices +1
*goto_scene goodendings
#Barge in!
We got caught in a trap!
*goto_scene badendings

view raw
house.txt
hosted with ❤ by GitHub

*title Example
*author Dan Cox
*scene_list
startup
house
*create choices 0
(You've currently made ${choices} choices.)
Where will we visit?
*choice
#The woods
We went into the woods!
It was a dark and scary place, but with our lantern, we were able to make our way deeper.
Deep in the woods, we came across a river. Did we try to cross it?
*set choices +1
*choice
#Cross river
We did not make it across the river and were swept downstream to our doom!
*goto_scene badendings
#Did not cross river.
We didn't bother with the river and made our way along the path deeper and deeper into the woods.
*set choices +1
*finish
#The mountains
We went into the mountains
*set choices +1
*finish
#The lake
We went to the lake
*set choices +1
*finish

view raw
startup.txt
hosted with ❤ by GitHub