Submit Your Article


 
RPG Maker

Welcome Guest ( Log In | Register )


  Games Resources RPG Maker VX RPG Maker XP Scripts Tutorials Downloads

 
Reply to this topicStart new topic
> [Scripting][Series]The Scripter’s Journey Series, 2. All about Variables!
The Law G14
post Jun 15 2012, 09:37 AM
Post #1


Scripter FTW
Group Icon

Group: Local Mod
Posts: 1,346
Type: Scripter
RM Skill: Skilled
Rev Points: 5




The Scripter’s Journey Series

2. All About Variables!


Table of Contents
I. What the *Bleep* is a variable?!
II. Types of values!
III. Variable Manipulation!
IV. Example in the default scripts!
V. Conclusion

Side note: It will be attempted by these tutorials (at least the first few foundational ones) to apply to all RGSS languages (RGSS1, RGSS2, and RGSS3), so it would still be a good idea to read up on these tutorials regardless of what engine you use for your game. So therefore I’ll be saying “RGSSX” to refer to all of them, but the script editor I’ll be using is that of RMXP

What the *Bleep* is a variable?!
A script without variables…that is rare. Variables are possibly the biggest key to your foundational learning in RGSSX. Well, what IS a variable, you ask? Simple. A variable is a way to refer to the majority of the time a value (object), but it can really be anything. Here are some examples: enemy HP, EXP, number of quests to complete, the list goes on. Think of variables as a list of nicknames. When you say “Johnny” you are actually referring to the “John”. Also, an important note, do not think that values and objects go inside or are contained in variables. Variables are simply ways to refer to a value. Now you ask “Why are they called variables then?!” This is because the value that is being referred to is nearly always variable (or changeable and not consistent). For example, in one game enemy HP may be 1000, but in another game enemy HP may be 5, to solve this problem we have one variable that just equals enemy HP since variables refer to a value, in this case, enemy HP.


Types of values!
Let’s open up our Script Editor (you should know how to do this from the last tutorial) and make a new page above Main (again, you should know how to do this). Call this script “Variables” (you should be able to name it by going in the bottom left where it says “Name:” Now, on to the fun stuff, we’re going to make some variables. Copy this code:

CODE
integer = 5
float = 5.225
string = “Five”
boolean = true


These are the major types of values accepted for variables to refer to. First we have integers which are simply whole numbers (like the main hero’s attack power). Next we have floats, which are integers with decimal portions added on. After that we have strings which to explain simply is just text (you must ALWAYS put quotation marks around it to signify it is a string), and finally we have booleans which are simple. A boolean value can either be “true” or “false”. There is another type of value that can be referred to and those are class objects (but we’ll get more into that when we discuss classes). Also as a side note (this can come in handy), you can set your variable to refer to absolutely nothing by using this code:

CODE
nothing = nil


“nil” is a keyword in RGSSX, and use CTRL+SHIFT+F (like discussed in the last tutorial) to find cases of the default scripts using nil.


Variable Manipulation!
Alright delete any code you have right now in your script editor and let’s get back to work. Now that we know what values a variables can refer to (integers, booleans, strings, floats, nothing, and soon we’ll learn about class objects) let’s manipulate these variables and their values. First thing though, I may not have made it clear in the last section, but in order to create a variable you must use the assignment operator which is just a basic equals sign. So take one of my examples from the last section:

CODE
integer = 5


“integer” is the name of the variable, the equals sign means “the variable ‘integer’ will now refer to the value to my right”, which in this case is an integer value of 5. This is how you build a variable, but an important note on naming conventions. ALWAYS make the names of your variables lower case, that is important, and also it is recommended that you make the variable name make sense to you so you know what value is being referred to. Alright, now that that’s out of the way, let’s manipulate these variables. Copy this code:

CODE
five = 5
four = 4
three = 3
# The sum
sum = five + four + three
print sum


Now there are a few things that need to be explained in this block of code. First of all, the comment symbol. Comments do not affect code or execution of the code AT ALL, they are simply used to explain code and help the coder not forget why he wrote a line of code the way he did. To create a comment, simply being a line with a “#” symbol.

Next, we have the print command. Basically all that does is allow you to print to the screen the value to the right of it.

Finally, we have our variable manipulation. We first built three variables that each refer to integer values (3, 4, and 5). Then we added the variables together using the addition sign and due to the fact that those variables now refer to integers, it’s like adding integers together. Now, we make another variable called “sum” refer to the literal sum of the three variables we built prior to that. We end the block of code with us printing the final result to the screen which is 12.


Example in the default scripts!
Alright guys, go to the “Game_Temp” default script and find a line of code that looks like a variable is being built. For me, since I’m in RMXP right now, I found the line of code:

CODE
@map_bgm = nil


Do not worry about the “@” sign, we’ll discuss that in later tutorial when we go over the different types of variables. You also don’t need to know what this means (even though you could infer that this means the map background music starts off being nothing or no music plays in the beginning unless explicitly stated). I just want to show you guys that variables can be found anywhere and they are extremely useful and there are constructed in the same way we discussed in this tutorial:

CODE
variable_name = value



Conclusion
Well, hopefully this tutorial was more interesting than the last one (I’m trying lol). Good news though is that you should have a nice grip on variables and their purpose (along with the types of acceptable values a variable can refer to and manipulating variables). I will talk more about variables in later tutorials because there is still more to cover about them, but we have to learn some other information first before diving in.

The order of how these tutorials will progress: http://www.rpgrevolution.com/forums/index....showtopic=54953

~Law


__________________________

To put in sig, copy this link:
CODE
[url="http://www.rpgrevolution.com/forums/index.php?showtopic=51540"][img]http://img40.imageshack.us/img40/6504/conceptthelawbanner.png[/img][/url]


Sig Stuff


"When you first come, no one knows you. When help them out, they all know you. When you leave, they all love you. When you come back, they've already forgotten you." -- copy into your sig if you think this quote speaks true!

If you are one of the very few teenagers that know what real rap is and don't blindly listen to the hate statements (rap is crap), then put this in your sig. I say this in the name of Common, Mos Def, Lupe Fiasco, 2Pac, Nas, Talib Kweli, Eminem, and many others. -Exiled One

My Project Thread: Gai's Hunters


Go to the top of the page
 
+Quote Post
   
Jonnie19
post Jul 7 2012, 03:52 PM
Post #2


Are you trying to rise from your lullaby?
Group Icon

Group: Global Mod
Posts: 1,311
Type: Developer
RM Skill: Intermediate
Rev Points: 45




I have seen quite a few tutorials on Variables in scripting. However, I still really didn't quite get it. This tutorial however is simple and effective and has explained Variables in a simple yet effective way, without overloading it. Once again brilliant job smile.gif

Although I will point out the link to the "Contents" of these tutorials, is slightly outdated and leads us to other tutorials that you have posted on the main site. Whether that is purposefully done I don't know but I thought I'd mention it still....

Another note....where is the Pokmon references... that is very disappointing wink.gif


__________________________

Finished Projects:
Slenderman's Army:


Go to the top of the page
 
+Quote Post
   
The Law G14
post Jul 14 2012, 03:55 PM
Post #3


Scripter FTW
Group Icon

Group: Local Mod
Posts: 1,346
Type: Scripter
RM Skill: Skilled
Rev Points: 5




Thanks again Jonnie! Glad you found this clear to understand! Ah and yea I'll fix up that Contents page so it can relate directly to the flow of these tutorials. And lol, yea the pokemon thing kind of got faded because like I mentioned I beleive in the conclusion of the last tutorial it was just to add some spice to the first tutorial and I realized it would be a huge headache to to keep writing this side play story that might confuse the reader lol but anyway thanks a lot really appreciate it smile.gif


__________________________

To put in sig, copy this link:
CODE
[url="http://www.rpgrevolution.com/forums/index.php?showtopic=51540"][img]http://img40.imageshack.us/img40/6504/conceptthelawbanner.png[/img][/url]


Sig Stuff


"When you first come, no one knows you. When help them out, they all know you. When you leave, they all love you. When you come back, they've already forgotten you." -- copy into your sig if you think this quote speaks true!

If you are one of the very few teenagers that know what real rap is and don't blindly listen to the hate statements (rap is crap), then put this in your sig. I say this in the name of Common, Mos Def, Lupe Fiasco, 2Pac, Nas, Talib Kweli, Eminem, and many others. -Exiled One

My Project Thread: Gai's Hunters


Go to the top of the page
 
+Quote Post
   

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 24th May 2013 - 03:30 AM
RPG RPG Revolution is an Privacy Policy and Legal
eXTReMe Tracker