Switches and Variables
If you have any amount of programming experience, you’re probably familiar with the use of switches and variables (especially the latter) in coding programs.
Switches? Variables? Pizza?
Yes. Yes. With pepperoni, please. Seriously, though, let me explain. Switches and variables in RPG Maker VX Ace (RMVXA) are similar to boxes in real life. They both hold information you would want to use later on. They are ubiquitous in video games as well. Don’t believe me? Here’s a small list of things that, with the use of switches and variables, you can influence in any video game:
As you must have noticed, you cannot make a video game without switches and variables!
Switches and Variables Do the Same Thing, Then?
Mostly, but there are differences. For switches, think of a light switch that has an on and an off state. You can set conditionals that only trigger when a certain switch is on or when said switch is off. Variables, on the other hand, can hold a number—any number that you can think of. Want to have-327 contained within a variable you call Bob? Sure, go right ahead! Basically, both switches and variables are used for storing relevant game information, but they are best used in different types of situations.
It is best to use a switch when you want to create a button/lever/switch in-game that is binary (that is to say, has exactly two states). It is best to use a variable when you have a situation that cannot be covered with a simple on/off state. For example, if you wanted to count how many goblins a player has defeated, you would have to store that information in a variable. On the other hand, if someone gave your character a quest to slay five goblins, you could have a switch flip to the “on” state after the fifth goblin is defeated.
While switch and variable theory is nice and all, let’s take some time to apply it.
Objectives:
So, how do we go about adding the necessary items to the map? We are finally going to start dabbling with the functionality that gives RMVXA its reputation as a robust game-development engine that even a novice can use. If you look directly below the Game item on the menu toolbar, you’ll notice a trio of icons (they are to the left of the drawing tool icons).
Click the Event Editing Mode icon (or press F6), and you’ll notice that a grid appears on the map. If you right-click any square of the grid, you’ll see a menu pop up, as seen in Figure 2-1.
Figure 2-1. The Event pop-up menu
The available options are as follows:
Click New Event and you’ll wind up on a large blank slate, as seen in Figure 2-2.
Figure 2-2. The New Event window
There are many aspects to the Event screen, so let’s break them down.
Note An easy way to determine if you need Player Touch or Event Touch is the following: for something that you want the player to trigger at his/her convenience, use Player Touch. For something that the player must avoid or is otherwise triggered outside of his/her control, use Event Touch.
An Event Can Have 99 Event Pages!
Yes, seriously. I’m hard-pressed to think of any situation in which you’d have to make use of all 99 pages, but most events you create for your RPG will require more than one. Multiple event pages are how you can make NPCs change conversations as you progress within the game, to name but one thing you can do with them. As a matter of fact, the button event I will create shortly is going to have two pages.
Ready?
Let us begin. First of all, check Figure 2-3 for the end result.
Figure 2-3. The finished button and exit in place
That’s a green button three steps to the player character’s right and the exit located five steps to the north. The button event is two pages, and the exit event is one page. Before I explain what commands to add, let’s think about the situation critically. Our player starts the game and sees nothing of note, save a single button. You want the player to press the button and receive some sort of feedback after the fact, so that he/she knows he/she did. So, page 1 of the button event will have a graphic of the green button in a raised position, while page 2 of that event will have a graphic of the same button in a lowered position. We will inform the player via a text box that the button has been pressed, before we do that.
Pressing the button will reveal the exit to the north. You are starting to see how this will all work out. In essence
How do we do that? With a switch! The final results for the pages are shown in Figures 2-4 to 2-6. We’ll step through the required instructions for reaching this state next.
Figure 2-4. The first page of our button
@>Text: -, -, Normal, Bottom
: : You stepped on the switch!
@>Play SE: 'Jump1', 80, 100
@>Control Switches: [0001:Switch] = ON
Figure 2-5. The second page of our button
@>Text: -, -, Normal, Bottom
: : You see a button pushed into the earth.
Figure 2-6. Our exit event for transferring the player to the next map
@>Transfer Player:[001:MAP001] (008,001)
Note If an event has a single page with a condition that has yet to be met, it will be invisible in-game. Thus, the player will not be able to see the exit until the “Switch” Switch is on.
Now you can see a large list of appropriately named Event Commands. There are three pages full of many useful commands for your eventing needs. The first command we need is called Show Text. Show Text will allow you to write in some text that will be displayed in a box when the event is triggered; see Figure 2-7.
Figure 2-7. The Show Text event window
As you can see in Figure 2-7, there are a few things you can do with this command.
Let’s write “You stepped on the switch!” and then click OK.
When you click on it, it will show you the screen shown in Figure 2-8. The Control Switches command is rather simple. You choose a single switch or a batch of switches to flip, and then you decide whether you want to set them to on or off. You can press the button labeled “...” to change the switch you are affecting (as well as give the switch a proper name, as all switches and variables default to having a blank name). Here, I’ve given Switch #0001 the name of Switch, but you can rename it to something like “ButtonPress” or “ButtonSwitch.” Once that is ready, all you have to do is set the other options, so that they match mine shown following:
Figure 2-8. The Control Switches window
Note The green switch that I used is from the tileset called !Switch1, namely, the one in the top-left corner.
That concludes the first event page for the button. Now, what you need to do is click New Event Page. You will see a new tab numbered 2 appear. Make sure to toggle the conditional, as I did, and set the relevant options, as shown following:
I also added some text reading: “You see a button pushed into the earth.” Once that is done, you can press OK, which will place you back at the map screen.
Note The green button graphic used in page 2 is the one on the lower-right corner of the group of relevant graphics in !Switch1.
Finally, right-click the square five steps to the north of the player’s starting position and set the parameters for the Exit Event as I have done below.
Note The graphic used for the exit can be found in the Tileset-B graphics group. It is two tiles down and one tile right from the upper-left corner.
As for the contents of the exit event, there are two ways to create a transfer event, as follows:
If you prefer to do it the long way, Transfer Player can be found on the second page of the Event Command List. Clicking on Transfer Player in the event command menu will present you with an image similar to that shown in Figure 2-9.
Figure 2-9. The Transfer Player event dialog screen
For now, just leave the settings as displayed in Figure 2-9. We’ll expand on this in the next section. Once all is said and done, you can play-test the game in its current state. Feel free to move around and interact with the button. If you have set up the map events correctly, all of the following should be true.
Note When an event has multiple pages, the right-most page with valid conditions is executed. That is the reason why we have the button event ordered as it is. If it were backward, the event would keep repeating itself every time you stepped onto the square.
You may have noticed beforehand that the destination for the map exit has been set to the same coordinates as the exit itself. What we need to do now is create a new map and change the Transfer event command to send us to the new map. To do that, take a look at the lower-left corner of the map screen, shown in Figure 2-10.
Figure 2-10. This portion of the screen displays all of the maps in your game
You should see something similar to the preceding screenshot. On your end, Test will be whatever you decided to call your project. MAP001 is the default 13×17 map that automatically gets inserted into every new project. Right-click MAP001, and you’ll see the following options:
For now, let’s click New Map. You will land on the screen shown in Figure 2-11.
Figure 2-11. The New Map dialog screen
As with several of the preceding screenshots, I have tweaked it, so that it can fit neatly within the confines of this page. I’ve left out the Encounters section on the right side of the window. Encounters are relevant to monster spawning, and I will be touch on them at a later time. Anyway,
Those are the relevant options. Let us set them, like so:
When you’re ready, press OK. You will be greeted with a large checkerboard of nothing.
Why Is the Map Empty?
While MAP001 comes with water tiles already set, subsequently created maps are completely blank (to be precise, that’s the transparent tile). To answer the question posed by the section heading, it’s just the way RMVXA works. Worthy of note is the fact that 30×30 is large enough a map size to require you to use the scrollbars (even if you’ve been following this book while using RMVXA full-screen). Make sure your editing mode is set to Map Mode (click the relevant icon or press F5) and then draw a rectangle with the Meadow tile that starts at (0,0) and ends on (23,15).
Tips
As you can see, the tileset for Exterior has different tiles than the Field tileset. Find (11,0) on the new map and the Dirt (Meadow) tile and draw a vertical line to (11,4). After that, take some time to add some terrain and play with the tileset.
Notes
You can find my rendition of the 23×15 area in Figure 2-12. Make sure your road is in the right place (and I’d highly recommend having at least the ledges directly to the sides of that road as well). Once that is done, switch to Event Mode and right-click (11,0). It’s time to create another transfer event.
Figure 2-12. My final map for the second area
Don’t forget to switch the event trigger to Player Touch; otherwise, the player will have to press spacebar or Enter to activate the event. Set the destination to one square south of the exit on MAP001. (You can left-click the map list within the Transfer Player menu to change which map you are looking at.) If you have followed the book up to now, that should be (8,2). Then, left-click MAP001 in the main map screen, to return to the first map. Right-click the Exit event and then left-click Edit. What you want to do is edit the Transfer Player command so that it points to the dirt road in our second map. For the sake of this tutorial, let’s place the destination at (11,2). Right-click the Transfer Player command and choose Edit, to make the appropriate changes. What you don’t want to do is place it right where you have the event to transfer to MAP001, as that has the potential to cause issues.
If you have done all of the above, you are now set. Play-test the game and see what happens.
Note If your character is going through the exit and facing north instead of south, it is because you must manually change the direction he/she faces after said transition. You can do this by changing the Direction setting in the Transfer Event dialog screen from Retain to Down.
Cool! Now, About Those Variables . . .
Okay fine, you’re right. Let’s apply the usage of variables before we end this chapter and move on to meatier subjects. Take a look at your second map. Place five trees (wherever you like, so long as you stay within that 23×15 area we populated with grass) around the area. If you managed to copy the map I created earlier, then the five trees you already have will be perfect.
Note For larger graphics (such as the trees we’ve used) that take up more than a single tile of space, you will have to place each individual tile accordingly. Trees have a top tile for foliage and a trunk as the bottom tile.
What we’re going to do now is have each tree increase the value of a variable by one when the player interacts with them. We have to make sure that the player can’t just interact with the same tree five times. In other words, each tree can only be counted once. So, how do we do this? There are several ways, but I’m going to take advantage of the fact that this is the switches and variables chapter to talk a little about self-switches.
Each event can contain up to four self-switches. While switches and variables have global scope (that is to say, the value of a switch or variable can be accessed from any other part of RMVXA), self-switches are local in scope. The rest of your game’s events care not if Event #441 on Map #72 has its self-switch B (self-switches go from A to D) set to the “ON” state. It only holds importance to that one specific event itself.
So, we need an event that does the following:
With that said, if you like a challenge or have previous coding experience, I invite you to try and figure this one out by yourself. You’ll need to keep in mind the following:
Did you figure it out? If you think you have (or are stumped), let’s resume. The crux of this particular exercise is in those new event commands that I have not explained yet. I wholeheartedly recommend experimentation throughout your use of RMVXA. After all, video game design is basically one long sequence of problems to be solved. (The same can be said of programming.) With that in mind, let’s take a look at the Conditional Branch command shown in Figure 2-13.
Figure 2-13. The Conditional Branch event dialog
Note In Ruby, and programming in general, a conditional is something that happens when a certain condition is met. In this case, we’re talking about a simple if/else condition. For example, if I have more than three cats, I am a cat person; else, I am not.
The Conditional Branch command in RMVXA automatically defaults to the switch option. Click the self-switch bubble and then set it so that the branch check for A is OFF. There’s a check box I didn’t include in Figure 2-13 that’s titled “Set handling when conditions do not apply.” It defaults with a check mark, but I usually untoggle it. Why, you may ask? I find it generally easier to make a pair of conditional branches rather than having to handle the exception (in this case, A is ON) within a single branch. As you work in RMVXA, you’ll realize that my way of working with conditional branches actually results in less clutter. In any case, we won’t need the Else that leaving that check box checked would create, as we don’t want the event to execute anything within that branch if the self-switch is already on.
The second new event command we should take a look at is Control Variables. See Figure 2-14 for an image of the Control Variables menu, as seen when accessed.
Figure 2-14. The Control Variables event dialog
Like Control Switches, Control Variables allows you to manipulate a single variable or a batch (group) of variables at the same time. Unlike Control Switches, there are many more things you can do with a variable. This is mostly owing to the fact that a variable can hold any number, while a switch can only hold an off or an on state. In what may seem as a rather meta thing, you can have a variable be affected by the value of another variable. Final Fantasy uses variables in such a way to great effect. For example, a certain character’s weapon in Final Fantasy IX causes greater damage the more dragons it has killed during the course of the game. While making a weapon stronger based on such a variable requires scripting in RMVXA, you can create a skill that does much the same, and it does not require scripting.
For this exercise, all we have to do is name the variable in question (I called it Tree), change the operation to Add, and set the Operand to Constant with a value of 1. If you leave the operation on Set, it will make the value of the variable 1. You want the variable to gain 1, not stay at 1, when the player interacts with a tree. To summarize, when the player performs an action on the tree the first time, the variable “Tree” will be increased by 1.
Finally, we need to add a Control Self Switch event. Control Self Switch is a simple enough event command that I won’t use an image on it. All you can do with that command is select one of the four self-switches and set its state to ON or OFF. Set self-switch A to ON, and there you have it.
In simpler terms, here’s the solution. Figure 2-15 shows the necessary Priority and Trigger settings.
Figure 2-15. The Priority and Trigger settings for the tree
@>Text: -, -, Normal, Bottom
: : This tree looks rather peculiar.
@>Conditional Branch: Self Switch A == OFF
@>Control Variables: [0001:Tree] += 1
@>Control Self Switch: A =ON
@>
: Branch End
@>
What’s the Point?
There is no greater honor than botany. No? Okay, fair enough. Let’s add a final event that requires the Tree variable to be at 5. We’ll make it a treasure chest, to show off the relevant Quick Event.
Note The default character, Eric, cannot equip the Longbow in his current state. I’ll cover how to allow him to do so in the next chapter.
Check Figure 2-16 to see how the relevant part of the second map looks for me. Besides the five trees and the road, your map will probably look different.
Figure 2-16. The final second area map, showing the events
Before I close this chapter, here’s one final application of variables that will most likely be of great use to you as you work on your RMVXA game.
Advanced Challenge: Using Variables to Handle an Area Transition!
Up to now, we have dealt with area transitions that are one square wide. However, what happens if we make an expansive area, such as a desert, and want to make exits for the player? It could take dozens of events to cover a single direction. For those of you following this book using the Lite version of RMVXA, that is unacceptable (as RMVXA Lite only allows ten events to be placed per map). Truly, it is unacceptable, period, even if you have the full version. Why spend 10–40 events for area transitions on a single map when you can cover them all in one? This exercise requires the use of the following:
First, switch to Event editing mode. Then, find a nice spot in the second map that the player cannot reach. In my case, I’ll be using one of the squares on the mountaintop. Afterward, create a new event at that square. Set the event trigger to Parallel Process. Next, we’ll have to check for the player’s x and y positions at all times. That is the reason why we use Parallel Process. (Remember: Autorun is meant more for cinematic sequences. Trying to use it for an event such as this will only cause the game to hang.) The secret lies in the Control Variables command, specifically the Game Data category of operand. The Game Data category defaults to Map ID. Click the button labeled “...” on the far end of the bar, to change what type of data you want to assign to your variable.
Once you click the button, you’ll notice a slew of possible things you can plug into a variable. What we’re looking for is the player’s position on the map (a.k.a. his/her x and y coordinates). Thus, we click the Character option, which defaults to Player’s Map X. Exactly what we need! Select that, then make sure you’re saving the value to a new variable (I’m calling it variable 2 X and variable 3 Y). Afterward, create a new Control Variable command and repeat the process up to the point where you are looking at the Player’s Map X setting. Click the arrow on the drop-down menu for Map X, and it will reveal four more settings. The one we want, of course, is Map Y.
Here’s where it gets interesting. I’m going to recommend that you lower the map size down to 23×15 (the size of the grass rectangle we made in starting our second map). To be fair, it doesn’t particularly matter for this exercise, but it’ll help you visualize the boundaries in-game. Now what you have to do is figure out the boundaries of each side of the map on which you want to have area transitions. Let me use some Region numbering, shown in Figure 2-17, to help you visualize what I mean.
Figure 2-17. The map Regions for our transitions
We have a single transition marked by the number four to the north. Additionally, we have 11 squares on the left, 21 squares on the bottom, and 12 squares on the right. That would be 45 transition events, if we did them one by one. So, let’s not.
Let’s Crunch Those Map Boundaries, Shall We?
The topmost square of the left boundary is (0,3), while the bottommost square of the left boundary is (0,13). The left-most square of the bottom boundary is (1,14), while the right-most square of the bottom boundary is (21,14). The topmost square of the right boundary is (22,2), while the bottommost square of the right boundary is (22,13).
What does this mean? Well, we can see some trends.
Thus, our conditional branches should read as follows:
@>Control Variables: [0002:X] = Player's Map X
@>Control Variables: [0003:Y] = Player's Map Y
@>Conditional Branch: Variable [0002:X] == 0
@>
: Branch End
@>Conditional Branch: Variable [0003:Y] == 14
@>
: Branch End
@>Conditional Branch: Variable [0002:X] == 22
@>
: Branch End
@>
Keeping in mind that == is the equal sign when used in this context (a single equal sign means that a value is assigned in programming), you can probably tell what is going on. Note how every conditional branch event ends with a Branch End. The first conditional branch covers the left border. The second branch covers the southern border, and the third branch covers the right border.
But You Don’t Have Anything Inside the Branches!
Keen eye! Indeed, you would be correct. Feel free to put in transfer events, as you see fit, to wherever you want them to lead. The best use of such a transition in practice is to send the player to the world map (on which it doesn’t particularly matter where the player exits from, only that he or she exits the area map from a certain direction).
@>Control Variables: [0002:X] = Player's Map X
@>Control Variables: [0003:Y] = Player's Map Y
@>Conditional Branch: Variable [0002:X] == 0
@>Transfer Player: [001:MAP001] (013,006), Left
@>
: Branch End
@>Conditional Branch: Variable [0003:Y] == 14
@>Transfer Player: [001:MAP001] (008,011), Up
@>
: Branch End
@>Conditional Branch: Variable [0003:Y] >= 2
@>Conditional Branch: Variable [0002:X] == 22
@>Transfer Player: [001:MAP001] (004,006), Right
@>
: Branch End
@>
Summary
That concludes this chapter on switches and variables. Here are two more exercises for you to stretch your mind with.
Next, let’s talk about a large part of what makes RPGs what they are: characters and enemies!