SephirothSpawn
Sponsor
By SephirothSpawn & Trickster
An Introduction
This Tutorial was designed to teach people how to add new attributes to the default RMXP classes whether they be the Game_Session classes or the Data Structure classes. We will begin with learning about instance variables and explore several ways to achieve adding attributes into classes.
A Quick look at Instance Variables
Instance Variables are basically properties to objects in Ruby. An object is everything from a certain actor, item, weapon, etc. In Ruby, these objects are made from classes. Classes are collection of variables and methods that basically run your game.
Instance variables are started with an @ symbol, followed by a letter or underscore followed by any combination of letters, numbers or underscores.
Code:
class Something
# The following is a special method, automatically evoked with the .new call
def initialize
# The following are instance variables
@instance_variable = 5
@hp = 100
@sp = 50
end
end
Instance Variables are special variables that can be used throughout an entire class. They will be accessible in all methods of the class. By default, you cannot access these variables outside of the object. Meaning, if you want to see the value @instance_variable in some_object, you cannot view or modify this data. So what Ruby does is Public access to these variables by creating specialized methods to do so.
Attr_Variables
Attr_Variables are quick versions of methods that allow you to read, write or both to an instance variable. Before we take a quick look at the actual attr_variables, lets look at the actual methods.
Code:
class Something
def initialize
@your_variable = 5
end
# The following method, allows you to read the data within your instance variable
def your_variable
return @your_variable
end
# The following method allows you to write over the variable
def your_variable=(new)
@your_variable = new
end
end
# First, we will create our object
object = Something.new
# To read our @your_variable within that class, we simply call the method .your_method
p object.your_variable -> 5
# To Write over our variable, we will use an = sign, followed by a new value
object.your_variable = 10
p object.your_variable -> 10
As you can see, if you were to have a class with several instance variables, giving public access to these variables will quickly add more code lines than you could need. So what Ruby did was give us attr_reader, attr_writer & attr_accessor.
attr_reader basically emulates our first method, that will return our value (Reads). So you could eliminate that method, and add the line - attr_reader :your_variable
attr_writer emulates our second method, that modifies our variables (Writes). So now you can eliminate our second method and add the line - attr_writer :your_variable
We now have:
Code:
class Something attr_reader :your_variable
attr_reader :your_variable
def initialize
@your_variable
end
end
We aren’t done yet though. This can still be simpler. Attr_accessor emulates both of our first and second method, allowing us to both read and write our variables.
Code:
class Something attr_accessor :your_variable
def initialize
@your_variable
end
end
This is just one instance variable. Now imagine dynamic objects with several instance variables. This drastically helps in larger scripts.
So why not always attr_accessor?
Many people often just make everything an attr_accessor (including myself). The main reason for this is setting up dynamic reader and writer method, that simply don’t just return an instance variable or simply overwrite it.
Lets say, in calculating defense of some object, you not only take the defensive value of the actor, but also add in any armor the actor has equipped. You would not make you @defense variable a direct reader, but instead make some dynamic method.
Code:
class Something # The following is just a made up method. Don’t bother looking in RMXP for it.
def defense
n = @defense
# The following line just passes through some make believe armors
for armor in [@armor1, @armor2]
# This just adds defense from our armor objects to our defense total
n += armor.defense
end
# Now we return the collection of our defense and our armors defense
return n
end
end
Through this, you are able to create more dynamic attributes for your objects.
Creating New Attributes
So, now that we know how to create new attributes to an object, we can now add new attributes to our classes. The most basic way of doing this is through new attr_variables into whatever class we want and going from there. What I have found is that by using several combinations of container classes (Arrays and Hashes) with methods in your classes gives you the most efficient way of doing this, but we will look at several ways of going about this.
The “Throw a bunch of accessor in a classâ€