The Scripter’s Journey Series
2. All About Variables!Table of Contents
I. What the *Bleep* is a variable?!
II. Types of values!
III. Variable Manipulation!
IV. Example in the default scripts!
V. ConclusionSide note: It will be attempted by these tutorials (at least the first few foundational ones) to apply to all RGSS languages (RGSS1, RGSS2, and RGSS3), so it would still be a good idea to read up on these tutorials regardless of what engine you use for your game. So therefore I’ll be saying “RGSSX” to refer to all of them, but the script editor I’ll be using is that of RMXP What the *Bleep* is a variable?!
A script without variables…that is rare. Variables are possibly the biggest key to your foundational learning in RGSSX. Well, what IS a variable, you ask? Simple. A variable is a way to refer to the majority of the time a value (object), but it can really be anything. Here are some examples: enemy HP, EXP, number of quests to complete, the list goes on. Think of variables as a list of nicknames. When you say “Johnny” you are actually referring to the “John”. Also, an important note, do not think that values and objects go inside
or are contained in variables. Variables are simply ways to refer to a value. Now you ask “Why are they called variables then?!” This is because the value that is being referred to is nearly always variable (or changeable and not consistent). For example, in one game enemy HP may be 1000, but in another game enemy HP may be 5, to solve this problem we have one variable that just equals enemy HP since variables refer to a value, in this case, enemy HP. Types of values!
Let’s open up our Script Editor (you should know how to do this from the last tutorial) and make a new page above Main (again, you should know how to do this). Call this script “Variables” (you should be able to name it by going in the bottom left where it says “Name:” Now, on to the fun stuff, we’re going to make some variables. Copy this code:
integer = 5
float = 5.225
string = “Five”
boolean = true
These are the major types of values accepted for variables to refer to. First we have integers which are simply whole numbers (like the main hero’s attack power). Next we have floats, which are integers with decimal portions added on. After that we have strings which to explain simply is just text (you must ALWAYS put quotation marks around it to signify it is a string), and finally we have booleans which are simple. A boolean value can either be “true” or “false”. There is another type of value that can be referred to and those are class objects (but we’ll get more into that when we discuss classes). Also as a side note (this can come in handy), you can set your variable to refer to absolutely nothing by using this code:
nothing = nil
“nil” is a keyword in RGSSX, and use CTRL+SHIFT+F (like discussed in the last tutorial) to find cases of the default scripts using nil. Variable Manipulation!
Alright delete any code you have right now in your script editor and let’s get back to work. Now that we know what values a variables can refer to (integers, booleans, strings, floats, nothing, and soon we’ll learn about class objects) let’s manipulate these variables and their values. First thing though, I may not have made it clear in the last section, but in order to create a variable you must use the assignment operator which is just a basic equals sign. So take one of my examples from the last section:
integer = 5
“integer” is the name of the variable, the equals sign means “the variable ‘integer’ will now refer to the value to my right”, which in this case is an integer value of 5. This is how you build a variable, but an important note on naming conventions. ALWAYS make the names of your variables lower case, that is important, and also it is recommended that you make the variable name make sense to you so you know what value is being referred to. Alright, now that that’s out of the way, let’s manipulate these variables. Copy this code:
five = 5
four = 4
three = 3
# The sum
sum = five + four + three
Now there are a few things that need to be explained in this block of code. First of all, the comment symbol. Comments do not affect code or execution of the code AT ALL, they are simply used to explain code and help the coder not forget why he wrote a line of code the way he did. To create a comment, simply being a line with a “#” symbol.
Next, we have the print command. Basically all that does is allow you to print to the screen the value to the right of it.
Finally, we have our variable manipulation. We first built three variables that each refer to integer values (3, 4, and 5). Then we added the variables together using the addition sign and due to the fact that those variables now refer to integers, it’s like adding integers together. Now, we make another variable called “sum” refer to the literal sum of the three variables we built prior to that. We end the block of code with us printing the final result to the screen which is 12. Example in the default scripts!
Alright guys, go to the “Game_Temp” default script and find a line of code that looks like a variable is being built. For me, since I’m in RMXP right now, I found the line of code:
@map_bgm = nil
Do not worry about the “@” sign, we’ll discuss that in later tutorial when we go over the different types of variables. You also don’t need to know what this means (even though you could infer that this means the map background music starts off being nothing or no music plays in the beginning unless explicitly stated). I just want to show you guys that variables can be found anywhere and they are extremely useful and there are constructed in the same way we discussed in this tutorial:
variable_name = valueConclusion
Well, hopefully this tutorial was more interesting than the last one (I’m trying lol). Good news though is that you should have a nice grip on variables and their purpose (along with the types of acceptable values a variable can refer to and manipulating variables). I will talk more about variables in later tutorials because there is still more to cover about them, but we have to learn some other information first before diving in.
The order of how these tutorials will progress: http://www.rpgrevolution.com/forums/index....showtopic=54953