# THE BASICS OF VARIABLES

### A tutorial on the basic use of variables used in every game.

So, you wanna learn how to use variables in your game? Well, you are one smart fella, because variables are practically the ONLY way you can make a decent game using almost ANY game engine. So, what is a variable, anyway? Dictionary.com says:

Something that may or does vary.

Simple definition, but it get's more complex. In this tutorial, you will be shown different ways you can use variables which will help you in making future games in RPGMaker.

So let's start! We are going to change one event by talking to another event using one variable. We will need a new room with 2 events. The first is the one that will change the variable, the other will change it's appearance when the variable changes, and if you want, make a third event that screams out that the second event changed. Do all of this so we can continue. Give them graphics so that we will be able to talk to then easily when testing.

With the above done, double click on the first event and enter the event menu. Double click on the rectangle at the right side to enter the command menu. When it pops up, you should see this on the first page:

With the highlighted button shown above, you can control the values of all the variables easily. You're gonna use this button a lot when you make your own game. Click on it to go to the variable menu.

The variable button has three sections. The first defines what variable is gonna be changed, the second defines what change is gonna be made, the third defines by how much it will be changed. Let's look at the first section.

By default, this will be set as "Single 0001:". This means that a single variable, in this case the very first one, will be changed. If you were to choose "Batch", it will select all the variables between two different variables and change those, meaning that if I picked variables 20 and 40, variables 21-39 will be changed. But, for now, we just want to change one variable. Let's change the name of that variable to something we can remember, since numbers can get confusing when making bigger games.

Click on the "..." button a new menu will pop up. When first opening this the default variable selected will be "0001:". If not, click on it. Now, go to the box where it says "Name:" and type in a name. I will be using "changeCharacter".

Click "OK" and you will see that the box where it said "Single 0001:" now says "Single 0001: changeCharacter". We have now have our variable labeled, let's continue to the next section. It looks like this:

You don't need to know all of them to make a game. In my opinion, the last one is pretty useless. The ones you will be using most often in your games are the first three.
Set: Changes the variable to the exact value specified in the third section.
Add: Adds the value specified in the third section to the value of the variable.
Sub: Subtracts from the variable the value specified in the third section.
The other two are Multiply and Divide. The last one, my guess is, Mode, but I have yet to use it, so who cares... (GameOverGamesProductions said that it is actually Modulus. Whatevs...)

The default operation is "Set", and that's what we want it for now. Now, to the third section. I could explain what all of these do, but then this tutorial will get longer than it needs to be. Experiment with them to see what they do. It's better that you teach yourself every once in a while. For this tutorial, we want to change the variable to a "Constant". This means that, no matter what, the variable will always change to 1. Click "OK" and the command will show in the white rectangle in the event menu.

You now know how to change a variable to whatever you please, but we are not finished. You need to learn how to use it now. Event 1 can now change the variable to 1, but what if we want it to set it back to 0? This is what a Conditional Branch is for. I will not explain what is a Conditional Branch since it's hard to explain in a sentence, so let's continue. Double click on the white rectangle and click on the "Conditional Branch" button on the first page. You should see this pop up menu:

Select the bubble that says variable. You will see the rectangles next to it turn white and contain letters and numbers. When you look at it as a sentence, it should read "Variable 0001: changeCharacter is Equal to Constant 0". This means exactly as it reads. Under constant, you can see that you can make it so that it would check if the variable specified is equal to another variable. For now, leave it as is. Make sure that the checkbox at the bottom is checked so that the conditional branch will include an "else" statement. Click "OK" and now, we should have this:
```@>Control Variable: [0001: changeCharacter] = 1
@>Conditional Branch: Variable [0001: changeCharacter] == 0
@>
: Else
@>
: Branch End
@>```
We want the variable to change to 1 when it equals 0 and change to 0 when it equals 1. To do this, we can just cut the Control Variable on the first line and paste it under the branch where it says variable changeCharacter == 0, then paste it again where it says Else and change the value to 0. It should look like this:
```@>Conditional Branch: Variable [0001: changeCharacter] == 0
@>Control Variable: [0001: changeCharacter] = 1
: Else
@>Control Variable: [0001: changeCharacter] = 0
: Branch End
@>```
If you saved this and test it out, you will not be able to tell the difference, so you might want the event to say something when you talk to it. Maybe what number the variable is on. This can be easily done by using the text command and this:
`/v[#]`

This will allow you to see the value of a variable where "#" is the id of the variable, in this case, 0001 or just 1. So make a text command on the white rectangle and type in something like this, perhaps:
`The value of changeCharacter is now /v[1].`

Now when you test the game, you will see that the number at the end of the sentence will change from 1 to 0 and it'll keep looping. Exit the game, we're not finished.

We still want Event 2 to change it' appearance when you talk to Event 1. Double click on Event 2 and look at this box at the left.

You can set whether to show an event or not when one or more of the conditions specified in this box are met. Before we touch this, make a new page on this event by clicking the new page button at the top of the event menu. Now we can mess with the box. On the second page of this event, click the checkbox next to "Variable" and the rectangles next to it turn white with letters and numbers. It should read "Variable 0001: changeCharacter is 0 or above". Since all variables start at 0, and we put no conditions on the first page, we could set the second page to 1 so that this page will show when the variable equals 1. You can place a graphic for page 2 if you want and save this, and if you made a third event that says that event 2 change, you can do that with a Conditional Branch exactly like Event 1's.

When you test the game, you will see that Event 2 will change when you talk to Event 1 and, if you made it, Event 3 will say something different as Event 2 changes. Those are pretty much the things you must know to make a simple, decent RPG when using RPGMaker. Variables can be used for the most tiniest things or for something big and advanced. If you think you need more practice, try out the homework and in no time, variables will be no problem to you.

HOMEWORK:

@>Make a game in which you have to talk to people in a certain order to advance to the next level.
@>Make a simple calculator using the "Input Number" command.
@>Make an event that gives the player a random amount of gold or items.
@>Make a mini-game where the player has to guess a number between 1 and 10.
@>Make a bank system were the player can deposit and withdraw gold.

Happy game making!

## Posts

Pages: 1
Modulus is very important to our modern game making math how dare you condescend it!

Also, if that script fix is still needed to make the as bought and downloaded RMVX do math properly, you might add some information or a link to it here. I don't know if they ever actually fixed it though.

"You don't need to know all of them to make a game." Also I would hope that anyone trying to make a game knows how to use addition, subtraction, multiplication, and division!
Racheal
4111
Modulus (%) gets the remainder when doing division. It's useful for something like getting the x position based on the index in a grid.
x = index % width
y = index / width

That said, I can't say I've ever used it with variables in an event though.
comment=38091
Modulus is very important to our modern game making math how dare you condescend it!

I certainly never used it, so I doubt a newbie would.
Pages: 1