Intermediate/Advanced Game Variables
Variables are useful in the creation of complex eventing systems that add functionality to any RPG Maker game. Once you get the hang of using them, they become an integral part of the game development process. However, once you start digging deeper into the system and use variables in conjunction with script calls, you'll find that they're magnitudes more useful than you previously thought. First things first. Before tackling this tutorial, make sure you are well versed in basic variable manipulation. Here are some recommended tutorials or skills needed that can help you catch up:
- Celianna's Variables Guide (For beginners!)
- Script Call Equivalent of Events
- You don't have to read through this whole topic. We will use it as a reference for specific script calls when necessary.
Show me something new about variables.
To you scripters out there, this isn't news and this is just basic programming. Alright so most people use variables as to deal with numbers. Bank accounts, how much of an item the player has, map coordinates, health manipulation and so forth. What a lot of people don't realize is that you can put almost "anything" into a variable. That includes letters, words or sentences (or strings). Why would you want to do this? A simple example of using variables for non-mathematical reasons would be a password system. There's already a basic tutorial of this on the RMW blog:
This is all well and good, but what if you want it to be very specific? Maybe the player has to make up a password early on in your game, and use it again later on. Maybe you want a password that uses multiple elements of the game, like a characters name with his level right afterwards, for some reason. Let's tackle the first one:
Create and Enter a Password.
With Event 6, we've created an Actor with the ID of 5 and named him Password for our purposes, and used this actor for our password input. Then we transfer "Passwords" new name into Variable 1.
With Event 7, we first empty Actor 5's name with the script call: $game_actors.name = "". By using two quotes, we've essentially emptied his name. If you skip this step, the actors name (or this case, our password) will show up in the name field. No fun, cheater!
Afterwards we use the same actor for the password input and then check it by comparing it to the first password we stored in Variable 1. Using == means we are checking if the value on the left is the same as the one on the right.
Keep in mind, the password must be EXACTLY the same as when you first input it. That means "Password" is not the same as "password".
Variables in Variables...?
Now lets get into something more complicated. Did you know that a variable is made up of an uncountable number of variables in it? That means you can have more than 5,000 variables without a script. In theory you could have way more than two billion variables! We call this an Array. So how do we take advantage of this? Lets look at how we can do this:
As you can see here, using the script call $game_variables = is the same as using the event command Control Variables. Here's the syntax on how to make a variable into an array, and how to access parts of this array.
In order to make a variable into an array, we have to tell the game. We do this by initializing the variable with a data set within brackets, separated by commas: $game_variables = [5, 10, 24, 4000].
In our example, I set variables 9 to 13 as separate entries of the array we made from variable 8. We access these entries by using the Index following $game_variables[#]. Realize that the first entry starts at 0, and increments up. So the first entry "5" is accessed by $game_variables.
The reason we are using Variables 9 through 13 to show the values within the array is because there is no window command that allows us to access these values. The easiest way to go about this is to have a number of variables be temporary use variables specifically for this purpose. Any scripter want to rectify this by adding some message box functionality?
You'll also notice that Variable 13 which accesses the 5th item (indexed as 4, because the 1st item is indexed as 0) is Zero. This is because all variables including any uninitialized entry within the array starts as Zero, also known as Nil. (for comparison, Switches are called "bools" and can only have two values, 1 and 0. 1 is On, and 0 is Off.)
Warning: If you use a normal Control Variable command on an array, it will overwrite the whole array with whatever you do in the command.
To really take advantage of arrays, you have to be fairly proficient with Script Calls. Going through the Script Call Equivalent of Events topic will really help. In here, you'll be able to find or ask for any equivalent of the Event Command Window as a script call. The following image shows all of the script call equivalents of the Control Variable command.
As you can see, the equivalents are pretty identical except for how it shows the variables themselves. In fact, if we just wanted to see "exactly" what was in the array without adding the separations to make them look similar, all we would need to do is use \V in the command window.
Using Strings (letters, words, statements).
Lets make things a little more interesting. How about I show you what we can do with strings instead? Did you know we can add strings together? What it does is append the second string onto the first. This is called concatenation. Here's an example.
Here, array 10 entries 0, 1 and 2 are "My", "name", and "no". We initialize variable 11 with entries in array 10 and add spaces and other words to create a whole sentence.
Now we've done something more useful with these variables. Did you know that a variable that's initialized as a single string is basically an array of letters (chars)? If you look at the code we can get single letters out of a string, or a name of an actor within the game. Perhaps you have the player input a name but you want someone to say it differently. Maybe they stutter or are short of breath.
First we initialize variable 11 with the party leaders name. Perhaps the party leader is the first person the event character sees. We then create a 'sentence' with variable 13 by adding specific portions of variable 11 (the name) and other things such as dots, question marks and so forth. We can do the same with a specific actors name, in this instance, Eric, the first actor. Don't want the event to say the same name twice if the leader and actor 1 are the same? How about doing a conditional. if $game_variables == $game_variables then use a different actor if its the same!
What about that super special password?
Easy peasy! All we did here was use three temporary variables. We initialized variable 11 with the leaders name, and 12 with the amount of gold. By using to_str() we've told the game to convert the numbers to string so that it can add it to the sentence. Without doing this, the game would give us an error, so it's very important!. After this, we initialize variable 13 with the first two letters of variable 11 by getting the index of the first two letters, in positions 0 and 1. Then we simply add the gold we've made into a string, and there we have it! Once the player inputs the password, we compare it to variable 13 and see if its correct. We then use another temporary variable 14 to show them the answer they have input. Edit: Actually, I could have just used \V since it's assumed to be correct in that conditional...
So I've explained how to go about using variables in different ways. There are much more creative people out there than me, and these techniques serve to bolster your repertoire in eventing. Have fun with it! It adds a whole new level of polish to a game that otherwise may be too straight forward. If I missed anything or something is too vague, please post and I'll gladly add to the tutorial. Have fun!
Edited by seita, 07 June 2014 - 06:25 PM.