Intermediate/Advanced Game Variables

seita

Donn_M
Veteran
Joined
Feb 6, 2013
Messages
2,255
Reaction score
612
First Language
English
Primarily Uses
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:

 ​
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[5].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[9] = 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[8] = [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[8][0].
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[8] 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[11] == $game_variables[13] 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[13] 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!
 
Last edited by a moderator:

Dragonduke

Villager
Member
Joined
Jun 23, 2012
Messages
22
Reaction score
1
First Language
English
Primarily Uses
Excellent tutorial Seita.  It takes a moment or two to see how the logic flows, but once you get it:

BOY can you have a ton of fun with the concepts presented here in your game.

Thanks for taking the time and effort to give us this.
 

Engr. Adiktuzmiko

Chemical Engineer, Game Developer, Using BlinkBoy'
Veteran
Joined
May 15, 2012
Messages
14,696
Reaction score
3,006
First Language
Tagalog
Primarily Uses
RMVXA
never knew that game_variables can take non integers... nice...
 

C-C-C-Cashmere

Veteran
Veteran
Joined
May 18, 2012
Messages
587
Reaction score
283
First Language
English
Primarily Uses
Question: can you store 2d arrays inside variables? Y/N
 

Mouser

Veteran
Veteran
Joined
Aug 19, 2012
Messages
1,246
Reaction score
264
First Language
English
Primarily Uses
never knew that game_variables can take non integers... nice...
Anything in Ruby can take anything, just about.

Everything is an object: everything. So you can have an array that contains strings, literals, constants, more arrays, etc...

It's the only language I know that lets you directly pass a block of code as a parameter, because a block of code is ... an object.

Edit: @thatBennyGuy: yes, though you might need to script to do it.

I don't mess much with stuff like this in events unless I absolutely have to.
 
Last edited by a moderator:

whitesphere

Veteran
Veteran
Joined
Mar 14, 2014
Messages
1,688
Reaction score
785
First Language
English
You can also use Hashtables in Ruby within variables.  This is a useful way to have a single variable contain an arbitrarily large number of other things, identified by a name, rather than an index.  To do this in a Script box:

$game_variables[5]={}

$game_variables[5]["health"]=100

$game_variables[5]["description"]="My Name"

$game_variables[5]["hunger"]=5.0

Then, to access them, do this:

$game_variables[5]["hunger"]

This is a fairly simple way to store a set of state information in a single variable.  

The only caveat is that, if a value has not been stored as that member, it will return the special Ruby constant called nil. 
 

SoulPour777

Crownless King
Veteran
Joined
Aug 15, 2012
Messages
1,093
Reaction score
104
First Language
English
Primarily Uses
N/A
Interesting. This is a great find.
 

Palsa

General of Aurda
Veteran
Joined
Dec 21, 2012
Messages
343
Reaction score
84
First Language
English
Primarily Uses
Excellent post Seita! :D


The things that variables can do never cease to amazing me. ^_^
 

KadoDragon

Veteran
Veteran
Joined
Sep 10, 2013
Messages
155
Reaction score
9
First Language
English
Primarily Uses
I have a question! So without any extra functionality to display specific array variables you have to put the array variables into other variables? What's the point of using an array then? I'm familiar with the function of an array, more or less to organize specific blocks of information. But if you're handing it out, why not just have separate variables that hold that information anyways? If the array is only used for mathematical purposes and is never displayed, THEN it could be useful. But to display them on the screen, you need some extra function.
 

seita

Donn_M
Veteran
Joined
Feb 6, 2013
Messages
2,255
Reaction score
612
First Language
English
Primarily Uses
Hey Kado~

The examples above are just a few limited uses of an array within a variable. Getting specific letters in the name wouldn't be possible if you didn't tap into the variable as an array in the first place. For other more advanced examples, you can use multiple variable arrays for iterative purposes.

Say you want to make a world map in the same style as FF11 or FF Tactics, where you can't travel on the world map, instead you would be looking at it like a map with locations. Choosing a location would teleport you to that place.

Array 1 would house the X location of the spot, Array 2 would house the Y location of the spot. Array 3 can house whether or not that location is unlocked.

So when you open up the world map (via a common event, or something) You would iterate through Array 3 to see if that location is unlocked. If not, skip it. If it is, then show the location marker at (Array1[#], Array2[#]).

Once all of the locations are being shown, you would go onto the next phase of the world map, showing the cursor. Perhaps you can use Array 3 as your current location as well. You can draw the cursor with an offset say (Array1[#] + 10, Array2[#]) so the little hand pointing wouldn't be blocking the location marker. The rest of it is easy enough.

Another even more advanced example would be what I'm currently developing, a shop system where the player runs a shop and the NPCs buy items, basically what Reccettear is. It would take forever writing down and explaining all of them so I'll just copy and paste some of my notes and references below:

v150 Display Item Type - var150[Ev_ID] holds Type of item on display. (1 = items, 2 = weapons, 3 = armors, -1 = EMPTY) (5-13 rare items)
v151 Display Item ID - var151[Ev_ID] holds ID of the item. (var151[Ev_ID][item_ID])
v155 Display Item Amount - var155[Ev_ID] holds # of items on display
v152 Display Spots - array holding ID of event displays
v153 Current Display - current display being processed (for ZAL, shop setup phase)
v154 Total Displays - total amount of displays in shop

v86 Customer Name - array that houses the name of the customer.
v87 Customer ID - array that houses the entry of the customer.
v88 Customer Filename - array that houses the filename of the customer.
v84 Customer Item Type - (1-3) (4 = anything)  - Entry in Array = Event ID
v85 Customer Item ID -  (-1 = anything)

v89 Name Database - Name of Customers
v90 ID Database - Event ID of Customers
v91 Filename Database - Sprite of Customers
v92 Customer to Event - entry in array = event ID, content = v86 entry#
v148 Cust Item Type Database - Type of Item customer wants
v149 Cust Item ID Database - ID of item customer wants

v93 Shop Capacity Current - Number of customers currently inside (limit the number of people inside) minus those in line.
v94 Shop Capacity Limit - Number of customers allowed at one time inside.

v95 Display Case X - x-coord for front of display case
v96 Display Case Y - y-coord for front of display case
v97 Display Case Dir - direction customer faces for display case (2-down 4-left 6-right 8-up) (1,3,5,7 stationary)
v98 Number of Display cases (not number of spots)

v3 Number of items being bought
v156 Price of Item - base price of item(s)
v157 Customer Price - price customer wants to pay
v158 Player Price - price player wants customer to pay
v164 Cust.Price Prcntage - Percentage of base price customer wants to pay
v165 Player.Price Prcntage - Percentage of base price player wants to pay
v166 Cust.Price Range - Percentage range for customer price. Always randomized 16%, may go up and down depending on item and location.
v167 Haggle.Prcntage - Percentage the customer will accept you haggling up.
v196 Current Item Description - houses the description of the current item.

v5 Timer Var - Use to randomize customer movements
v6 Super Temp Var 1 - Use to iterate
v7 Super Temp Var 2 -Array! Use for randomizing customer decision.
v8 Super Temp Var 3 - iterating for selling and display hiding [0] = iterate items to store [1] = iterate items into array, and number of different items available (use as normal var during shop setup)
v9 Super Temp Var 4 - Use for dialogue.
v159 Rand Cust. Purchase - Use for randomizing customer decision.
 
Last edited by a moderator:

KadoDragon

Veteran
Veteran
Joined
Sep 10, 2013
Messages
155
Reaction score
9
First Language
English
Primarily Uses
Hey Kado~

The examples above are just a few limited uses of an array within a variable. Getting specific letters in the name wouldn't be possible if you didn't tap into the variable as an array in the first place. For other more advanced examples, you can use multiple variable arrays for iterative purposes.

Say you want to make a world map in the same style as FF11 or FF Tactics, where you can't travel on the world map, instead you would be looking at it like a map with locations. Choosing a location would teleport you to that place.

Array 1 would house the X location of the spot, Array 2 would house the Y location of the spot. Array 3 can house whether or not that location is unlocked.

So when you open up the world map (via a common event, or something) You would iterate through Array 3 to see if that location is unlocked. If not, skip it. If it is, then show the location marker at (Array1[#], Array2[#]).

Once all of the locations are being shown, you would go onto the next phase of the world map, showing the cursor. Perhaps you can use Array 3 as your current location as well. You can draw the cursor with an offset say (Array1[#] + 10, Array2[#]) so the little hand pointing wouldn't be blocking the location marker. The rest of it is easy enough.

Another even more advanced example would be what I'm currently developing, a shop system where the player runs a shop and the NPCs buy items, basically what Reccettear is. It would take forever writing down and explaining all of them so I'll just copy and paste some of my notes and references below:

v150 Display Item Type - var150[Ev_ID] holds Type of item on display. (1 = items, 2 = weapons, 3 = armors, -1 = EMPTY) (5-13 rare items)

v151 Display Item ID - var151[Ev_ID] holds ID of the item. (var151[Ev_ID][item_ID])

v155 Display Item Amount - var155[Ev_ID] holds # of items on display

v152 Display Spots - array holding ID of event displays

v153 Current Display - current display being processed (for ZAL, shop setup phase)

v154 Total Displays - total amount of displays in shop

v86 Customer Name - array that houses the name of the customer.

v87 Customer ID - array that houses the entry of the customer.

v88 Customer Filename - array that houses the filename of the customer.

v84 Customer Item Type - (1-3) (4 = anything)  - Entry in Array = Event ID

v85 Customer Item ID -  (-1 = anything)

v89 Name Database - Name of Customers

v90 ID Database - Event ID of Customers

v91 Filename Database - Sprite of Customers

v92 Customer to Event - entry in array = event ID, content = v86 entry#

v148 Cust Item Type Database - Type of Item customer wants

v149 Cust Item ID Database - ID of item customer wants

v93 Shop Capacity Current - Number of customers currently inside (limit the number of people inside) minus those in line.

v94 Shop Capacity Limit - Number of customers allowed at one time inside.

v95 Display Case X - x-coord for front of display case

v96 Display Case Y - y-coord for front of display case

v97 Display Case Dir - direction customer faces for display case (2-down 4-left 6-right 8-up) (1,3,5,7 stationary)

v98 Number of Display cases (not number of spots)

v3 Number of items being bought

v156 Price of Item - base price of item(s)

v157 Customer Price - price customer wants to pay

v158 Player Price - price player wants customer to pay

v164 Cust.Price Prcntage - Percentage of base price customer wants to pay

v165 Player.Price Prcntage - Percentage of base price player wants to pay

v166 Cust.Price Range - Percentage range for customer price. Always randomized 16%, may go up and down depending on item and location.

v167 Haggle.Prcntage - Percentage the customer will accept you haggling up.

v196 Current Item Description - houses the description of the current item.

v5 Timer Var - Use to randomize customer movements

v6 Super Temp Var 1 - Use to iterate

v7 Super Temp Var 2 -Array! Use for randomizing customer decision.

v8 Super Temp Var 3 - iterating for selling and display hiding [0] = iterate items to store [1] = iterate items into array, and number of different items available (use as normal var during shop setup)

v9 Super Temp Var 4 - Use for dialogue.

v159 Rand Cust. Purchase - Use for randomizing customer decision.
Ok, so here's the dealio of where I stand right now. I am an eventer. I do not do ANY script work (using other scripts doesn't count). I do have enough programming background to understand how arrays work and what their purpose is. I want to make a menu that displays a list of skill levels. I can store each numerical value into one array. Now I need to display each individual value on it's own. Using a script I have acquired, I can display text anywhere on the screen. Put a menu looking picture and display the numbers over top of the picture. To do that, I would need to start distributing each array into other variables. Why wouldn't I just put each skill into its own variable instead of having to take up an additional variable for the array? I MIGHT be able to finagle a way to do this, I have something jumbling around in my head. But if there is a way to display each array individually WITHOUT having to distribute them to other variables, let me know.

EDIT: OKAY! So with some finagling, I found a way (using the script I mentioned earlier) to display any part of the array at the same time only using up two variables to do so. One for making the array in the first place, and another to take in the array values and be used to display them. Basically, I make the array. Then I get the "display" variable to take in, say, index 0 of the array. Then I display the text. Then I change the "display" variable to index 1 of the array and display a second text layer. Then bim bam bozzle. I got both array values on the screen with the use of only two variables. So theoretically, I could have a ton of arrays and one display variable that changes and is called upon as necessary. But again, that only works if you use the script I was referring to earlier. Not the default message system. But that solves my problem.
 
Last edited by a moderator:

seita

Donn_M
Veteran
Joined
Feb 6, 2013
Messages
2,255
Reaction score
612
First Language
English
Primarily Uses
How does the script show text on the screen? Does it go by comment call or do you have to do a script call? Chances are you just have to add the array entry into the call to get the right bit of information without having to transfer each value into its own variable. If you can show me how the script works or link me to it I can show you a way to display it.
 

KadoDragon

Veteran
Veteran
Joined
Sep 10, 2013
Messages
155
Reaction score
9
First Language
English
Primarily Uses
How does the script show text on the screen? Does it go by comment call or do you have to do a script call? Chances are you just have to add the array entry into the call to get the right bit of information without having to transfer each value into its own variable. If you can show me how the script works or link me to it I can show you a way to display it.
Sorry. I edited my post before refreshing. Look above. Again, sorry. I also should read. Your way is probably easier... -_- But still, my finagled way is kind of interesting.

So ya. Its in a script call. So I can just call the individual array values. I tend to over think things. I'm also a little retarded. Leave me alone.  :p
 
Last edited by a moderator:

seita

Donn_M
Veteran
Joined
Feb 6, 2013
Messages
2,255
Reaction score
612
First Language
English
Primarily Uses
well glad you got all that sorted out ;p
 

KadoDragon

Veteran
Veteran
Joined
Sep 10, 2013
Messages
155
Reaction score
9
First Language
English
Primarily Uses
I have one more question. Is there a limit to how many different values an array can hold?
 

seita

Donn_M
Veteran
Joined
Feb 6, 2013
Messages
2,255
Reaction score
612
First Language
English
Primarily Uses
Not sure if you're asking if an array can hold multiple types of values or if you're asking how many values an array can hold. If the former, it seems to be able to handle multiple types of data, so entry 1 can be a number while entry 2 can be a string. I can't vouch for its stability though, since iterating through an array with different types of values may require multiple data type checks and conversions to make sure it doesn't error out.

For the latter, I believe it's in the millions, so if you're entering entries by hand, you probably won't run out of space.
 

KadoDragon

Veteran
Veteran
Joined
Sep 10, 2013
Messages
155
Reaction score
9
First Language
English
Primarily Uses
Not sure if you're asking if an array can hold multiple types of values or if you're asking how many values an array can hold. If the former, it seems to be able to handle multiple types of data, so entry 1 can be a number while entry 2 can be a string. I can't vouch for its stability though, since iterating through an array with different types of values may require multiple data type checks and conversions to make sure it doesn't error out.

For the latter, I believe it's in the millions, so if you're entering entries by hand, you probably won't run out of space.
I was asking for the latter ya. I had an array that held 2, 3, 4, 5, 6 and "The" and displayed them proplerly. So I guess it can handle different types perfectly fine.

It's in the millions? ****. Guess I wont have to worry about running out of room any time soon. Man. I hope people realize that this kind of power over game variables actually opens up a WHOLE lot more to the developer.
 

KanaX

Just being a sleepy
Veteran
Joined
Apr 3, 2013
Messages
1,464
Reaction score
1,291
First Language
Broken English.
Primarily Uses
N/A
Is there a way to create a conditional where we ask that a variable has a value between two other values? Example: (value1) <= variable <= (value2)
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

Feeling a little overwhelmed... There are so many things to do.
- Building a simple website for portfolio
- Setup *******
- Manage social media
There are so many things inside of these tasks :(
Meet the cute little ghost, FhooFhoo. :kaopride:
If anyone here is looking for another high-potential RPGMaker game--besides She Dreams Elsewhere--be sure to follow Lawmage Academy. Good social media presence will take that game far.
Lol, I was about to write a small tutorial and realized it would belong to the MZ section ....that does not exist yet.
I've noticed that more videos on YouTube have these segmented bookmark things in the videos themselves. It's nice being able to jump to parts of a video you want, and to have them all clearly labelled.

Forum statistics

Threads
99,574
Messages
966,799
Members
131,231
Latest member
air_kity
Top