Next level RGSS - From beginner to expert
#1
Next level RGSS
This guide will teach the beginner RGSS scripter how to take his skills to the next level using advanced techniques.

Using comments effectively
Comments are pieces of text inside code that aren't executed. Comments are used for organization and documentation. Whenever you script, you should apply comments to your commands. Comments should not be everywhere, but they should be above every function and every confusing command. Comments can be applied in two ways. There are single line comments that are added using a # and there are multi-line comments applied using =begin comment here =end. I will show you the difference:
Code:
class TestClass
def initialize
@a = 1
end
def addToA(val)
@a += 1
return @a
end
def addToAtimes(val, times)
for i in 1..times
@a += val
end
end
end
Was it easy to understand? My guess is no. How about this:
Code:
=begin
A simple class for purpose of testing. It has one variable, @a. It has two
functions both modifying the value of a
=end
class TestClass

# Initialize object
def initialize
# create the variable @a with a value of 1
@a = 1
end

#This function adds the value passed with "val" to the variable @a.
def addToA(val)
@a += 1
# Return the new value.
return @a
end

# This function does the same as addToA, but it repeats the process
# as many times as passed with the "times" argument.
def addToAtimes(val, times)
for i in 1..times
@a += val
end
end
end

Using classes effectively
If you plan on creating systems or engines similar to battle systems, you will have to make use of the object oriented concepts of RGSS. Classes are extremely important in order to make dynamic in your scripts. If we are planning on making a new system from scratch that gives the player ability to learn spells, create spells and to use the spells. A class is a blueprint of a type of object, so we would have to make a Class for the spells so we can easily organize all the data. Read the following source code:
Code:
class Spell
# Make variables accessable from outside the objects
attr_accessor :name
attr_accessor :accuracy
attr_accessor :speed
attr_accessor :damage
attr_accessor :icon
attr_accessor :mana
# Initalization
def initialize(name, accuracy, speed, damage, icon, mana)
@name = name
@accuracy = accuracy
@speed = speed
@damage = damage
@icon = icon
@mana = mana
end
end
The class is very simple, but this is all that is needed as most of the functionality will be implemented elsewhere. The initialize function stores all the passed values into instance variables that are made public in the beginning of the script. We have the following variables. Name, a string containing the name of the spell, accuracy, an integer deciding the chance of hit and miss, speed, an integer deciding which position the spell has in a turn based battle system, damage, an integer determining what amount of HP will be removed from the target upon hit, icon, a string containing the path to the icon image and finally mana, an integer determining how many mana points will be removed from the user upon casting the spell. Next, we should use an array to store all created spells. My recommendation would be Game_Party, unless you want the spells to be available for enemies as well, in which case I would use Game_System.

Saving data in save-files
In many cases, you want the data you handle to be stored when the player saves his game and retrieved when he loads it. There are several classes that are being saved and loaded into the save-files. $game_system, $game_party and $game_player are examples. Adding data to these classes are very easy using aliases. Alias copies a method which allows you to overwrite the old method then call the copy and thereby not erasing any statements in the function but adding new ones to it. Let's say we want two variables, @var1 and @var2 to be added to the $game_system object so it can be saved along with other data and loaded next game session. Read the following source:
Code:
class Game_System
attr_accessor :var1
attr_accessor :var2
# Copy method initialize
alias old_init initialize
def initialize
old_init
@var1 = "value1"
@var2 = "value2"
end
end
And that's it. We first use two attr_accessor statements to make the variables publicly accessible. We then copy the statements of the method initialize which is the constructor, into a new method, old_init so we can call it even though we overwrite the original initialize. We now redefine initialize, call the old contents of it and add two new variables to it. $game_system is an instance of Game_System, so it will be created with these two variables when a new game is created. They will then be stored with the object in save files. This is mainly used if you are creating a script for public use. If you are making a system for yourself you would rather go and edit the original method.
This will not work if implemented after a new save file is created. It will work for all files created after the script is changed.

Using loops to perform operations
In some cases you have a whole array of objects of the same or similar classes that all need to be performed the same operation on. An example of this is the array of events stored in the Game_Map class. All the events (Game_Event) need to be called the .update function upon every frame, but we can't bother with writing one line for each of them for obvious reasons. We use the for loop. Take a look at this:
Code:
for event in @events
event.update
end
The "for" stands for forward and this is a so-called iteration loop. You write "for", then the name of a variable that will be pointing to each of the elements in the array specified after "in" one by one.
The loop will process one time for each element in the range (y..x, y...x) or array ([x, y, z]) specified on the end of the line. Whatever operations you perform on the variable "event" or whatever name you choose will therefore be done on every object in the array.
Sometimes it might be necessary to know how many times the loop has run. There are two ways. The first is the most obvious:
Code:
t = 0
for i in @array
i.action
t += 1
print "The loop has now run #{t} times."
end
Alternatively you can make the loop run trough a range instead of an array.
Code:
for i in 1..10
print "the loop has now run #{i} times."
end
This has the same effect, but in this context we need it to iterate trough an array, and that can be done with the last method as well.
Code:
for i in 0...@array.size
@array[i].action
print "The loop is performing the action on the #{i}th element of the array.
end
Remember that three dots (...) means skip the last iteration which must usually be done when you begin on 0. Most arrays has it's first element as the 0th so you will need to use this format or add a - 1 to the index variable.[/i]

Using modules to store constant data
Modules are great for configuration and for databases. They are difficult to use with variable values, but they are great for constants, in other words values that stay the same always. Here is an example:
Code:
Module Valdred_Configuration
TEXT_TO_PRINT = "Hello world"
TIMES_TO_PRINT = 5
end
Let's say I have made a script that will display a popup window with creator-specified text a number of times before the game begins. A useless script, I know, but this is for illustration. I want to let the user (the game creator) easily choose what text to display and how many times, so I make a module. Now I can make the statements look like this:
Code:
Class Scene_Title
alias old_init initialize
def initialize
old_init
for i in 1..Valdred_Configuration::TIMES_TO_PRINT
print Valdred_Configuration::TEXT_TO_PRINT
end
end
end
This is great for user friendlyness. Modules can also be used to store static databases, that is, databases that will stay the same and not change. Here is a little example:
Code:
Module Database
WEAPON_NAMES = ["Sword", "Bow", "Lance"]
WEAPON_TYPES = ["Melee", "Range", "Melee"]
WEAPON_DAMAGES = [10, 5, 7]
WEAPON_SPEEDS = [5, 10, 8]
end
Of course, you should stick to classes for this kind of thing, but they should still be stored in modules if they are static like this.

That is all for now!
Valdred
Tech Administrator of Save-Point
Reply }
#2
Just came across this through the Online List. This is definitely a very useful tutorial. I just learned a little more about Module so thanks for that.
Reply }
#3
Glad you like it :)
Valdred
Tech Administrator of Save-Point
Reply }


Possibly Related Threads…
Thread Author Replies Views Last Post
   Proper Script Placement and Usage for RGSS 1 through 3 kyonides 1 3,885 08-25-2019, 08:36 AM
Last Post: kyonides
Brick  WIP - Randomly Generating Terrain - RGSS azrith001 0 4,163 07-12-2012, 07:21 PM
Last Post: azrith001
   RGSS Roadguide Trickster 0 4,907 12-07-2008, 06:21 AM
Last Post: Trickster
   Regular Expressions in RGSS (Regexp) Zeriab 0 4,546 12-07-2008, 04:26 AM
Last Post: Zeriab



Users browsing this thread: 1 Guest(s)