Bookmark RPG RPG Revolution! RPG RPG Revolution RPG Maker VX.com
 
RPG Maker
 

 Username:
 Password:
   Not a member? Register!



Home > Tutorials > RPG Maker 2003 > A Beginners' Guide to Switches, Variables, and Conditional Branches

A Beginners' Guide to Switches, Variables, and Conditional Branches


Beginner's Switches, Variables, and Conditional Branches

Introduction

Author’s note: Throughout this series of RM2K3 tutorials, you will find loads of information that you can undoubtedly find elsewhere on the web. However, instead of merely presenting a solution for the reader to copy directly into their game, these RPG Maker guides are centered on helping the reader to understand how and why these solutions work, effectively enabling them to create advanced code on their own. However, I do tend to ramble on at times, and you may end up getting all sorts of information that doesn’t quite relate to the topic you are reading, but is useful to know nonetheless. Also, these tutorials are not only directed towards those beginning with RPG Maker, but to anyone who feels they could benefit from reading them. If you have any questions or comments, please feel free to email me at goldengriffon@gmail.com.

Note: There are several different translations of RPG Maker, and therefore the names and expressions I refer to might be different on the version you are using. However, most terms are at least similar to each other, so you should be able to figure it out.


Contents

1. This Tutorial
2. What Are Switches and Variables?
3. What Are Conditional Branches?
4. Using Switches
5. Using Variables
6. Conclusion

This Tutorial
For those who have mastered the basics of RPG Maker 2003 and want to continue on the path to game development, learning about switches, variables, and conditional statements and branches is an essential step. Knowing how to use these well will form the basis for nearly everything else that you want to put in your game, and expand your ability to create custom RPG systems. This tutorial is designed to teach what switches and variables are, how to use them in conditional branches, and how they can be used to make things happen in your game.

What Are Switches and Variables?
Essentially, a switch is just like it sounds, a value that can be switched either ON or OFF. They can be used to keep track of whether a character has finished a certain quest, walked inside a certain building, or if an enemy is dead or alive. If you open up an event, go to the event commands and click "Switch Operations" (page 1, column 1, row 6), you will see that there are one hundred empty switches there, just waiting to be used. Each switch has its own number but can also be given a name, making it easier to find out which switches you've used for what.

Variables are a bit harder to explain. They are like switches, but more advanced, as they can hold numbers rather that just be ON or OFF. Think of a variable as an empty slot, waiting to be filled with some sort of value.

picture
Each empty slot has a name, like a switch, and can hold any integer value, meaning negative and posite numbers, but no fractions or decimals.

picture
Variables can be used to keep track of how many times you've talked to a certain character, how many steps have been taken, the X coordinate of a character, a map ID number, a character's current HP, and other in-game values you might need.

What Are Conditional Branches?
A conditional branch is what you use to check if a switch is ON or OFF, or to check whether a variable is equal to a certain number. Depending on whether the condition is true or false, certain things may happen. Example:

-If switch is ON, make the hero say something.
-If switch is not ON, teleport hero to a different map.
-If variable is 5, make monster attack hero.
-If variable is not 5, kill hero and take player's money.

Without conditional statements, switches and variables wouldn't actually DO anything, they would just sit there and hold values. Before we can experiment with conditional statements, though, we have to learn to change switches and variables.

Using Switches
For our first example of a using a switch, we will create a man who will change a switch on and off and another man who will tell you whether the switch is on or off.

Make an event somewhere, title it "Change Switch" and give it a graphic. Now, we want this event to change a switch ON when we click on it, so the first thing we need to do is to choose a switch to use. Open up the event commands and find "Switch Operations". You will be confronted with a window that looks like this:

picture

Since we only want to change one switch and we only want to turn the switch ON, leave the current settings as they are and click the "..." beside "Single Switch". The "Switches" dalog box will appear, where you should select switch #1 and name it something like "Sample Switch". Click OK to exit the switch settings, OK again to exit the operation settings, and notice that in the commands box there is now a command reading "<> Switch Operation: [0001: Sample Switch] ON". That's all you need this event for, so click OK to exit out.

Now that we are able to turn the switch ON in-game, we need to make an event that shows whether the switch is ON or OFF. Create another event, give it a graphic, and name it "Conditional Branch". In the event commands box, open up the commands options and find "Conditional Branch" (page 3, column 2, row 5).

picture

These are the options available that we can check. Since we're trying to see if the switch called "Sample Switch" is ON, we will make sure the "Switch" option is selected, "Sample Switch" is the variable being used, and it's checking to see if it is ON. The option at the bottom that says "Execute Custom Handler if Condition Not Met" is just an advanced-sounding way of saying "do something if the condition is FALSE". We want the event to inform us if the switch is not ON, so check that option. Click OK and you will get some code that looks like this:

Code:
<> Branch if Switch [0001: Sample Switch] is ON
<>
: Else Handler
<>
: End


Now, everything we put underneath the "<> Branch if Switch [0001: Sample Switch] is ON" will only occur IF "Sample Switch" is on. So underneath that let's display a message using "Message Options" that will say "Sample Switch is ON."
Anything we put under the Else Handler will only happen if "Sample Switch" is NOT ON. (In other words, OFF.) So underneath that we'll display a message saying "Sample Switch is OFF." Click OK, and now your switch and conditional branch are ready. Test play your game and talk to the Conditional Branch guy. He should tell you that Sample Switch is OFF.
picture
Next, go and talk to the guy who turns the switch on. Nothing will happen right away, but go and talk to the other guy again. If all goes well, he should now tell you that Sample Switch is ON.
picture
If you got it right, congratulations! There's your first successful switch operation and conditional branch. If you didn't get it right, welcome to what RPG coding is all about. You will undoubtably spend much of the time you use to code something searching for and fixing bugs. Sometimes your error might be blaringly obvious, such as forgetting to turn the event to "Action Key", or it might be more subtle, like selecting the wrong variable to check in your conditional branch. Go look through your code, find what went wrong, and fix it. When you've got it right, proceed to a the next section.

Using Variables
Our example of variable usage will be similar to the switch example in some ways, but instead of having only two messages, we will make an event that says 4 different things, changing his message each time you talk to him. Before we start, let's make a plan of what we want the event to do and how it will do it:
-First, we will check to see what the variable number is.
-Next, we will display a message depending on what the variable number is.
-Finally, we will add 1 to the variable number so that the message will be different the next time.

So to start, we will make an event named Variable Stuff and give it a graphic. In the event commands box we will first name the variable we are going to use by going to "Variable Operations" (page 1, column 1, row 7), selecting the "..." beside "Single Variable", and typing in something like "Sample Var". once we have it named, we can exit out of the variable options box, because we don't need to do anything to it yet. Right now we need a conditional statement to check what number the variable is at currently, so go to "Conditional Branch", and this time select "Variable". Since we want to check whether it is equal to 0, select "Number" and set it to 0, and make sure the comparing option is set to "Equal to". Uncheck the custom handler option at the bottom, since we will have multiple cases that could happen if the variable is not 0. Click OK, and inside the conditional statement, display a message: "This is the first of four messages." Since we want 4 different messages, we will have to make a new conditional statement and message to check if the variable is 1, 2, and 3. When you complete this, your code might look something like this:

<>Branch if Var[0001:Sample Var] is 0
<>Message: This is the first of four messages!
<>
: End
<>Branch if Var[0001:Sample Var] is 1
<>Message: This is the second of four messages!
<>
: End
<>Branch if Var[0001:Sample Var] is 2
<>Message: This is the third of four messages!
<>
: End
<>Branch if Var[0001:Sample Var] is 3
<>Message: This is the fourth of four messages!
<>
: End
<>

Now that we've finished checking to see what the variable number is and displaying a corresponding message, all we need to do is add 1 to the variable so that it displays a different message the next time you click the event. Go to "Variable Operations" again, select the single variable "Sample Var", select the "Add" operation, and under "Operand"select "Number" and set it to 1. Click OK and this will add "<>Variable Oper: [0001: Sample Var] +, 1" to your code. The event should be ready now, so test play your game and talk to the event. The first thing it should say is "This is the first of four messages!". Talk to it more times and you should be able to run through all four messages. You will notice that after the fourth message is displayed, it will no longer say anything when you talk to it. This is because the variable has been moved above 3, and no longer is equal to the 0, 1, 2, or 3 your event needs to display the message.
Again, if you didn't get it right the first time, go back and find out what went wrong. I guarantee this won't be the only time you find yourself having to do this, so you may as well get used to it, right?

If you still don't understand why the event is doing what it's doing, I'll go through it again step by step.

You talk to the event the first time.
Sample Var is currently set to the default number, 0.
The event recognizes that Sample Var is 0, and proceeds to display the message "This is the first of four messages!".
The event recognizes that Sample Var is NOT 1, 2, or 3, so it skips those conditions.
The event adds 1 to Sample Var's current value.
You talk to the event the second time.
Sample Var is currently set to 1.
The event recognizes that Sample Var is NOT 0, so it skips that condition.
The event recognizes that Sample Var is 1, and proceeds to display the message "This is the second of four messages!".
The event recognizes that Sample Var is NOT 2 or 3, so it skips those conditions.
The event adds 1 to Sample Var's current value.
You talk to the event the third time.
Sample Var is currently set to 2.
The event recognizes that Sample Var is NOT 0 or 1, so it skips those conditions.
The event recognizes that Sample Var is 2, and proceeds to display the message "This is the third of four messages!".
The event recognizes that Sample Var is NOT 3, so it skips that condition.
The event adds 1 to Sample Var's current value.
You talk to the event the fourth time.
Sample Var is currently set to 3.
The event recognizes that Sample Var is NOT 0, 1, or 2, so it skips those conditions.
The event recognizes that Sample Var is 3, and proceeds to display the message "This is the fourth of four messages!".
The event adds 1 to Sample Var's current value.

Conclusion
Get it? Maybe, maybe not. It's tough to grasp if you've never done anything with programming before, but if you're having trouble you may want to check out a few other tutorials and see if you can understand them better. For practice, and to test whether you can do some stuff with this on your own, try modifying your variable event to make it so that it gives you a random message each time you talk to it, or try making it so that it will start over again once you've seen all four messages. These are only the fundementals of Switches and Variables, but again, you have to no the basics if you want to continue to the advanced stuff. If you're wondering what to start learning next, I would suggest finding out how to use all the event commands of RPG Maker, as they will expand your abilities the most. 
Details
Tutorial: A Beginners' Guide to Switches, Variables, and Conditional Branches
Date Listed: Fri, 27 Jun 2008 13:40:49 -0700
Author: GoldenGriffon
Total Hits: 931


Embed
Short URL:

HTML:

BB Code:



RPG RPG Revolution
RPG RPG Revolution is your #1 stop for game development and console RPG games, as well as those created by people like you. Link to us to support us, so we may grow to be better website community for you.

RPG RPG Revolution is an Privacy Policy and Legal