RMXP Variables Demystified PDF Print E-mail
User Rating: / 2
PoorBest 
Written by Dubealex   
(1) Introuction

This is my first class, so I would like to have your opinion about it; so that I can improve anything that may need to be improved. This class will teach you absolutly everything about variables and what are their uses in RPG Maker XP. If you are a newbie, this is for you; and if you are not a newbie, this can still interest you in many ways, so read it quickly looking at the section's names... You may learn something !

(2) Introduction to Variables in General

- Definition of "Variable": able or liable to change.

Variables are used in every games and softwares; they store information, information that is then used by the system. We could say that a variable is a box in wich we want to put something for later use. What kind of information ? This can be divided into 4 major groups:

1 - Numbers
2 - Text
3 - Boolean
4 - Arrays

"Number" variable stores numbers; they are mainly used to do maths operation and to count. In RPG Maker XP, a "Number" variable could store the HP of the hero.

"Text" variable store text; they are usually called "String" in development terms. In RPG Maker XP, a "Text" variable could store the name of the hero.

"Boolean" variable are triggers, they can only be TRUE or FALSE. Meaning they can contains only 1 value, and you have only the choices between TRUE or FALSE. In RPG Maker XP, a "Switch" is actually a "Boolean" variable; it can be wether ON or OFF.

"Arrays" variable are a special kind of variable, in wich you can store multiple values. In a "Number" variable, you can store any numbers, but only one at a time. But in a "Array" variable, you can store many. So, as example, if you would want to store the actual HP the hero possess AND the maximum HP the hero can have, you could use an "Array" variable as follows:

On screen you see that: HP 120/245

If you would be using "Number" variable, you would need 2 of them, one for each numbers. If you had used an "Array" variable, you can store both in the same. This can help to minimize the amount of variable you are using, and its also helping the developer to better visualize its own concept. I.E: Instead of making 2 variable named "Hero1_Actual_HP" and "Hero1_Max_HP", he can just do the "Array" variable named "Hero1_HP". But how does he access both values ?

An array variable, in much program, are seen like this:
array_name [value1, value2, etc]

Our Hero HP example would then look like this:
hero1 [120, 245]

-> We will see more stuff about how to use and manage those variable type later.

(3) RPG Maker Variable System

RPG Maker XP Variable Operation Menu

This is everything RPG Maker XP allows you to do with variable from within an event environment. But don't worry, RPG Maker XP allows you to manipulate variable as specified in our introduction using its script capabalities. (We'll talk about script later.)

--> IMPORTANT ABOUT VARIABLE :
Variable in RPG Maker XP are recognized by the system only by their ID numbers, not by their names. Meaning that you could have 2 variable having the same name, but never the same ID. When naming a variable, always try your best to name it with something that would reflect it's content or its usage. I.E: If you want to store the balance of money the hero possess, you can call that variable "Money Balance".


The first thing you could notice is the fact that you can only store numbers in a variable; if you are only using an event command, that is true. The following is an explanation of the "Variable Operation" window:

(1) Specific Variable
Use this if you want to make an operation on one variable only. To choose a variable, you can click on the "dropdown" menu to it's right; wich will bring a new window up. The left column is the Variable ID numbers, classified that way in order for you to found what you want quickly. The right column contains the variable list from the "pack" you have chosen from the left colum. You can name your variables using the name field at the bottom right of the window.

You can have a maximum of 5000 variables in RPG Maker XP, when you begin a new project, you have 50 already pre-created and empty. To add/remove variable, click on the button "Array Size" at the bottom left. (Note: As you could had noticed, the button is named "Array Size", as in our "Array variable" of the introduction. This is because RPG Maker uses an array variable to store all your variable. But you see and manage them as different variable. This is the same for hero's name, switches, items, etc; and it is that way since RPG Maker 95.)

All in all, the variable you choose here will be affected by the operation you will choose in one of the options below.

(2) Variable Range
Use this if you want to make an operation on multiple variable at the same time. The numbers you choose in both case are actually the Variable ID, meaning that if you choose 3 in the first case and 10 in the second, the operation you will do will affect variable ID 3 to 10.

(3) Operator
This option is used to set how the operation will be set in the variable. You have the following choices:

Set
This will change the variable content to the one you specified. Example: If you do a "Set" operation with the choice "10", the variable will be equal to 10, whatever its old content was.

+
This will make an addition. Example: If you do a "+" operation with the choice "5", and the content of the variable was 5, the system will do 5+5, then the variable content will become 10.

-
This will make a subtraction. Example: If you do a "-" operation with the choice "5", and the content of the variable was 5, the system will do 5-5, then the variable content will become 0.

*
This will make a multiplication. Example: If you do a "*" operation with the choice "5", and the content of the variable was 5, the system will do 5 X 5, then the variable content will become 25.

/
This will make a division. Example: If you do a "/" operation with the choice "5", and the content of the variable was 5, the system will do 5 / 5, then the variable content will become 1.

Mod
This will make a modulus operation. But what is a modulus operation ? Here's a little class on mathematics:

The modulus is the ramainder of a division. If we divide 17 by 3 on a calculator, its answer would be "5.666666667", but this anwser has nothing to do with modulus.
The mod function is defined as the amount by which a number exceeds the largest integer multiple of the divisor that is not greater than that number. Here's how it's working:

17 mod 3 = 2
Because 17 / 3 = 5 remaining 2, wich means that 17 = 3 * 5 + 2

Try this: Take a calculator and divide 17 by 3, it will give you 5.666666667.
Now, take only the full integer 5 (don't take what is after the decimal). Multiply 5 by 3, and it gives you 15. Then what remains from 17 - 15 ? Exactly, 2. This makes the 17 mod 3 = 2 take all it's sense.

So, how does RPG Maker manage that ? It's not really hard, RPG Maker takes the number already held in the variable, and makes a mod by the number you specify. Meaning that to reproduce our 17 mod 3 example from above, we need to set a variable equal to 17, then do another variable operation using the MOD option, and choose 3. Then our variable would be equal to 2. I hope this clear out what the MOD is, even if you will probably never use it...

(4) Constant
A constant is like a variable, but it never changes. In this situation, the "constant" option allows you to choose by wich number the operation will be done. So, if you want to add 5 to a variable, you would simply choose the "+" operator and then put "5" in the "constant" field. Wich will make RPG Maker do the operation 5+5=10.

(5) Variable
This field allows you to choose a variable in a list. RPG Maker will then use the number held in this variable. Example: If the number "10" was stored in the variable "Money", and we set the operator "-" to the variable "Points", and "points" hold the number 5, RPG Maker would actually do 10-5=5.

(6) Random number between [...] and [...]
This will make RPG Maker choose a random number between the one you specified, and it will use it in the operation you choose. Example: If you have set the random numbers between 5 and 10, and the operator to "Set", if RPG Maker got a 6 as the random number, your variable will equal 6.

(7) Items
This will take the amount of the specified item as the value you will operates. Example: If the player have 10 potions, and you choosed potion in the item field, and you selected "Set" as the operator, your variable will now be equal to 10.

(8) Hero
The first field is where you choose wich hero to use data from. The second field is to choose what information (data) you want to use as the value in the operation. The following choices are offered to you:

1 - Level
2 - Exp
3 - HP
4 - SP
5 - Max HP
6 - Max SP
7 - Strengh
8 - Dexterity
9 - Agility
10 - Intelligence
11 - Attack Power
12 - Phys Defense
13 - Magi Defense
14 - Evasion

Meaning that RPG Maker will take the value of the information you specified, and use it in the operation. Example: If the hero's level is 12, and you make an "Set" operation, the variable will equal 12.

(9) Monsters
This works as the "Hero" field. Just refer to it for more details. The choices are the same, minus Exp and Level.

(10) Sprite
This field is used to take information about a certain sprite on the current map. (Sprites are the events on your map, including the player.) The following choices are offered to you:

1 - X Tile
2 - Y Tile
3 - Face
4 - Screen X
5 - Screen Y
6 - Terrain

The choices "X Tile" and "Y Tile" will take the actual map coordinates of the selected sprite. Meaning that if the player's current position on the entire map is 14:124 (Meaning that X=14 and Y=124), the value taken for "X Tile" would be 14.

The "Face" option will take the facing direction of the selected sprite. RPG Maker will use numbers as follows:

8 = Up
2 = Down
4 = Left
6 = Right

A trick to remember that is easy, look at your NumPad on the right of your keyboard: The layout is the same.

The "Screen X" and "Screen Y" will set the variable equal to the sprite relative screen coordinates.

The "terrain" option will take the ID of the terrain of the tile on wich the selected sprite is. (You can set terrain type in the Database, in the TileSet properties, press the Terrain button.).

(11) Other
This field is used to take data from the system. The following choices are offered to you:

1 - Map ID
2 - Party Size
3 - Money
4 - Number of steps
5 - Play Time
6 - Timer in seconds
7 - Number of saves

Everything in RPG Maker has an ID, and it is what is relevant to the system. So choosing "Map ID" will take the ID of the map the player is on.

Okay, now that everything about that window is explained, we can go to something more interesting: Variable usage.

(4) How to Display the Content of a Variable

To display the content of a variable on screen, in a dialog box, you simply create a "Message" command, and add that code in it:

\v[ID#]

"\v" is the code to say to RPG Maker XP to display the content of a variable.
"[ID#]" is where you insert the ID# of the variable for wich you want to show data. Meaning that if you want to display the content of the variable ID# 1, you would write this:

\v[1]

Just remember that !

(5) How to put text in a Variable

As I was saying in the introduction, RPG Maker XP is able to put text (string) in a variable, but only by using its script capabalities. A little class about RPG Maker XP's script:

RPG Maker XP uses "RUBY" as it's script language; a language is the code you are using to create a system; just like with Javascript, Perl, C++ or Basic. This means that RUBY has its own syntax and structure rules, and since it is a public language (it wasn't created by Enterbrain and it is not only used by them), you can found alot of ressources about RUBY everywhere on the web. The script capabalities of RPG Maker XP can be accessed in 2 places: The first place is in the massive Script Editor, accessible using the TOOL menu, or by pressing F11. In this editor, you can read and modify every script that made a game run in RPG Maker XP. The second place is by short call using an event command. There is an event command called "Call Script" on the last page of the event command list, to the bottom right. But the space you have is limited. You can also use short script sentence in the "Branch Condition" event command, once again last in the 4th page.

So, to put a text in a variable, you need to write it with a script. And it is very simple: Just use a "Call Script" command, and write this down in it:
$game_variables[1] = "Hi there, how is it going ? I feel really good today !"


This will have the effect of inserting the sentence "Hi there, how is it going ? I feel really good today !" in the variable ID #1. Then, if you want to display the content of that variable back on screen, you simply do as specified earlier, using "\v[1]" .

You just learned how RPG Maker XP recognizes variables in script language.
$game_variables[1] --> Where [1] is where you insert the variable ID#, just as in the text command " \v[1]" .

The " " symbol are what specifies to RPG Maker that what you just wrote is a text, and must be interpreted as such. This means that if you try to use a variable containing text in a math operation, the program will return an error.

If you wanted to change the value as a number using the script, you would simply discard the " " symbol. It would look like this:
$game_variables[1] = 10


This makes variable ID# 1 equal 10.

(6) Variable Usage within a Script Language

We have already seen the peek of the iceberg with the section "How to put text in a variable", you should read it if you want details about RPG Maker's script basic.

Every single operation that you can do with the "Variable Operation" command can be done using script. I'll show you the most common one. The syntax of RUBY for modifying a variable is the following:

Operator SET (Will modify the value by another one):
for numbers: $game_variables[1] = 10 

for text: $game_variables[1] = "text is in here"


Operator + (Will do an addition):
$game_variables[1] += 10 

#You need to put the "=" sign, otherwise it's not working.


Operator - (Will do a subtraction):
$game_variables[1] -= 10 

#You need to put the "=" sign, otherwise it's not working.



Operator / (Will do a division):
$game_variables[1] /= 10 

#You need to put the "=" sign, otherwise it's not working.



Operator * (Will do a multiplication):
$game_variables[1] *= 10 

#You need to put the "=" sign, otherwise it's not working.



Operator MOD (Will do a modulus):
$game_variables[1] %= 10 

#You need to put the "=" sign, otherwise it's not working.


You can also do things like that:
$game_variables[1] += $game_variables[2]


This will make an addition using both variables. So, if the variable ID#1 contains 10 and that variable ID#2 contains 5, the end result of 15 will be stored in variable ID#1 (The first in the line.) Of course you can use all other operators.

You can also uses any syntax used in regular maths in your code, so you can do powerful things like this:
$game_variables[1] = (10+10) * 2 

#makes this variable equal 40.


Now comes the cool stuff:
What can you do with script that you couldn't do with an event command, beside inserting text in a variable ? Many things ! You can now insert anything you like from any ressources RPG Maker uses, you must simply know the syntax that RPG Maker uses to identify its ressources. Here's an example of something that was impossible with only a "Variable Operation":
$game_variables[1] = $data_enemies[1].name 

#Will make the variable ID#1 contains the name of the monster ID#1 as text.


As you may have noticed, what come after the dot in "$data_enemies[1].name" is actually what you want to use. So, if you want to store the amount of Gold a monster gives you, you simply replace the "name" with "gold". Wich gives you this:
$game_variables[1] = $data_enemies[1].gold 

#Will make the variable ID#1 contains the gold dropped value of the monster ID#1.


To prove my point about the fact that you can access any ressouce RPG Maker uses, just by knowing its syntax, here's something that might be useless:
$game_variables[1] = $data_system.windowskin_name 

#Will store the name of the WIndowSkin set in the system in the variable ID#1 as text.


I will conclude this section with a little bonus: I'll tell you how to store the real timer value in a variable; not only in seconds, I mean the time you see on screen, like this: 10:24

To do that, you need to add a little script using the script editor. This will also introduce you to the script editor if you aren't used to it. Press F11 in RPG Maker XP and choose the class named Sprite_Timer (Class are what are listed in the left column.). Once you selected it, put your cursor at the beggining of line #48 and press enter to put the script that is already there below, we will have to insert our little code there. Once it is done, just copy that code in the space you just made:

NOTE: You must paste that code right below the line:
text = sprintf("%02d:%02d", min, sec)
#Alex code to show timer value in variable 
$timer_value = [1, ":", "",2] #Create a array variable

$timer_value[0] = min
#This makes the first value of the array = the minutes of the timer
$timer_value[3] = sec
#This makes the last value of the array = the second of the timer

if $timer_value[3] <= 9
#This check if the min are below 9, if yes, insert a 0 before.
$timer_value[2]= "0"
else
$timer_value[2]= ""
end
#End of Alex code to show timer value in variable


Then, if you want to show the timer values in a message, you just have to make a "Call Script" operation and store the content of our new array variable (timer_value) in a game variable. Like that:
$game_variables[1] = $timer_value 

#This will make the variable ID#1 equal the content of the entire array timer_value


You can try this out ! Just start a timer using "Timer Operation" and START it with a certain time (as you wish).

(7) Arrays Variable

We have seen an example of an Array variable above, with our "timer_value" bonus. Now comes the time to explore what an array is and how it is used. First thing first, let's explain the syntax (structure) of an array:
array_name [Value1, Value2, Value3, Etc]


Array_Name is the name you want to give to the array.

--> QUICK NOTES ABOUT VARIABLE TYPE & SCOPE:
If you only write the name of the variable without any code before it, it will become a local variable, useable only within the structure where it was created. Meaning that our timer_value script we did in the above section would not had worked if we did not add the $ sign in front the variable name. The $ sign makes the variable global, accessible from anywhere in the system. Make sure to remember that. (There are also Instance variable and class variable, starting with the @ symbol, but I won't explain that in here.)

Each value you want the array to possess are listed in the brackets [...], seperated by comas. The syntax is the same for storing elements in the array as for storing elements in a variable. I.E: Using the " " symbol will create a text element, and discarding them will create a number element. Here's some example and their result:
My_Array = [1, 2, 3] 

#Will store the numbers 1, 2 and 3
My_Array = ["123", "Ruby", "RPG Maker"] 

#Will store the text "123", ruby and RPG Maker.
My_Array = ["Enterbrain", 2, 3] 

#Will store the text Enterbrain and the numbers 2 and 3


As you can see, an array can possess element of both type, wich makes it a powerful tool to understand.

And now, how do you access those values ?
Let's say that an array is a multi-disc CD-Player. Let's say that in your multi-disc CD-Player you can have 5 CDs, this would make an array with 5 different values. Each values in the array would take 1 slot, those slot actually have an ID, called Array ID. The only trick is to remember that the first slot isn't slot 1, it is slot 0; and it is that way in all the programation language that I know of. This means that in the following example:
my_array = ["Hello", 120, "Peter Pan"]


To access the value "Hello", we would call the Array ID #0. To call the value "Peter Pan", we would call the Array ID #2. We call (or identify) an array ID within script like this:
my_array [1]


This would call the value 120 stored in the array ID #1 of our array. (Note: Now you can see that almost everything in RPG Maker are arrays; look at the way you call a variable within a script --> $game_variables[1] , this is the syntax for an array variable, no wonder why the ID is what is important !)

Knowing this, you can also change individual slot in your array without retyping everything, like that:
My_Array[1] = "X-RPG" 

#Will make the slot ID#1 that was 120 become the text X-RPG.


So, knowing this, you can now uses all the stuff explained in the above section implementing arrays, here's some example using the following array:
$My_Array = ["Hello", 120, "Peter Pan", "RubyScript"]


You can do those tings:
$game_variables[1] = $My_Array 

#Will store Hello120Peter PanRubyScript in the variable ID#1
$game_variables[1] = $My_Array[2] 

#Will store the text Peter Pan in the variable ID#1
$game_variables[1] = $My_Array[0..2] 

#Will store Hello120Peter Pan in the variable ID#1
#It will actually store value in slots ID #0 to 2 included
$game_variables[1] = $My_Array[0...2] 

#Will store Hello120 in the variable ID#1
#It will actually store value in slots ID #0 to 2 excluding the last


As you can see, it is somewhat powerful and interesting.

(8) How to do a Percentage

Let's say you want to give the completion percentage of a certain maze to the player. And you based your percentage on the number of chest to found, you would do something like that:

There are 34 chest to found. The rule to do a percentage is the following:

A/B --> (A*100) / B.0
Where A is the actual amount of chest the player has found, B is the total amount he must found. Example:

12/34 --> (12*100) / 34.0 = 35%

(We must add a .0 after the number that divise to obtain the right value from RMXP.)

So, to do this using only event commands, you would need to create a variable named "Chest Found" and another named "Total Chest". (I know that you aren't obligated to use a variable to store the total amount of chest, but it is a better design that way, because if you want to add or remove chest later, you just change the value of this variable; you won't have to check all your events.)

Then you need another variable named "Chest Percentage" to store the answer. Now that you have all your variables, you just do some "variable operation" to compute your percentage as shown above. You should be able to do it with all the details I've said since the beggining of the class.

What if you want to be fancy and use Ruby Script, the process would be much faster. This is a good example where script is useful as a matter of time. You can do a percentage like this, using one single line:
$game_variables[3] = ($game_variables[1] * 100) / $game_variables[2]


(9) How to compute a success rate

Let's say you are creating a lotery system in your game; you would probably want a success rate to regulate the amount of loss and win the player can have. Here's how to do it:

A success rate can be alot like a percentage: Your odds of winning are 3 on 10, wich makes a 30% chance of winning. But you want that system to be at random, so you need to do that:

Create a variable named "Lotery Odds", and in the "variable operation" window, choose the field "Random Numbers between..." and set it to choose a number between 0 and 10. Wich will make RPG Maker randomly take a number between 0 and 10 to store it in your variable "Lotery Odds".

Now, you want to compute a success rate of 30% (that is 3/10)... That is way better achieved using a script call than using "branch condition", so I will explain the script:
case $game_variables 
When 1..3
$game_switches[1]=true
Else
$game_switches[2]=true
end


You can recognize the syntax explained for arrays in the script (1..3), that line tells RPG Maker that when the content of the variable ID#1 is between 1 and 3 included, turn the switch ID#1 ON. (Take note that this is here only as example, so depending on your own game design and events placement, the code may have to be altered.) So that little 6 lines script actually let 3 chances to win to the player out of 10. (30% chances of winning).


(10) Conclusion

This conclude this class, I hope I was able to teach you everythinf about variable, and that you will be able to improve your skills with RPG Maker XP due to that new knowledge ! Variables are a powerful asset, and understanding them is an obligation if you want to create a game.

If you have any comments, suggestions or questions regarding this class, feel free to enter my class using the link at the top of the page and reply !
Comments (0)add
Write comment

busy
Last Updated on Saturday, 01 March 2008 02:01