Hello. My name is Broken, and I'm here to give my first ever Scripting Tutorial. I have decided to make a small Object Oriented Programming (OOP) Tutorial for anybody who doesn't understand the basics of OOP.
Note: This tutorial uses OOP as used in RGSS, but pertains to other languages, such as Java, as well.
For this Tutorial you must know:
Step One: Purpose
The first step to making any script is a Purpose. Without a reason to make a script, there is no need for it. For this tutorial we will be making a PC Script. The purpose of the PC Script is to show how OOP works.
Step Two: Plan
The next step is to plan out the script. In this step you must write on paper in MS Word a basic layout for the script, any attributes for classes, and methods, with detailed descriptions of what they all do. The only part of the design I will tell you for this script is in the spoiler.
I hope this tutorial taught you something, and more importantly that it was understandable. I'm sorry if this confuses you, or in any way seems retarded (as I am horrible at explaining such things well). Peace Out!
~Broken
Note: This tutorial uses OOP as used in RGSS, but pertains to other languages, such as Java, as well.
For this Tutorial you must know:
- how to create a class,
- how to use the Attribute Accessor and what it is for,
- what each of the data types are (String, Integer, Boolean, etc.)
- what each of the comparison operaters are (==, >=, <=, !=, >, <, etc.)
- how to define a method,
- and what looping structures are and how to use them.
Step One: Purpose
The first step to making any script is a Purpose. Without a reason to make a script, there is no need for it. For this tutorial we will be making a PC Script. The purpose of the PC Script is to show how OOP works.
Step Two: Plan
The next step is to plan out the script. In this step you must write on paper in MS Word a basic layout for the script, any attributes for classes, and methods, with detailed descriptions of what they all do. The only part of the design I will tell you for this script is in the spoiler.
The plan is to create a PC with a Monitor, Speakers, a Tower, and Input Devices (Mouse and Keyboard). The class PC will hold instances of Moniter, Speakers, Tower, and InputDevice. The PC's components will inherit from Electronics. I will explain inheritence and instance later (as these are the purpose of the Tutorial!)
Here we will write the script. First we must create class Electronics.
Then we shall add the attributes and initialize method.
plugged_in is a boolean we shall use to determine if the electronic is even plugged in. We use on as a boolean as well to determine if the Electronic is turned on. If plugged_in == false then on will automatically equal false.
Now we will set up the inside of initialize. This method should have two arguments; plugged_in, and on. You should know what these are for by now.
We assign these arguments to @plugged_in, and @on, the instance veriables for this class.
As you can see, I used a conditional statement to test if @plugged_in == false. Also note that in the arguments, on = false. This is so that you don't have to pass a value to the argument. If no value is given, on will automatically equal false.
Now we must create the next class. We shall make class Tower now.
For class Tower, we will need these attributes: cd_drives, ram, proccessor, on, and plugged_in.
Note: Do you see on and plugged_in? These come from class Electronics. Look and see:
As you can see, in the class definition, I add < Electronics. This is called extending a class, and the result is that the Parent Class (in this case, Electronics) gives its methods and instance variables to its child class (here, Tower). This means that class Tower can now be plugged in and turned on, like Electronics.
I also used a new keyword, super, which is important for inheritance. You see, if you want to use the parent class's initialize method, then in the first line of the child's initialize method you use super(args), where args is the set of arguments that must be passed to the parent class's initialize method.
Note: super can be used with other methods than the initialize method. Just make sure that the Parent Class method has the same name as the child class method that uses super!
Now we will create the Monitor class! This class contains the attributes plugged_in, on, and wide_screen. wide_screen is a boolean used to determine if the Monitor is a widescreen monitor or not.
This is a short and easy class. I'll just skip to the next class; Speakers.
class Speakers contains the attributes plugged_in, on, volume, and pan, where volume is an integer between 0 and 100, and pan is an integer (0 = pan right, 1 = pan center, 2 = pan left).
This looks about the same as the others, except for check_volume and check_pan. These are two methods we will create to make sure that @volume and @pan are within the appropriate ranges.
Here are these methods:
These are very basic, and should be easy to understand. Now, what if we want to change the volume or pitch? These methods aren't called when a value is changed, so we now need to create two new methods. They will be the last methods in class Speakers.
These are simple assignment methods, with method calls to check_volume and check_pan inside. This will ensure that volume and pan never become something they are not allowed to be!
And now it is time for class InputDevice! These usually don't need to be plugged in or turned on, so they do not extend Electronics. Here is InputDevice's list of attributes: name (the string name of the device), type (0 for mouse, 1 for keyboard), and buttons (integer of the amount of buttons on the device). Name is assigned 'Mouse' or 'Keyboard' based on type.
And now for the ever-so-important class PC. class PC contains the attributes on, tower, monitor, speakers, mouse, and keyboard. on is determined by whether the Tower is on or not.
Now some of you may be confused. Let me explain; in this class, the varaibles each equal one of the classes we created before. By typing @tower = Tower.new() we are saying '@tower is an instance of class Tower'. What this really means is, @tower holds all of the variables, methods, etc. of class Tower. and here is the really cool part;
As you can probably see, I assigned InputDevice to 2 different variables. That's because we need a Mouse and a Keyboard, but we don't want to make a class for each one. So we make on class that can be either a Mouse or a Keyboard. If we want both of them at the same time, just assign InputDevice to 2 different variables like I did, with the argument type equalling 0 on @mouse and 1 on @keyboard. I'm sorry if this doesn't make sence, so I'll explain better (kinda):
An instance of a class is like a copy. The original class is never really altered, and you can make as many copies as you wish. So if I say @mouse.name = 'Pickle', only the name variable inside @mouse will change. @keyboard.name will remain the same regardless of what you call @mouse.name. This is true with all of the attributes of an instance of a Class.
Now to test this script.
after placing the other codes in your script editor, replace class PC with this new class PC:
and go to Main in the Script Editor and replace line 12 with this:
Code:
class Electronics
end
Code:
class Electronics
attr_accessor :plugged_in
attr_accessor :on
def initialize
end
end
Now we will set up the inside of initialize. This method should have two arguments; plugged_in, and on. You should know what these are for by now.
We assign these arguments to @plugged_in, and @on, the instance veriables for this class.
Code:
class Electronics
attr_accessor :on
attr_accessor :plugged_in
def initialize(plugged_in, on = false)
@plugged_in = plugged_in
if @plugged_in == false
@on = false
else
@on = on
end
end
end
Now we must create the next class. We shall make class Tower now.
For class Tower, we will need these attributes: cd_drives, ram, proccessor, on, and plugged_in.
Note: Do you see on and plugged_in? These come from class Electronics. Look and see:
Code:
class Tower < Electronics
attr_accessor :cd_drives #Int amount of CD Drives
attr_accessor :ram #Int Size of RAM in MB
attr_accessor :proccessor #String proccessor name
attr_accessor :plugged_in #Bool plugged in
attr_accessor :on #Bool turned on
def initialize(cd_drives, ram, proccessor, plugged_in, on)
super(plugged_in, on)
@cd_drives = cd_drives
@ram = ram
@proccessor = proccessor
end
end
I also used a new keyword, super, which is important for inheritance. You see, if you want to use the parent class's initialize method, then in the first line of the child's initialize method you use super(args), where args is the set of arguments that must be passed to the parent class's initialize method.
Note: super can be used with other methods than the initialize method. Just make sure that the Parent Class method has the same name as the child class method that uses super!
Now we will create the Monitor class! This class contains the attributes plugged_in, on, and wide_screen. wide_screen is a boolean used to determine if the Monitor is a widescreen monitor or not.
Code:
class Monitor < Electronics
attr_accessor :wide_screen
attr_accessor :plugged_in
attr_accessor :on
def initialize(wide_screen, plugged_in, on)
super(plugged_in)
@wide_screen = wide_screen
end
end
class Speakers contains the attributes plugged_in, on, volume, and pan, where volume is an integer between 0 and 100, and pan is an integer (0 = pan right, 1 = pan center, 2 = pan left).
Code:
class Speakers < Electronics
attr_accessor :volume
attr_accessor :pan
attr_accessor :plugged_in
attr_accessor :on
def initialize(volume, pan, plugged_in, on)
super(plugged_in, on)
@volume = volume
@pan = pan
check_volume
check_pan
end
end
Here are these methods:
Code:
#check_volume
def check_volume
if @volume < 0
@volume = 0
elsif @volume > 100
@volume = 100
end
end
#check-pan
def check_pan
if @pan != 0 or @pan != 1 or @pan != 2
@pan = 1
end
end
Code:
#volume
def volume=(value)
@volume = value
check_volume
end
#pan
def pan=(value)
@pan = pan
check_pan
end
And now it is time for class InputDevice! These usually don't need to be plugged in or turned on, so they do not extend Electronics. Here is InputDevice's list of attributes: name (the string name of the device), type (0 for mouse, 1 for keyboard), and buttons (integer of the amount of buttons on the device). Name is assigned 'Mouse' or 'Keyboard' based on type.
Code:
class InputDevice
attr_accessor :name
attr_accessor :type
attr_accessor :buttons
def initialize(type = 0, buttons = 2)
@type = type
@buttons = buttons
if @type == 0
@name = 'Mouse'
elsif @type == 1
@name == 'Keyboard'
end
end
end
Code:
class PC
attr_accessor :on
attr_accessor :tower
attr_accessor :monitor
attr_accessor :speakers
attr_accessor :mouse
attr_accessor :keyboard
def initialize
@tower = Tower.new(1, 1024, 'Pentium IV', true, true)
@monitor = Monitor.new(false, true, true)
@speakers = Speakers.new(68, 2, true, false)
@mouse = InputDevice.new(0, 3)
@keyboard = InputDevice.new(1, 316)
end
end
As you can probably see, I assigned InputDevice to 2 different variables. That's because we need a Mouse and a Keyboard, but we don't want to make a class for each one. So we make on class that can be either a Mouse or a Keyboard. If we want both of them at the same time, just assign InputDevice to 2 different variables like I did, with the argument type equalling 0 on @mouse and 1 on @keyboard. I'm sorry if this doesn't make sence, so I'll explain better (kinda):
An instance of a class is like a copy. The original class is never really altered, and you can make as many copies as you wish. So if I say @mouse.name = 'Pickle', only the name variable inside @mouse will change. @keyboard.name will remain the same regardless of what you call @mouse.name. This is true with all of the attributes of an instance of a Class.
Now to test this script.
after placing the other codes in your script editor, replace class PC with this new class PC:
Code:
class PC
attr_accessor :on
attr_accessor :tower
attr_accessor :monitor
attr_accessor :speakers
attr_accessor :mouse
attr_accessor :keyboard
def initialize
@tower = Tower.new(1, 1024, 'Pentium IV', true, true)
@monitor = Monitor.new(false, true, true)
@speakers = Speakers.new(68, 2, true, false)
@mouse = InputDevice.new(0, 3)
@keyboard = InputDevice.new(1, 316)
end
def main
print @tower.proccessor
print @monitor.wide_screen.to_s
print @speakers.volume.to_s
print @mouse.name
print @keyboard.buttons.to_s
$scene = Scene_Title.new
end
end
Code:
$scene = PC.new
~Broken