module Tutorial
class Main
Hello once again, it's Pacman here with another bunch of scripting tips. This set will show you how the main declaration in YOUR game works, and how it is relevant to things you can do in your own scripts.
def Declaration
The Declaration
So, what is a method? Good question, me. A method is a... well, method that a script carries out when referred to. How do you refer to a method? In another method! We'll look at those in another topic. This is about main. So, how does this chain of referring end up becoming a game? The main script doesn't have a method, only a begin line. This means that it runs automatically upon startup. Let's study the main script in all, shall we?
begin
Graphics.freeze
$scene = Scene_Title.new
$scene.main while $scene != nil
Graphics.transition(30)
rescue Errno::ENOENT
filename = $!.message.sub("No such file or directory - ", "")
print("Unable to find file #{filename}.")
end
We left out the first 6 lines because the were
comments, or
blank lines. Neither of these contribute to scripting overall, but are handy for layout and usage.
begin
The first line, begin, simply signifies the beginning of the script. However, were it not for this line the rescue method can't be carried out. We'll talk about that later.
Graphics.freeze
Graphics.freeze is a line that refers to the freeze method in the Graphics module. You'll not find the Graphics module in the script editor, only in the help file. The way this line works is by calling a module, Graphics, and then a method within the module following a period. This means that the method belongs inside the module. Graphics.freeze is a method that fixes the current screen to prepare for transitions.
$scene = Scene_Title.new
$scene = Scene_Title.new, the third line, refers to the Scene_Title class, which you can find in the script editor. $scene means that the class specified will serve as the scene. .new runs the initialize and main methods of the scene. So, basically this line calls the title screen to run. We'll look into those another time.
$scene.main while $scene != nil
$scene.main while $scene != nil might take a bit more explaining. $scene.main means that main will keep running in a loop, calling stuff and things. while is a simple syntax: it means while. != means an inverse equal, meaning that this stuff will happen if this stuff isn't happening. nil means nothing, of course. $scene != nil means, as a whole, scene isn't nothing, or scene is something. The entire line as a whole means that main will keep running as long as there is a scene happening. When main isn't running, the program shuts down.
Graphics.transition
Graphics.transition(30) is quite simple. It once again refers to the graphics module, with the transition(duration, filename, vague) method. You might notice that the line calls the method with only one argument, whereas the method is defined (in the help file) with three. Usually, this would result in an argument error, but filename and vague are allowed to be omitted, as is duration which isn't in this case. This is because the 'hidden' modules and classes deal with all that stuff. Quite simply, Graphics.transition carries out a transition from the screen fixed in Graphics.freeze to the current screen. (Help file)
rescue Errno::ENOENT
rescue Errno::ENOENT confused to hell out of me when I first saw it, but don't be scared. rescue is another built-in syntax word, designed to save errors from happening. Have a look in the help file if you're need some explanation on it. Errno is a module that contains exceptions corresponding to system call errors. The :: part is to refer to the ENOENT value, which is defined in the build-in module that we don't get to see. ENOENT means, in a nutshell, no file or directory. This line is a branch modifier because of the rescue, meaning that it needs a branch to work off of which is why we need the begin at the start of the script. The lines that follow this are only called when ENOENT occurs, ergo displaying missing files.
filename = $!.message.sub("No such file or directory - '', "")
filename = $!.message.sub("No such file or directory - '', "") is a lot simpler than it looks. Filename is a value that is being defined once the rescue in the previous line is called. It is set in usage for the following line, because it is a value that is used in that print.
print("Unable to find file #{filename}."
print("Unable to find file #{filename}." Opens up a text window that displays the text within the quotation marks. print is a very useful command for acknowledging errors. #{filename} includes the filename value in the print, i.e. displaying the missing filename and it's supposed directory.
end
end is simple. It finishes off the script, balancing it from the begin. Scripts must have balanced endings, which is why using proper indentation in scripts is so crucial because it tips you off as to what you still need in terms of endings. We only need one end because rescue is a branch modifier, not a branch itself. The first line starts the branch, rescue modifies it and end finishes it.
end #def Declaration
I hope this helped you understand a bit more about the system of VX and how RGSS2 and Ruby work. If not, then I hope it confused you pants-less.
end #class Main
end #Tutorial