Hopefully if you're reading this you have a good grasp on variables and switches.
Now, I want to bring up the lesser known Global Value.
If you understand scripting, this tutorial may not offer you much, as it introduces simple scripting ideas. You will already know most of what is in here. But for those who don't know much of any scripting, here is your chance to learn easily learn some basics.
Let me start off with the global value, and touch on it's brethren.
As you should know, a Variable is something that holds a number. A switch is something that holds a value of ON or OFF. (true/false)
What you may not know is variable is a bit misleading. Both a switch and a "variable" are variables. The variable that holds only numbers is more specifically a fixnum. It holds values of numbers, whereas a variable can hold any data (words, numbers, true/false,etc.) and is able to be changed. I need to teach you this because I need to separate them for you.
From now on, think of variables as something that can hold any kind of data.
Great! You're set. So lets move on to how to use these variables.
First, we gotta decide on where we can and can't use these variables. We'll call where we can use them local, class, and global. Explaining local and class would require me to go into more scripting detail than you'd like, so we'll skip them. Suffice to say, you won't be using them in this tutorial.
That leaves Global. Global variables can be used anywhere. That's great, cause then we can tell the game hey, lets make a global variable at level 1 then at level 20 we can still reference that variable. The drawback is that at any time it can be changed, so you have to be careful with it. Compatibility issues may arise if you name one variable, forget, and create another variable with the same name and overwrite your old variable. So always name your variables with care and thoughtfulness.
Now, the proper way to set up a global variable is like so:
The $ always starts a global variable. "variablename" is where you put the name of the variable.
So, $apple could be a good variable name. Especially if you're trying to keep track of information about an apple.
Lets say we want to make that variable hold how many apples we have. The proper way to do that would be:
$apple = 10
But hey, how do we even do this in our RPG Maker program? Well, there should be an Event called "Script..." We just put it on one line in the space it gives us.
Now, we have told the game we have a global variable called apple. It's value is 10. To us, that means we have 10 apples.
But what use is this?
Well, lets say we want to give apples away.
So, if we talk to someone and give them an apple, we'd reduce apples by 1.
$apple = $apple - 1
It may be beneficial at this point to learn a little bit of basic programming somewhere.
(Putting this tutorial on hold. I feel it could be very beneficial for people starting to get the hang of Events and Scripts so they can integrate very low level scripting into their work. This allows for much more robust game play. However, I'm not sure how difficult the concepts ahead will be, and how to explain them without explaining a class period worth of material.)
Group: +Gold Member
RM Skill: Undisclosed
Just want to point out that, programming-wise, using global variables is considered bad design, unless you really have no choice. In case of RPG Maker, you already have objects globally accessible you could use to hold new data: Game_Temp ($game_temp) for temp data and Game_System ($game_system) for data you need to save. Since, in your example, the global variable would not be saved, on a pure design point of view, it's better to put an attr_accessor :apple within the Game_Temp definition and then call $game_temp.apple = 10 from the call script event command.
There's also the fact that, for numerical values, you can use normal event variables without any problem unless you need to manipulate float, binary, octal, hexadecimal or imaginary numbers.
FRACTURE - a SMT inspired game (demo) made by Rhyme, Karsuman and me. Weep and ragequit.
Yeah another problem with the tutorial is the design issues surrounding global variables. I briefly touched on it with the fact that globals can clash, and overwrite easily. They are the only variables that can be used in script calls for the Events though. (Or so I understand, since class and local variables would have unknown room to work in.)
But I wasn't aware of the Game_Temp and Game_System being setup like that. That's very helpful. Since I just started using global variables I could easily rewrite the code I've started, and make the change. Infact, I hadn't even considered variables that need to be saved on load and those which are reverted to default.
This post has been edited by Titanhex: Aug 9 2011, 11:44 AM