Switches and Variables (Part Two)

Des

timefantasy.net
Veteran
Joined
Mar 2, 2012
Messages
1,370
Reaction score
508
First Language
American
Primarily Uses
N/A
RMVX Ace: Switches and Variables, Part 2

Introduction

Howdy! Uncle Despain here, with the second part of my tutorial on RPG Maker VX Ace's switch and variable functions.

The

previous tutorial explained what switches and variables are, and then we walked through the creation of a treasure chest event. We used switches and self switches, and learned about event pages and their conditions. In this part of the tutorial, we're going to expand on that knowledge by using variables. If you haven't read part one, check it out now, because this tutorial builds off of the elements introduced there.​



The Chicken Game

In this tutorial, we're going to create a minigame where the player must collect chickens, and we are going to use a variable to keep track of how many chickens he has picked up. We'll start by setting up the chicken event:









(click the image for afull view with animation)




The event right now has a simple two-page setup. The first page is the chicken itself—when the player walks on top of the chicken (note that the priority it set to "Below Characters" and the trigger is set to "Player Touch"), the chicken will disappear. In this case, we use a self switch to make the chicken disappear when the player touches it. Page 2 of the event is blank, and it is activated by the self switch.

Now, in order to turn this chicken into a minigame, we're going to need a way to keep track of many chickens the player has caught. In order to do this, we can use a variable.

Adding to a Variable

On the first page, insert a new event command on the line before the self switch is activated. In the "Game Progression" command category, select "Control Variables". This is similar to how you set up a switch before, but you'll notice that the "Control Variables" window is a lot more complicated:









The top part should be familiar: you can choose to modify a single variable of a range of them. If you click on the "..." icon, you'll see another familiar window:









The variable list is the same as the switch list that we looked at in part one. Again, you can name each variable. For our example, I'm going to give the variable a suitable name, and then OK back to the "Control Variables" window.









I briefly explained in the first part of the tutorial that a variable is a number: when you modify a variable in this window, you will be manipulating the number that is store into at variable. In this tutorial, the variable 001, "Chickens Caught", will keep track of the amount of chickens that the player has picked up.

The "Operation" section gives you a choice of how you want to manipulate the variable. The first option, "Set", will directly set the variable to the number of your choice. For our example, we want to select the "Add" operation (the other options are other mathematical operations: subtraction, multiplication, division and mod/remainder).

The "Operand" section is where you define what number you want to manipulate the variable with. You can define a specific number with "Constant", or you can pull numbers from other variables, random numbers, or from game data. For our example, simply choose a constant number of one.

When you OK back out, your event contents should look like this:









This means that whenever the player runs over the chicken, the event will add 1 to the value of the "Chickens Caught" variable. Remember that by default, the value of every variable is zero.

Displaying a Variable

Remember that we used a self switch to hide the chicken after it's caught? We did this so that we can copy and paste the chicken event as many times as we want without having to edit every one. Go ahead: paste a few copies of the chicken event.









Then go ahead and give your game a test play; try to gather up all the chickens. If you press F9 during the game to open the switches and variables debug menu, you can see that the variable will accurately reflect how many chickens you have caught:









Now, the next step is to do reflect that number within the game itself. Let's do that by creating a new event, an NPC who will keep track for the player.

Make an event with a character graphic, and insert a new event command: choose "Show Text" from the "Message" command category:









One of the really cool things about RPG Maker VX Ace's "Show Text" command is the versatility of special codes that can customize the message. In this example, I use color codes (\c[3]) to separate the speaker's name from his words.

More relevant to this tutorial, however, is the code for displaying the contents of a variable: \v[n], where n is the number of the variable. In this case, the code will display the number of caught chickens within the farmer's dialogue.

Check it out in-game:









(click the image for afull view)


Congrats! You've finished the basics for the chicken-catching minigame using variables. You should be comfortable creating and manipulating variables and displaying them in messages. You can also use them in the event page conditions, like we did in the switches in part one of this tutorial.

We're almost done with this tutorial, but there's one final touch to make our chicken game perfect.

Using Conditional Branches

The way that the event is set up right now, there's a grammatical problem if you've caught only one chicken. The farmer will still say "chickens". If we want to polish this event, there's a quick way to do it by using a really useful event command: the conditional branch.

In your farmer event, insert a new event command. "Conditional Branch" is under the "Flow Control" command category.




A conditional branch creates a fork within your event's contents; it is functionally an if-then condition (the check box at the bottom, "handling when conditions to not apply", creates an alternate branch that makes it if-then-else). In other words, you can have your event react different depending on different game scenarios. The options should be somewhat familiar: you can check whether a switch or self switch is on or off, you can check the value's relation to a set number or to another variable's value, and plenty more.

Let's put it in action: Select the "001: Chickens Caught" variable that we've been using. In this case, we want to give the farmer a different reply when the player has caught exactly one chicken, so you'd set up the branch to check if our variable is equal to the number 1.

When you OK back out, make sure to move your original text so that it is in the "else" half of the branch (by copying and pasting it), and then create another message inside the first condition. It should look something like this:




Try it out by test playing your game. Talk to your farmer after catching only one chicken, and then again after a few more.

You can also next multiple conditional branches inside each other. I decided that I would add another branch to give the farmer something to say before we've caught any chickens.




You can take this even further; maybe you want the farmer to give you a reward if you catch more than 5 chickens—and then, using a self switch, you can make him simply say thanks if the player talks to him again after that. Give it a try.

Conclusion

That's it for part two of this tutorial. By now you should be comfortable with a basic understanding of variables and conditional branches, in addition to the material that we covered in the first part of the tutorial.

Use what you've learned and don't be afraid to dive deeper into the program and experiment. Thanks for reading. Have fun!

For more tutorials check out my website at finalbossblues.com.
 
Last edited by a moderator:

jaypee

hobbyist
Veteran
Joined
Mar 13, 2012
Messages
160
Reaction score
28
First Language
english
Hi needles is there a way to read a variable without going to such conditions, hope I can explain it properly...for example:

a simple straight story:

character do something - +1

character do something again - +1

character do something again the third time - +1

now when he go meet the pharmacist it would check his deed points... if >3 he would get a potion if <3 he goes home empty handed.
 
Last edited by a moderator:

Des

timefantasy.net
Veteran
Joined
Mar 2, 2012
Messages
1,370
Reaction score
508
First Language
American
Primarily Uses
N/A
The first part of the tutorial covers event pages and conditions—and you can use those very easily with variables, too. Might want to check that out. :)
 

Mako

Veteran
Veteran
Joined
Mar 13, 2012
Messages
275
Reaction score
47
First Language
English
Primarily Uses
Alright, firstly: Stop being so helpful. Your making it very hard to be helpful around here when your all over the place helping people.

Secondly, its not cool enough to use pics? no. You have to you animated pics!

Lastly, quit being awesome and let us lower beings in to help!

***

All kidding aside I've never seen such an active member on a forum before. Good job needles. -_-
 
Last edited by a moderator:

Marty

Villager
Member
Joined
Mar 13, 2012
Messages
11
Reaction score
2
First Language
english
Primarily Uses
Nice job, this is help me out to learn more about what VXace can do. thanks for putting it up for us all.
 

AstoXx

Subconscious Punmaster
Member
Joined
Mar 14, 2012
Messages
469
Reaction score
76
First Language
Logic
Primarily Uses
Damn you, Needles! I was going to do this, too but you went ahead and super-ninja'd me. You sneaky sneak, you. Although variables, if you know how to use them, have so much potential. So far in making the first chapter of my game, I have used 2 switches and 18 variables. And some of those variables are for plot progression.

I love variables so much. Starting to be able to use them for crazy crap was a magical moment for me and the real point I think I could finally answer when people ask me what my main role was. I could get that smug look on my face and go, "Oh,me? I'm an EVENTER! Frick, yeah!" Keep up the excellent work, though.
 

Des

timefantasy.net
Veteran
Joined
Mar 2, 2012
Messages
1,370
Reaction score
508
First Language
American
Primarily Uses
N/A
There's no such thing as an "eventer". Knowing how to use events is just knowing the program. Everybody on these forums should be an "eventer"—that's the goal of mu tutorials. It's not comparable with a resource skill.
 
Last edited by a moderator:

AstoXx

Subconscious Punmaster
Member
Joined
Mar 14, 2012
Messages
469
Reaction score
76
First Language
Logic
Primarily Uses
WHile the ideal is for everyone to be able to understand and use the in's and out's of what events can do, alas, however, that is not the case. Not everyone would be able to grasp all the intricacies of event systems easily, some people ask for scripts for stuff they can event around and a lot just don't take the time to practice and try to improve. This is also the case for mapping, too. Everyone should be a "mapper" and learning to make half-decent maps only requires a little bit of practice. I'm not saying it's on par with something like scripting and spriting, etc. but until absolutely everyone is capable of not only understanding why complex event systems work but are able to visualise and make their own, eventing is a niche skill that only some can do effectively.

...Also, I don't really have any other skills on par with what people can do except this. Don't take that from me, dude. :(
 
  • Like
Reactions: PK8

Joeri

Villager
Member
Joined
May 4, 2012
Messages
34
Reaction score
1
First Language
Dutch
Primarily Uses
Heya Super Mr. Satan, thank you so much for this tutorial. I just followed it step by step and finally understand how variables work :) Finally i can start working on my first game now that i know this as well. Thank you so much!
 
  • Like
Reactions: Des

Kyouki

Veteran
Veteran
Joined
Nov 14, 2012
Messages
64
Reaction score
0
First Language
English
Primarily Uses
Is there a way to make a chest disappear immediately using variables and switches once used?
 
Last edited by a moderator:

Des

timefantasy.net
Veteran
Joined
Mar 2, 2012
Messages
1,370
Reaction score
508
First Language
American
Primarily Uses
N/A
Last edited by a moderator:

Kyouki

Veteran
Veteran
Joined
Nov 14, 2012
Messages
64
Reaction score
0
First Language
English
Primarily Uses
Just follow part one of the tutorial. Instead of leaving the open chest on the second page, just make the graphic blank.
I did. but the chest is kinda in the way...so i need it to disappear literally immediately. also, I came back into the room on another save file that came before I put in the chest and it's still there. I took it out and did a basic treasure chest insert then made the page two with the condition that the switch is on, which of course it is, with no graphic and it's still there when i leave and come back.
 

Des

timefantasy.net
Veteran
Joined
Mar 2, 2012
Messages
1,370
Reaction score
508
First Language
American
Primarily Uses
N/A
That makes no sense. Show me a screenshot of your event pages.
 

Kyouki

Veteran
Veteran
Joined
Nov 14, 2012
Messages
64
Reaction score
0
First Language
English
Primarily Uses
O. M. G. I'm an idiot. I forgot to delete the second page that just showed the chest as opened. It's gone now. I feel so stupid. Sorry for bothering you!!
 

Des

timefantasy.net
Veteran
Joined
Mar 2, 2012
Messages
1,370
Reaction score
508
First Language
American
Primarily Uses
N/A
Glad you got it figured out.
 

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

Latest Threads

Latest Profile Posts

Karen: Hi, I have a condition that prevents me from wearing a mask. :)

Employee: Can you tell me what it is?

Karen: >:(
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.

Forum statistics

Threads
99,574
Messages
966,813
Members
131,232
Latest member
Bektos
Top