Variables & Values
Store and reuse values in your TruScript code.
In this guide, we will cover the basics of working with values and variables in TruScript. You should be familiar with both terms (value and variable) as they are fundamental to programming in TruScript. If you are new to programming, don’t worry! We will explain these concepts in detail, so keep reading.
What is a value?
A value is a single piece of data that TruScript can work with. Some value types include numbers, strings, and booleans. These might be types you have heard from other scripting languages. We will cover these value types in more detail in the next section.
Values can be used to perform calculations, make decisions, and store information. You can pass values to functions, get values back from functions, and store them in variables.
What is a variable?
Variables let you give a name to a value so you can use it later.
A variable can be whatever type of value you want, and the type of the variable can change over time. For example, you could have a variable that starts out as a number and later becomes a string.
Declaring variables
To give a name to a value, you can declare a variable by using the = operator. For example, the following code declares a variable called greeting and assigns it the value "hello":
greeting = "hello"
count = 3
print(greeting)
print(count)
Naming rules
Variable names start with a letter or underscore and may contain letters, digits, and underscores. You cannot start a variable name with a number, and you cannot use TruScript’s built-in keywords as variable names.
my_variable = "hello!" # This is a good variable name!
1st_variable = "hello!" # You can't start a variable name with a number.
if = "hello!" # You can't use TruScript's built-in keywords as variable names.
| Built-in keywords and global types are given a special color in the TruScript editor to help you avoid naming conflicts. If you name a variable incorrectly, it will be highlighted in red in TruWorlds Creator. |
We recommend snake_case for variable names, which means using all lowercase letters and underscores to separate words. For example, my_variable is a good variable name, while you should avoid using myVariable or MyVariable.
This is just a convention for writing good TruScript, though, and you can use whatever naming style you’d like! Just make sure you aren’t running into any naming conflicts with TruScript’s built-in functions and keywords, or TruWorlds object names
nil: This doesn’t exist
If you try to use a variable that hasn’t been declared yet, TruScript will instead give you the nil value.
nil is a special kind of value that basically means "this doesn’t exist". You can use nil to check if a variable has been declared yet or not.
print(my_variable) # We didn't make a variable called my_variable, so this will print "nil"
Make sure you are spelling your variable names correctly, or you may run into problems with nil! TruScript is case-sensitive, so my_variable and My_Variable are two different variables and can have different values.