A. Intro to variables
If you’ve ever programmed before, then you already know what a variable is–skip right ahead to section B. For everyone else: it’s time to learn about variables!
A variable is just a symbol that stands for something else. Remember algebra from school? Remember how, in algebra, a variable was just a letter that acts as sort of a stand-in for a number? Variables in code act like that too! The only differences are that (1) a variable doesn’t need to be just a letter; a variable can be a whole word (or even multiple words mashed together), and (2) it doesn’t have to represent a number; it can stand in for a word or phrase instead.
So why should we care about variables? Simple! They can be used to remember things.
For example: suppose you want to offer the player a choice of different dialogue responses, and the response they pick is going to matter later on. You can set a variable based on the response the player picked. Later on, you can check that variable, then tailor consequences based on the response they selected way earlier in the game!
Not only can you use variables to make the game remember things, you can also adjust them on the fly. Suppose that we want each choice the player makes in dialogue to affect her reputation. We’d need to create a variable that represents her starting reputation, then adjust that number up or down based on her choices.
Getting the idea? Variables are the building blocks of consequential choices. If that sounds like something you want to learn how to work with (and why wouldn’t it?), then follow along with me to section B!
B. Using SetString
Telepath Tactics lets you create two basic types of variables: strings and values. Strings are sequences of alphanumeric characters (words, phrases, etc.); values are integers (non-decimal numbers that can be either positive or negative).
Strings are a bit less complex, so we’ll start with those. To create a string, we use the SetString action.
SetString has two parameters: Variable Name and String. The String can be any word or phrase you wish.
So let’s say that there’s a dialogue branch where the player gets a choice of multiple replies, each representing a different faction she can ally herself with. Suppose she decides to ally herself with Tony Wonder, and we want to store the name of the faction the player selected. SetString is perfect for this!
We want to pick something short and memorable for the variable name–AlliedWith, say. Then we type Tony Wonder as our string. Thus, this will be the script action we run on the branch the player’s reply sends her to:
SetString AlliedWith,Tony Wonder
From this moment forward, the game will remember that the AlliedWith variable is set to Tony Wonder. And if the player makes a different choice later on that changes her alliance, we can easily reset the variable the same way we set it:
SetString AlliedWith,Barry Zuckerkorn
That is how we create and set string variables. But what about values?
C. Using SetVal
To create a value, we use the SetVal action. SetVal works similarly to SetString, but with one more parameter to set.
SetVal has three parameters: Variable Name, Operation, and Amount. The amount must always be an integer. When first creating a variable, the operation is always =.
So! Let’s say we want to create a value that stands for Ann Veal’s emotional well-being; we’ll use the variable name AnnEmotion. Since we’re just now creating the variable, the oepration is =. As for the amount, let’s say that her emotional well-being starts out neutral at 0, and that she gets happier as it rises higher and less happy as it sinks further into the negatives. This is what we end up with:
Now, the thing that’s cool about values is that we can modify them using mathematical operations. Here is the full list of operations that SetVal supports:
- = (make variable equal to amount)
- + (add amount to variable)
- – (subtract amount from variable, with results constrained to 0 or higher)
- — (subtract amount from variable, with negative results allowed)
- * (multiply variable by amount)
- / (divide variable by amount; the result will be rounded to the nearest integer value)
- % (multiply variable by amount as a percentage; the result will be rounded to the nearest integer value)
- r (randomly select a positive integer between 1 and the amount)
Suppose we want to increase Ann Veal’s happiness by 5. We’d run this script action:
Suppose we then want to multiply it by 2. We’d do this:
Now, if you look at the last type of operation on the list, you might notice that we can create values set via a virtual die roll. This, for instance, simulates a 1D6 roll and saves the amount “rolled” under the variable name DieRoll:
Pretty cool, eh?
D. Using SetValByVal
Now, what if you have two different numerical variables and you want to add them? Or subtract them? Or multiply them? Or divide one by the other? That’s what we have SetValByVal for!
SetValByVal – alter the value of a numerical variable by reference to the value of a second numerical variable. There are three parameters to the SetValByVal action: Custom Variable name, Operation, and Second Custom Variable name.
We use two minus signs here because we want AnnEmotion to be capable of going below zero. If we were doing this with a variable that represented something physical, like money or lumps of iron ore or something, we’d just use a single minus sign to ensure that the variable value does not dip below zero.
All right! Now that we know how to set and alter strings and values, we’ll learn how to actually use those variables to affect dialogue trees. See you next time!