RMRK is retiring.
Registration is disabled. The site will remain online, but eventually become a read-only archive. More information.

RMRK.net has nothing to do with Blockchains, Cryptocurrency or NFTs. We have been around since the early 2000s, but there is a new group using the RMRK name that deals with those things. We have nothing to do with them.
NFTs are a scam, and if somebody is trying to persuade you to buy or invest in crypto/blockchain/NFT content, please turn them down and save your money. See this video for more information.
Scripting Basics: Studying the Main script

0 Members and 1 Guest are viewing this topic.

*****
my name is Timothy what's yours
Rep:
Level 79
Hello
2014 Zero to Hero2014 Best IRC Quote2014 Most Missed Member2012 Zero To HeroSecret Santa 2012 ParticipantContestant - GIAW 9For frequently finding and reporting spam and spam bots2011 Zero to Hero
Code: [Select]
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.

Code: [Select]
    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?
Code: [Select]
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.

Code: [Select]
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.

Code: [Select]
  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.

Code: [Select]
  $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.

Code: [Select]
  $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.

Code: [Select]
  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)

Code: [Select]
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.

Code: [Select]
  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.

Code: [Select]
  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.

Code: [Select]
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.
Code: [Select]
    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.
Code: [Select]
  end #class Main
end #Tutorial
« Last Edit: May 19, 2011, 12:38:09 PM by Pacman »
it's like a metaphor or something i don't know