How To Use Variables in Adult Interactive Stories

This article will help authors of interactive adult stories use Variables to keep track of items, inventory, stats, or anything else that you need to keep track of in a story. For example, using Variables you could start a character with twenty charisma points and remove one point every time they do something rude or mean. Variables allow you to easily keep track of items your characters are carrying (inventory) or items with which they are interacting in the world. There are many things you can do with Variables - allowing readers to customize their own characters in your story, keeping track of time, changing the seasons, letting your characters learn new skills, leveling up characters, and more.

Before starting on Variables, you should be familiar with the basics of writing interactive stories here at Literotica. Make sure you’ve read our “Getting Started With Writing Adult Interactive Stories”, “How To Write Interactive Stories With Choices”, and “How To Use Conditionals in Interactive Fiction” articles. Once you’re read those, let’s find out more about Variables!

What is a Variable?

A Variable is a container to hold a value. A simple example might be “Name” could be a variable that holds the value “Jane”. “Health_Points” could be a variable that contains the number “20”. “Day_Of_Week” could be a variable that holds the value “Tuesday”. “Day_Night” could be a variable that holds the value “Night” or “Day”. Variables can be used to show the reader customized information, or used silently by the author (most often with the help of Conditionals) to change the story based on each reader’s specific actions. Variables are an advanced writing tool to give your story game-like interactivity.

How Are Variables Used In Literotica Stories?

Variables are easy to use in Literotica Interactive Stories. You must first declare a Variable, and then you can use it anywhere in your story. Usage of variables may include showing custom text to the user, showing Choices to the reader (or hiding Choices from the reader), or updating/manipulating the variable itself. Variables are usually updated based on Choices that the reader makes in a story.

Let’s take a look at the easiest way you can use Variables in your story.

Declaring Variables

Before you can use a Variable, you need to declare it. What does “declaring” a Variable mean? It simply means you need to define it and tell the script that you will be using it. There are three parts to declaring a Variable, all of which should happen at the very top of your story, before the text starts.

  1. Variable_Name - The name of your Variable. The name must use underscore characters “_” instead of spaces and it should tell you what the Variable is being used for in the story. We recommend using naming prefixes to group variables if you will be using a lot of them. A Variable name may be as simple as “Dollars”.
  2. The equals sign “=”.
  3. Variable Value - The initial value of the variable. For the Variable “Dollars” (and for many Variables you will use in a story), the initial value might be “0”.

Here is a simple Variable declaration at the start of a story:

VAR dollars = 0
VAR day_night = "day"
VAR has_piece_of_gum = false
VAR inventory_typewriter = false
VAR inventory_sheets_of_paper = 0

The story text starts here.
They lived happily ever after.
-> END

In this example, we declare five Variables before the text at the top of our story. We’ve used three different types of Variables: numbers, true/false, and text strings. You should decide what type you want each variable to be at the start of your story and stick to that type throughout the story for consistency.

If you look at the last two Variables, we use a name prefix “inventory” to remind us that this Variable is part of the inventory system in our story. Variable naming is completely up to you, so you should choose a system that you can understand and remember.

Showing Variables To Readers Directly

There are two ways to use Variables to change the text that readers see. The first is the most simple:

You have {dollars} dollars in your pocket. It is {day_night} time.

You can show any Variable to the reader at any time by typing the Variable name inside of curly brackets “{variable_name_goes_here}”. This works for simple things like numbers and names, but in most cases it’s preferable to use a more flexible way to show custom text to your readers.

Using Variables To Customize Story Text

Using Variables, you can show custom text to your reader based on If/Then statements. Look at this example:

{dollars > 0: You have {dollars} dollars in your pocket.}{dollars == 0: You have no money.}

Here, we have two statements, of which the reader will see only one. The first statement says “If the reader has more than 0 dollars, then show them the text telling them how many dollars they have”. The second statement says “If the reader has 0 dollars, then tell them they have no money”. The exact way to write this type of text is:

{Logic Here: Text Here}

Use curly brackets “{}” to tell the script you are creating a logic block (instead of plain story text). Inside of the curly brackets, you write the If/Then statement first “dollars > 0” then you write a colon “:” and then the text you want to show if the If/Then statement is true “You have {dollars} dollars in your pocket.”. You can use any of the following type of If/Then statements in your story:

A == “B” - Variable is equal to a value.
{day_night == "day": It is day time.}

A > B - Variable is greater than a value.
{dollars > 0: You have {dollars} dollars in your pocket.}

A > B - Variable is less than a value.
{dollars < 1: You have no money in your pocket.}

A != B - Variable is not equal to a value.
{dollars != 10: You do not have 10 dollars in your pocket.}

A >= B - Variable is greater than or equal to a value.
{dollars >= 10: You have 10 dollars or more in your pocket.}

A <= B - Variable is less than or equal to a value.
{dollars <= 10: You have 10 dollars or less in your pocket.}

A - Value is true.
{inventory_typewriter: You are carrying a typewriter.}

A == false - Value is not true.
{inventory_typewriter == false: You are not carrying a typewriter.}

When comparing a Variable to text string value, you should use quotes around the value to tell the script that it is a value. If you don’t use quotes, the script will assume that the value is a variable name. Here are two examples:
{day_night == "day": It is day time.}
In the above example with quotes, the script sees “day” as a value and checks to see if the “day_night” variable is equal to the exact text “day”.
{day_night == day: It is day time.}
In this example, we removed the quotes from “day”. Now the script will assume that “day” is another Variable, not a value. If you want to compare two of your Variables, for example if you have a Variable for the size of a shoe found in the game and another variable for the shoe size of your reader and you want to find out if the shoe in the game fits your reader shoe size, you could do this:
glass_slipper_size == reader_shoe_size
In this example, the script will check to see if the Variable “glass_slipper_size” is equal to the Variable “reader_shoe_size”. You can then show some text if the values contained in the two Variables match. There are times when comparing two Variables will be useful, and times when comparing a Variable to a string value will be useful. Remember to use quotes around values, and not to use quotes around Variables. When using numbers or the logical words “true” or “false”, you don’t need to use quotes since the script knows those elements cannot be Variable names.

Note: You might notice that we use the equals sign in different ways in different logic statements. There are two main things to remember when using the equals sign:

“=” assigns a value to a Variable on the left side of the “=” sign.
variable_name = 10
Makes variable_name equal to 10.

“==” compares two values on either side of the “==” sign.
variable_name == 10
Checks to see if variable_name is equal to 10.

Using Variables To Show Choices To Readers

The other primary use for Variables in an Interactive Story is to show or hide Choices. This is the same usage covered in our “Introduction To Conditionals Article”, except we use Variables instead of Knot names for our If/Then statements. Here is a simple example:

VAR dollars = 0
VAR inventory_typewriter = false

The story starts here.
-> first_knot

=== first_knot ===
You want a typewriter to tell your story.
+ {inventory_typewriter == false} Go to the typewriter shop.
-> typewriter_shop

=== typewriter_shop ===
The man asks, "Would you like to buy a typewriter for 20 dollars?"
You have {dollars} dollars in your pocket.

+ {dollars >= 20} You have enough money, so you buy the typewriter.
~ inventory_typewriter = true
-> buy_a_typewriter
+ {dollars < 20} You don't have enough money. Go to your part time job.
-> get_a_job

=== buy_a_typewriter ===
You buy a typewriter.
+ {inventory_typewriter} Start typing.
-> live_happily

==== get_a_job ===
You want money to buy a typewriter, so you need to work.
+ Work 1 hour.
~ dollars = dollars +10
You got $10.
-> first_knot

=== live_happily ===
You live happily ever after.
-> END

In the above example, we use the Variable “inventory_typewriter” to check if the reader already has a typewriter or not.
+ {inventory_typewriter == false} Go to the typewriter shop.
If the reader does not have a typewriter, we show them a Choice that allows them to visit the “typewriter_shop” Knot. If this was a real story, we might add another Choice to show them if they already had a typewriter, but since this is the first Knot and we declared the “inventory_typewriter” as “false”, the author knows that no reader have a typewriter at this point in the story.

In the “typewriter_shop” Knot, we use the Variable “dollars” to check how much money the reader has:
+ {dollars >= 20} You have enough money, so you buy the typewriter.
If the reader has 20 dollars or more, we show them a Choice to buy the typewriter by going to the “buy_a_typewriter” Knot. If they have less than 20 dollars:
+ {dollars < 20} You don't have enough money. Go to your part time job.
We show them a Choice to go to the job Knot “get_a_job” to earn more money so they can come back and buy a typewriter.

Again, the If/Then logic for using Variables to show or hide Choices should already be familiar from previous tutorials. If it isn’t clear, please review the Conditionals FAQ.

Updating Variables In Your Story

Variables wouldn’t be very useful if you couldn’t update them. Luckily, it’s extremely easy to update a Variable most anywhere in your story. To update a Variable, you use the tilde symbol “~” at the start of a line, followed by the Variable name, the equals sign “=”, and then the value that you want to update. Here are a few examples:
~ my_text_variable = "new string value"
~ my_number_variable = my_number_variable + 10
~ my_true_false_variable = true
In each case above, your can see how to update a different type of Variable. Variables can be updated after the reader makes a Choice or inside of a Knot, depending on your needs. Updating after a Choice is the most common way to update Variables. Here is our previous example story:

VAR dollars = 0
VAR inventory_typewriter = false

The story starts here.
-> first_knot

=== first_knot ===
You want a typewriter to tell your story.
+ {inventory_typewriter == false} Go to the typewriter shop.
-> typewriter_shop

=== typewriter_shop ===
The man asks, "Would you like to buy a typewriter for 20 dollars?"
You have {dollars} dollars in your pocket.

+ {dollars >= 20} You have enough money, so you buy the typewriter.
~ inventory_typewriter = true
-> buy_a_typewriter
+ {dollars < 20} You don't have enough money. Go to your part time job.
-> get_a_job

=== buy_a_typewriter ===
You buy a typewriter.
+ {inventory_typewriter} Start typing.
-> live_happily

==== get_a_job ===
You want money to buy a typewriter, so you need to work.
+ Work 1 hour.
~ dollars = dollars +10
You got $10.
-> first_knot

=== live_happily ===
You live happily ever after.
-> END

In the “get_a_job” Knot, we update the Variable “dollars” by adding 10 dollars to it after the reader makes a Choice to work for an hour:
~ dollars = dollars +10
Using this format, we tell the script to update the Variables “dollars” to equal the current value of the Variable “dollars” + “10”. Using the “variable_name = variable_name + ##” allows you to add a specific amount to whatever amount of money the reader currently has, without having to know exactly how much they currently have.

Once the reader has enough money and buys the typewriter, we update the Variable “inventory_typewriter” to “true” so that the reader can start typing:
~ inventory_typewriter = true
The reader has purchased the typewriter, so we add the typewriter to their inventory when they make this Choice.

There is a lot more you can do with Variables, but these are the most common and simple ways to implement Variables to make your story more interactive (and more fun) for your readers. We plan to publish more advanced tutorials in the future. For now, enjoy writing with Variables!

Further Reading on Advanced Variables

Our article on Using Parameters and Temporary Variables details another method authors can us to share story information between Knots.

The creators of the Ink language have written a detailed help document explaining how to use every advanced feature of the language. If you’re interested in becoming a power user, please see their “Writing With Ink” tutorial for more information. We also have our own growing library of Interactive Story Writing Tutorials and FAQs that you might want to read.

If you’re interested in helping test and give feedback on the new Literotica Interactive Story format, either as an author or a reader, please read this thread in the Literotica Forum: Interactive Adult Story Testers Needed.