Twine Variables - adanter/Prairie GitHub Wiki

Introduction

Twine variables are a feature added to the twine story creation. In essence, developers can keep track of certain 'states' for branching stories. An example for this might be keeping track of whether an NPC is 'happy' or not based on previous actions, and choosing subsequent actions based on this 'state'. This was added to facilitate more complicated stories to simplify the amount of branching twine nodes that might be required.

Syntax

Basic Syntax

Double Parentheses

Every time you want to use variables, you need to outline it in double parentheses like this: (($variable_name: value)) This tells Prairie to look for variable operations, and also allows Prairie to hide these lines during the game so the player doesn't see them.

Variable names

Whenever you write the name of a variable, you need to put a dollar sign ($) in front of it so that Prairie recognizes it. Otherwise, Prairie won't know what to do, and will not function properly. Also, remember that variable names can only include letters and numbers. They are also case sensitive, so $color and $Color would be treated as two different values. Examples: (($COLOR)) (($value1)) (($value2))

Setting Variable Values

Assignment

Assignment gives a variable the value you specify, regardless of what it's value was before. To assign a value to a variable, write the variable name followed by a colon and then the value you want to set it to. For example, (($color: red)) sets the variable "$color" to have the value "red". Like variables, values can include both letters and numbers. You can also put a minus sign at the beginning of a variable to give it a negative value, like "-6". Examples: (($color: red)) (($number: 1)) (($otherNumber: -1))

Addition

Addition allows you to add something to the value of a variable. If both the variable's current value and the value you are trying to add are numbers, then they will be added normally. For example, if you have both of these lines in a Twine node... (($number = 1)) (($number + 1)) ...then the value of "$number" would be 2.

Addition can also be used to combine non-numeric values. If either the variable's current value or the value you are adding include letters, the new value will be appended to the end of the existing value. For example, these lines... (($message: 2)) (($message + cool)) (($message + 4)) (($message + me)) ...would create the value "2cool4me".

If the variable you're trying to add a value to does not have a value already, then addition works like assignment. For example, if $color does not have a value already, (($color: red)) and (($color + red)) have the same effect.

Subtraction

Subtraction works exactly like addition, but first it reverses the sign of the value being added. For example, in the line (($number - 3)), Prairie first changes "3" to "-3", and then adds the negative value to $number.

Prairie also makes negative numbers positive before adding them. For example, in (($number - -3)), "-3" is changed to "3", then added to $number.

The catch is that Prairie does the same thing to non-numbers - it flips the sign, then uses addition like normal. As a result, in (($message - hi)), "hi" would be changed to "-hi", then "-hi" would be stuck to the end of $message. In other words, it's best to use subtraction only when you're sure you're working with two numbers.

Checking Variable Values

You can use the value of a Twine variable to enable or disable links in your Twine stories using an "If" statement. "If" the value of the variable meets a certain condition, then the link is enabled; otherwise, the link is not enabled and disappears. A node's "if" statements are checked continuously. Whenever a variable's value is changed, links are immediately updated to reflect it.

The Basic "If" Statement

The basic "If" statement looks something like this: ((if $variable = value [This link is enabled](/adanter/Prairie/wiki/This-link-is-enabled))) In that example, if the value of $variable is equal to the word "value", then the Twine link is enabled. Otherwise, for example if $variable's value is 5 or "apples", the link will not be enabled.

Comparison Options

There are currently six operations that can be used in an "if" statement:

  • = (Equals) This is the most simple option. If the variable's value and the other value are exactly the same, the link is enabled. Otherwise, the link is not enabled. Remember that variable names and values are case-sensitive, so "Apple" and "apple" are considered different values.
  • != (Does Not Equal) Does Not Equal, or "Not Equals" is the reverse of Equals. Whenever the variable's value and the other value are not exactly the same, the link is enabled.
  • > (Greater Than) Greater Than checks to see if the variable's value is a greater number than the other value. If so, the link is enabled. Greater Than only compares numbers. If either value contains letters, it will not enable the link.
  • < (Less Than) Less Than enables the link if the variable's value is less than the other value. Like Greater Than, it will only enable the link if both values are numbers.
  • >= (Greater Than or Equals To) This operation works exactly like Greater Than, except it also enables the link if the two values are the same number.
  • <= (Less Than or Equals To) This operation works exactly like Less Than, except it also enables the link if the two values are the same number.