|
> |
| Home > Articles > Tutorials > Ruby Game Scripting System (RGSS) > RGSS For Dummies Tutorial 2: Variables
|
|
RGSS For Dummies Tutorial 2: Variables
|
|
Author: RPG
Updated: September 04, 2007
|
|
Introduction
|
|
Forget all about the first tutorial (the basics), I only wrote it to give you a
general idea about scripting/programming. You need to understand the example
from the last tutorial though, you need to know that ( print stuff ) displays
stuff on the screen (in RMXP's case, it displays a message box) and that ( #
stuff ) is a comment ignored by the compiler, it's just there to remind you of
stuff or comment stuff out. Enough with stuff, let's get to some fun stuff.
Variables are storage places for your stuff, you stuff your stuff in variables
and then use them for other stuff. Did I mention that the Finnish word for stuff
is Juttuja? And no, I'm not Finnish but I thought it was some interesting stuff.
|
|
Contents
|
|
1. Values
2. Advanced Note
3. Variables
4. The Need for Variables
5. Assignment
6. Variable Fun
7. Conclusion
8. Summary
|
|
1. Values
|
|
There are cases (many of them!) where you'd need to store some value in the
computer's memory. But before discussing how or why would we do such a thing,
let's try to understand what a value is. Most of the time, the value is a
number. It could be someone's age, an enemy's HP, an ID number, a random number,
number of saves, etc. The values of those 'variables' (they vary from person to
person, enemy to another, game to game, etc.) could be 34 for age, 9999 for
Enemy HP, 1921 for ID, 0.3563 for random numbers, 3 for saves. So basically, a
numeric value is a number! Isn't that awesome? Now, a number could be an integer
or a float. An integer has no fraction, like: 1, 34, -57, 9834, 0, -346.
Integers are useful because you don't need fractions often, you can't have 3.5
living cows for example. Most of the time you'll be dealing with integers.
(Actually, the variables used in RPG Maker XP/2K/3's point scripting are always
integers) Sometimes you might want to use fractions, if you and your friend had
one apple that you wanted to share, each of you would get half an apple. If
we're using integers, you can't divide the apple so one would get the whole
thing and the other won't get anything. We need to use fractions so that each of
you get 0.5 (1/2) apple, and we use floats to represent such values. I can't
think of many situations where you'd need to use floats in programming, they're
useful in programs where you need to get scary things like angles, circles,
flying cows and other stuff that gets you an F in school.
A special case of integer values are Booleans, values that can be either on or off, true or false.
They are like RPG Maker XP/2K/3 switches, you either opened the chest or didn't,
talked to the king or didn't, killed the cow or didn't. I said they're a special
case of integer values while they are not, but thinking of them as the integers
1 and 0 could be useful. 0 could be off (false) and 1 could be on (true), but
Boolean values aren't integers and you can't add two Booleans for example.
Basically, the value of a Boolean is either true or false. You can either turn a
light on (true) or off (false), and your nose could be green (true) or red
(false), etc. The real use of Booleans is to evaluate expressions, which we'd
discuss in the 3rd tutorial. (next one)
The final type of values that we are
going to discuss here is strings. A string is basically a string (array) of
characters, but that doesn't really tell anything, does it? Basically, a string
is a sentence, or maybe a group of them, or maybe just some random words. Just
think of strings as text, you might need to store the someone's name, some
messages, etc. Strings in Ruby (RGSS) are placed within quotation marks (either
' ' or " "), some examples of strings would be: 'Alex RTPson', "Hello!", 'I had
a big monkey and it was funny', "sretpok jifrjio uhure", 'abc'. It is
recommended that you use single quotes ( ' ' ) whenever possible, I'd rather use
double quotes myself (because most programming languages do) but using in Ruby
isn't very efficient because the compiler (RMXP in this case) will need to check
more things and that might slow things down. Back to strings, you store text as
string values, you can also add some strings together, for example 'I like ' +
'cows'. Enough said.
|
|
2. Advanced Note
|
|
I'll often use the term 'store' to refer to how variables and values (objects)
interact. In Ruby's case, the variable doesn't really store the value but rather
refer to it. The value is stored in the memory and a variable or more refer to
it. A variable is a reference to some value and two variables might refer to the
same value and changing one variable's value also changes it in the other.
However, I think using 'store' might make variables easier to understand for
beginners, but that's just me.
|
|
3. Variables
|
|
Now, sometimes we might need to store a value for later use. We use variables to
do that. A variable is basically a name (identifier) that represents a value
stored in memory. For example, my_age could be a variable that stores your age.
You can later use variables as you use their values; for instance, you can do
arithmetic operation on numeric variables. In Ruby (RGSS) variables are all
objects, but you don't need to worry about that for now. Keep in mind that
variable names (identifiers) can't start with a capital letter for some reason
(except in certain cases), here are some valid names: age, enemyHP, id_number,
RaNdOm, num_of_saves. Variable names are also case sensitive, password and
passworD aren't the same thing. Here's a little example of variables:
number1 = 9
number2 = 1
number3 = number1 + number2
print number3 # 10
|
Basically, we declare two variables (number1 and number2) with values 9 and 1.
We add the two variables and store the sum in a third variable (number3).
Finally, we display the value of the third variable (number3). You can test it
yourself, make a new event, go to the third page of the command list and paste
the example. When you activate the event you should get a message box with the
number 10. (another way to do it is adding a new section to the script editor
and pasting that, you'll see the message box when you start the game) As you can
see, variables act just like their values. The same example could be written as
( print 9 + 1 ). Also notice the comment ( # 10), it's on the same line as the
print command. You can always do that, as long as it's after the code. In this
series of tutorials, I'll always place a comment with the display value after
the print command (function), there are times when you don't know the output
value though... actually, you use variables because you don't! Our example is
really stupid actually, why would we need to store these values in variables if
we can use them directly? Why use variables?
|
|
4. The Need for Variables
|
|
There are cases where you don't really know the values you're dealing with.
Maybe you wrote a program that asks the user for two numbers and then displays
the sum. The user might type any two numbers, you can't just guess the numbers
or anything. So, if we store the number (without knowing what the number is) in
a variable, we can operate on it with ease. Another example is the stats of the
hero in some game, you can't always know what the HP, level, strength, etc of
the hero is, and you might not be able to know how many potions does the party
hold. Why not? Because the game stats vary from time to time and from a person
to another; we can just store the hero's HP in a variable and then use it as we
please. A final example of the uses of variables would be constant values that
you change, sometimes you know the value of something (someone's age for
example) and you use that value heavily in your code. But what if the value
changed? (that someone's birthday came and they're one year older) You'll have
to change all the occurrences of that value in your code. Note that the value is
constant (it doesn't change during the execution of your program) but it could
change outside your program, so you just use a variable and change it's initial
value whenever you like. As we can see from the previous examples, variables are
used for values that 'vary'. In other words, variables are used to store
'variable' (changing) data. It might not be very clear at the moment, but you'll
see how important they are as you learn more.
|
|
5. Assignment
|
|
Assignment refers to assigning a value to a variable, i.e saying that this
variable stores this value. We use the equal sign for assignment, it might look
mathematically incorrect as in the following example:
It doesn't mean that my_var is equal to my_var + 7, it means that we calculate
the stuff on the right side of the equal sign and then store it in the variable
on the left side. So, think of the equal sign as an assignment (assign some
value to some variable) rather than an equality. The variable is declared (the
program knows about it) when you assign some value to it, here are some examples
of assignment:
cow = "Hello this is some random text"
magic = 7 + 13 - 9 * 100 / (1 - 3)
teh_var = true
heroHP = 9999
|
And some different ways to assign things:
cow = magic = teh_var = 3
cow, magic = 1, 2
|
The first line is like saying "store 3 in cow, magic and teh_var". The second
line is like saying "store 1 in cow, store 2 in magic".
|
|
6. Variable Fun
|
|
Now, let's have some examples of variables. Each example is basically a comment
describing how things work, some variable operations and then a print statement.
I'll use ( p stuff ) instead of ( print stuff ) from now on (because I'm lazy),
both of them are the same.
# Just some mathematics.
# The * sign is used to indicate multiplication (number x number),
# the / sign indicates division.
a = 2
b = 4
p a + b # 6
p a - b # -2
p b - a # 2
p a * b # 8
p b / a # 2
# You can join two strings together by using the plus (+) sign
text1 = 'mon'
text2 = 'key'
p text1 + text2 # "monkey"
# We store a 'lol' in v1, we store v1's value in v2, we store "!!!" in v3
# Then we print v1 + ' ' (space) + v2 + v3
v1 = 'lol'
v2 = v1
v3 = '!!!'
p v1 + ' ' + v2 + v3 # "lol lol!!!"
# One of the ways to add some numeric value to a string is embedding it
# using #{}, you just put #{number or variable} inside the string.
# When you do that, you must use double quotes (" ") for the string.
# Double quotes lets the compiler check the string to find any #{}s
# otherwise (single quotes), no checking is done and no conversion either.
num = 1337
p "The number is #{num}" # "The number is 1337"
# Another way is using number.to_s, like this:
num = 1337
p 'The number is ' + num.to_s # "The number is 1337"
# You can also convert strings to numbers.
# You see, 3 and '3' aren't the same thing, you
# can't do '3' + 1 because '3' is a string.
# To change it into an integer use string.to_i
var = '3'
p var.to_i + 1 # 4
# Just a note, you don't need to use variables
# to use .to_s and .to_i
p 'The number is ' + 1337.to_s # "The number is 1337"
p '3'.to_i + 1 # 4
|
|
|
7. Conclusion
|
|
Variables allow us to store values that vary, although all the examples shown
didn't really show useful variables. Understanding variables is essential; you
won't go far without using them. I tried to make this tutorial very easy to
understand but I might've failed in doing that. Don't worry though, you can find
tons of variable tutorials on Google or something, and you can always read this
boring tutorial again. The next tutorial will be about flow control statements
such as loops and the if statement. Later~
|
|
8. Summary
|
- There are cases when you'll need to store some values in memory. The values
could be either numeric, strings or Booleans. Numeric values can be either
integers (no fractions) or floats (fraction). Strings must be enclosed in quotes
(single or double). Boolean values can be either true or false. - Variables
are basically identifiers presenting values stored in memory. You use variables
just like you use their values. The names (identifiers) of variables must start
with a small letter (most of the time) and are case sensitive. - We use
variables to store values that might change. We might not know the value of
something at a given time but we still need to use that value, so we use
variables.. - We assign values to variables by using the equal sign, which
assigns the values at the right side of the sign to the variable(s) at the left
side ( var = value ). Some other ways to assign variables is ( var1 = var2 =
var3 = value ) and ( var1, var2 = value1, value2 ) - We can do mathematical
operations on variables, join strings together, embed numbers in strings and
convert variables from a type to another. |
 |
More in the 'RGSS For Dummies' series:
• RGSS For Dummies Tutorial 1: The Basics
• RGSS For Dummies Tutorial 2: Variables
• RGSS For Dummies Tutorial 3: Control Flow
• RGSS For Dummies Tutorial 4: Containers and Methods
• RGSS For Dummies Tutorial 5: Object Oriented Programming and More!
• RGSS For Dummies Tutorial 6: Game Programming 101
• RGSS For Dummies Tutorial 7: Windows for Dummies |
|