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 a full 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 a full 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!

 

24 comments

  1. reptile614 says:

    I got the game to work, thanks was fun. However, I want
    to make a event to collect certain items. Basically you
    need ‘ X ‘ amount of items to get to next area. Thanks
    in advance for any and all help.

    : )

    • admin says:

      you can set that item to a variable (under Game Data in the variable window, which sets the variable to the amount of those items that the player has in inventory); and then count if the variable is high enough in a conditional branch.

      good luck :)

  2. Lost161 says:

    I was wondering do this work with doors as well?

  3. Lost161 says:

    alrighty….ive followed everything youve put to a t…the chest opens and stays open but my toons are stuck in one direction and its normaly the way he opens the chest….what I dont get is it is set up just like their quick event but its not working the same…what do you think the problem might be. And I was also wondering just how I would set a door up in the same fasion?

  4. admin says:

    I’m not sure exactly what you mean by “toons”. Send me an email or a PM on the rpgmakerweb forums with some screenshots of your events, and I can help you out in more detail. :)

  5. John Scherzi says:

    The tutorials are great. I finished Part 1 of the Switches and Variables and started on part 2. However, the screenshots in part 2 that are suppose to give you a larger image if you click on them don’t all work. The images are so small you can’t make out the lines in the the event and you get the dreaded 404 error message when you click on them. Can you possible send me a copy of the article or just the enlarged screenshot images. Your tutorials and podcasts are great.

    John S

  6. John Scherzi says:

    These were outstanding tutorials. I completed both tutorials and everything worked perfectly. BTW how many nested if statements can you use in the conditional branch in the event menu?

    The reason I am asking is that I wanted to set up conditional branching based on quest sections completed. The grand quest probably has more than 20 different stages to test on. I know its going to be complicated, but I want certain NPCs to react differently based upon how the players have progressed in the quest, so my variable is “quest stage” and will test it value on various events.

    Also, I am trying to balance the monsters and the players (as they progress)in the game. I noticed in my battletests that the 1st level player has 562 HP (he’s a fighter). Is that where players start out more or less? So it seems that the exp. pts to hp ratio of the monsters is about 6% (not including special abilities). Does that seem correct? You rock man.

  7. TheXXXJester says:

    I was wondering the best method for refilling a chest after a certain period of time. Can switches and variables be used in that situation?

    • Despain says:

      Absolutely. You can use wait commands and variables in a common event to keep track of time.

  8. Lawrence says:

    Amazing tutorials. Thanks for this.

  9. Jamie says:

    I am building a mini game where you have to move chess pieces on to switches, when you do they unlock chests. However each chess piece as you know moves in its own way.

    The challange is that you can not get all the prizes unless you move them in a certain order, so that they don’t block each other off.

    My question is how could I do this, as at the moment while they will get blocked the switch that told them to move is still operational an therefore unlocks the chest regardless…

    If that makes sense…

  10. Josh says:

    I put a timer on the minigame. But is there any way to stop the timer when you interact with NPC’s?

    • Despain says:

      There should be an event command for stopping the timer. You can do it from any event and it’ll stop the timer that’s currently active.

  11. Cody L says:

    Hello, I loved your tutorials and I am thankful for how easily you explained some complex processes. I did not find a answer to what I was looking for though, I will try and make it simple as possible. I have 4 Items you have to collect. The person requiring these items gives helpful information on where to find these items. I want him to be able to respond based on the specific items obtained.
    Example:
    “You have collected Orb A, but not B, C, or D.”
    or
    “You have collected Orb C, but not A, B, or D”
    “You have collected Orb D and A, but not B or C.”

    Is there a way to do this using conditional variables?

    • Despain says:

      If there isn’t a specific linear order that he needs to get them in, then using a single variable isn’t the best option.

      Honestly, I think the most efficient way would be to use a series of regular switches in your conditional branches. It’ll feel a little clunky, but gives you the easiest control over tracking what the player has gotten and what he hasn’t.

      • Cody L says:

        I thought that might be the case, thankyou for such a quick response! I have bookmarked your page! It’s gonna be a fun night creating!

  12. dinorush says:

    Hi, these are great tutorials and I feel like I’m learning a lot with them! Though, as is the case with my past experiences with game creation engines, I also feel not quite there yet…
    Messing around with variables, I’d like to know if you could point me in the right direction with further variable implementation. Here’s what I’m trying to achieve: a randomized set of maps and events (these being enemies). The idea is to use an on-map “combat system”, sort of. It plays out in a way that when a player presses the action button against an enemy (an event), the variables drop by a fixed value until they reach 0, a point wherein the event is erased.

    My issue is that I actually managed to do this – thanks to your tutorial! :) The first event page sets the Control Variable to 10, and a Control Self-Switch On. The second page has:
    *Variable Control to -2 with the press of the Action Button;
    *A Conditional Branch wherein once the variable reaches 0, the event is destroyed; ELSE it keeps activating the self-switch so the event can keep registering the variable numbers.

    However, I noticed that while it works for one event, it has obvious problems for multiple ones using the same variable (one of several issues: two different enemies with the same variable leads to problems such as pressing the Action Button four times on one of them, means that pressing the Action Button a single time on the other will kill it instead as both use the same variable; plus, the first one will now be invincible because the variable will go below 0, reaching negatives). My main goal was to have random enemies use this kind of variable, to the extent where the system would allocate a fixed variable number (as faux HP) on different enemy types… but it seems I’d have to create one different variable for every different enemy.

    Is there a way, also through variables, make this work? Thanks in advance, and keep up the good work :)

  13. Damian says:

    I want to know how I can make a pirate sell a boat, the boat cost 5,000 gold.

    • Shotty says:

      Just do a conditional Branch which chekcs if you have 5000+ gold and if you do decreaase number of gold by 5000 and than activate a switch to display the boat.

  14. Lady Beatrice says:

    How can I make a bottle or any object fall when the actor steps on a certain part of the floor?

    Great tutorial by the way! ;)

  15. Lady Beatrice says:

    Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *