Constants are one of the main types of values, another is variables. There are many types of variables, and here's a basic introduction to some of them. You'll have to use most of these if you ever write a script.
A
local variable is a variable only accessible to the method or block in which it is defined. Don't worry about what a block is, we'll get to them eventually. To show you what I mean, I'll give you an example of improper use of a local variable. You'll also get to see some important syntax in use.
class Tutorial # -> Declaring the class
def local_variable # -> Defining a method: def method_name
x = 4 # -> Define x variable as 4
end # End method definition
def show # -> Defining a method
print x # -> Print variable x (4)
end # End method definition
end # End class declaration
t = Tutorial.new # -> Create instance of Tutorial class, store in variable
t.local_variable # -> Set the x variable
t.show # -> Throws an error
Now, if you didn't know any better, you'd assume that the last line would print '4' in a message. But it doesn't. It gives you an error. This is because the defined variable x is a local variable, only accessible to the method in which it is defined. The 'show' method attempts to access the x variable, but it does not exist for that method so it gives you an error.
Instance variables give you a way around that. Instance variables can be accessed throughout the entirety of an instance of a class. They are usually defined in the 'initialize' method of a class, because that is the method that is run as soon as you create the instance. Instance variables always begin with an @ symbol. Watch an instance variable in action.
class Tutorial # -> Declaring the class
def initialize # -> Defining a method
@x = 4 # -> Define instance variable as 4
end # End method definition
def show # -> Defining a method
print @x # -> Print instance variable '@x'
end # End method definition
def add_to_x # -> Defining a method
@x += 1 # -> Add one to instance variable '@x'
end # End method definition
end # End class declaration
t = Tutorial.new # -> Create instance of Tutorial class, creating with it the '@x' variable and storing it in a variable
t.show # -> Works, prints 4
t.add_to_x # -> Add 1 to @x
t.show # -> Works, prints 5
print t.x # -> Does not work, throws an error
You may notice that the last line there doesn't work. This is because instance variables are not accessible from outside of the instance. There is, however, a way around this. attr_reader, attr_writer and attr_accessor are all methods to fix this problem. attr_reader creates a method that allows you to read the variable from outside the instance, attr_writer creates a method that allows you to write the variable from outside the instance, and attr_accessor does both. In short, attr_reader creates a 'get' method for the variable, attr_writer creates a 'set' method for the variable, and attr_accessor creates both a 'get' and a 'set' method. Here is how you use them.
class Tutorial # -> Declaring the class
attr_reader :x # -> Creates a 'get' method for '@x'. Syntax: attr_reader :variable
def initialize # -> Defining a method
@x = 4 # -> Define instance variable as 4
end # End method definition
end # End class declaration
t = Tutorial.new # -> Create instance of Tutorial class, creating with it the '@x' variable and storing it in a variable
print t.x # -> Successfully prints '4'
t.x = 5 # -> Does not work.
Now, I think you might be getting the hang of this, so I won't comment every line, just the important ones.
class Tutorial
attr_writer :x # -> Creates a 'set' method for '@x'. Syntax: attr_writer :variable
def initialize
@x = 4
end
end
t = Tutorial.new
t.x = 5 # -> Works. Variable '@x' of t is now 5.
print t.x # -> Does not work.
We can now set the variable, but no longer read it. Here's the most efficient way to create both the 'get' and 'set' methods.
class Tutorial
attr_accessor :x
def initialize
@x = 4
end
end
t = Tutorial.new
print t.x # -> Prints 4
t.x += 1 # -> t.x = 5
print t.x # -> Prints 5
t.x = "We can make variables whatever we want!"
print t.x # -> "We can make variables whatever we want!"
So now that all works. Good. That's instance variables, very very useful variables.
The next thing we'll be looking at in this mammoth of a lesson is
class variables. You won't use these very often, but it's still good to know how to use them. A class variable is a bit like an instance variable, they are always made as such by two @ symbols, '@@', and are accessible from
all instances of the class in which they are made. This means that if we have 2 instances of the same class that use this same variable, changing it in one will change it in the other. This would not happen with instance variables. Here's a demonstration:
class Tutorial
def initialize
@@x = 4 # -> Define class variable as 4
end
def show
print @@x # -> Print class variable '@@x'
end
def add
@@x += 1 # -> Add 1 to class variable '@@x'
end
end
t = Tutorial.new
s = Tutorial.new # -> Create another, separate instance of the class
t.show # -> 4
t.add # -> @@x now equals 5
s.show # -> 5 (it was altered by the add method, even though it was in a different instance)
Global variables is the last type of variables we'll be looking at. You'll definitely be using these if you take up RGSS. Global variables are denoted by a '$' at the beginning of the name. Global variables are accessible from anywhere. All classes, all modules, all methods, everything. They are used in RGSS to store important instances of classes that need to be used everywhere, like $data_system, $game_party, $game_temp, etc.
class Tutorial
def say
print "This is a global variable!"
end
end
$tut = Tutorial.new # -> Create global variable
$tut.say # -> "This is a global variable!"
class Something
def say
$tut.say
end
end
s = Something.new
s.say # -> "This is a global variable!"
When using global variables, be aware of their
downsides.
And that's variables in a nutshell.