Learn Ruby with me.
by Tiger
Note: This tutorial reproduces the free Game Forerunner forum, the copyright belongs to author Tiger.
First article
Second article
First Article
Preface
I'm going to start learning Ruby from today. What's up, don't you see? A newcomer writes a tutorial. On the basis of my strong perseverance, scientific self-study methods, as well as my research spirit, I have the confidence to learn Ruby, but also novice you can! Because this tutorial is a rookie teach the birds if there is anything wrong, please teach the elder! If no one is watching, then I am a fool of my own music.
======================================================================
Roll First language
--------------------------------------------------------------------------
Prepare knowledge
Why should I learn ruby?
Needless to say, it must be helping to unleash the potential of RM. And how much of Ruby's bad is irrelevant to me, because I learned it for one reason--rm!
Second, what is Ruby?
It is a purely object-oriented programming scripting language. This is like C language, is a computer can understand the language, like you shout to the computer "I want to surf the internet" it will not let you surf the Internet, must be a language to tell it, Ruby is one of them, of course, Ruby also need to play, not to say ...
What should I prepare to learn Ruby? RM, of course, and I've prepared three things:
Notebook--Don't look at it, this is the heavy weight, some of your doubts and pinang sense, the conclusions of the study can be recorded above.
. A new project, I have temporarily named it Test, it will be used in research and practice.
Another new project, I temporarily named it the standard, its role is in case you bring your own Ruby changed beyond recognition, there is a reference to it. Of course RM's script data is stored in the Scripts.rxdata file in the Data folder, and you can also back up this file.
--------------------------------------------------------------------------
Fundamentals of Practice
first, about the script Script Editor:
. RM, the way to enter the script Script Editor is to press F11 or select Script Editor from the main Menu Tools menu ...
Some shortcut keys in the Script editor (thanks to Carol3, this is not what I found):
Tab: Can jump grid, can automatically indent
CTRL + Mouse wheel: Adjust the size of text
CTRL + F: Find script on this page
CTRL + SHIFT + F: Find all scripts
CTRL + H: replace
CTRL + A: Select All
CTRL + C,ctrl + V: Copy and paste
CTRL + Z,ctrl + Y: Undo and Redo
F3: Used with CTRL + F, "Find Next"
F4,F5: Select previous/Next script
F6: Name this line of choice now
CTRL + ENTER: Save and close this window
Ii. steps to create a program:
Determine the goal of the program-for example, your goal is to do a triangular area of the program.
Determine what method to use-I know s=1/2ah=1/2absinc, the angle is too annoying, and the latter is not a separate multiplication operation, so I chose the former.
. Create the program.
Three, the good habits that should be cultivated in the programming:
. Write a comment. Many people think that writing notes is a waste of time--but don't forget--when you're writing code, maybe your ideas are clear, but when the program gets bigger, or when you're dealing with code written a few months ago, it's obvious what the annotation does.
In Ruby, an entire line of text following the # symbol is recognized as a comment. When you want to write large chunks of comments, you can also use =begin and =end in the middle of everything as a comment.
Comments are green, not executed, and do not affect the operation of the program, but are used only as a memo record for the writer's mark.
Not completed ...
Iv. comments on compilation errors
When you have a spelling, grammar error, the program will stop running and tell you the relevant information, and then press F11 into the Script editor, you will see the cursor in the wrong line.
Line: Uninitialized constant "Persond"
I have not summed up the specific method. Not completed ...
--------------------------------------------------------------------------
Type&run
(originally is not ready to send, but some people say that this tutorial wrote some out of RM, so sent the following--not ready to send the reason is I do not know Carol3 "RGSS District Top script collation, the current collection of scripts: 30, I hope you add" a post there is a tutorial! And there is a part of me almost the same, forget, in order to ensure the integrity of the tutorial, I will write my hair up ... No plagiarism! )
Let's start with Ruby:
Practice: Part of the title screen customization
Practical purposes:
. Learn how Ruby is studied.
. Learn to do some experiments according to the notes.
. Proficiency in the use of some shortcut keys in the Script Editor.
Open the Script Script Editor
. Locate Scene_title on the left side of the script editor
You can see the first line in the basket on the right:
#=====================================================================
# Scene_title
#------------------------------------------------------------------------------
# The class that handles the title screen
#=====================================================================
We know this is a comment, it means scene_title This code is for processing the title screen. Similarly, we can casually look at other groups (groups are similar to the scene_title in the Script Editor's left side of a channeling of code), how, their use at a glance!
Now that we know it's for processing the title screen, let's take it to the axe!
Take a tour: Read the database, build the System object, .... These are too deep to look down--
S1 = "New Game"
S2 = "Continue"
S3 = "Exit"
Finally saw the familiar Chinese word, ah! This is not an option for the title screen in RM!
Take a look at the note above it-generate a command window. Sure enough, it's the title screen option.
We changed it to try:
S1 = "New Reincarnation"
S2 = "Old Memories"
S3 = "Gone With the Wind"
Press CTRL + ENTER: Save and close this window.
Then press F12 to run the game. It sure did!
The ambition of man is always limitless, and I want to add a game about this! OK, try again:
S1 = "New Reincarnation"
S2 = "Old Memories"
S3 = "Gone With the Wind"
S4 = "About the game"
Greed always has some guilty, and then look at the contents of it and then run the game now!
@command_window = Window_command.new (192, [S1, S2, S3])
@command_window. back_opacity = 160
@command_window. x = 320-@command_window. WIDTH/2
@command_window. y = 288
Have x, y big cover is in say option of the box! Worse, one more option, the box must be longer, if you do not repair the box's properties option will appear when the option cannot be displayed. Do it! Even if it's true, there's nothing I can do.
The first line has a (192, [S1, S2, S3]) S1,s1,s3 as in S1 = "new Reincarnation" and so on S1,s1,s3 said the same thing? Now that I have added S4, I have to add the S4 as well:
@command_window = Window_command.new (192, [S1, S2, S3,S4])
S4;S3 is also used as a connection between learning it.
. Run the game again. Sure enough there was a game option and the box was automatically adjusted to include four options. We can see that our guess is wrong, there are X, y big lid is said in the box
The location?
The figure shows that the relative position of the star in the red box has not changed before and after the modification, and the relative position of the lower frame in the blue box and the RPG character has changed, that is to say, the position of the upper frame is constant, and when there are new options, the lower end of the box is automatically extended. And the x, y big cap is the position of the upper end of the box that says the option?
. Try again:
Copy CodeThe code is as follows:
S1 = "New Reincarnation"
S2 = "Old Memories"
S3 = "Gone With the Wind"
S4 = "About the game"
S5 = "ASD"
S6 = "SDASD"
S7 = "Weqw"
@command_window = Window_command.new (192, [S1, S2, S3,S4,S5,S6,S7])
Sure enough, there is no title to display.
. Look again:
S1 = "New Reincarnation"  
S2 = "Old Memories"
S3 = "Gone With the Wind"  
S4 = "About the game"  
@command _window = Window_command.new (192, [S1, S2, s3,s4,s5])
This error occurs.
S1 = "New Samsara"
S2 = "Old Memories"
S3 = "Died with wind"
S4 = "About the game"
@command_window = Window_ Command.new (192, [S1, S2, S3])
about the game will not be displayed.
. Now there's no use for this game. We can give it a function by referring to the "new Samsara", "Old Memories", "Passing with the wind". Whatever they have, I'm adding to the game. Looking down, notice that there are:
def update
@command_window. update
# when the C key is pressed;
if Input.trigger? ( INPUT::C)
Case @command_window. index
When 0 # new game
command_new_game
When 1 # continue
command_continue
When 2 # exits
command_shutdown
end
end
End
when-when ... , this is like the conditional part of a similar condition, and the note tells me that it is a new game to choose from;
then Command_new_game;command_continue;command_shutdown is the action. Will they be something that has already been defined in Ruby? If so, it is useless for you to write anything else about this game. So let's share one thing about the game and the exit:
Copy CodeThe code is as follows:
def update
@command_window. Update
# when you press the C key
If Input.trigger? (INPUT::C)
Case @command_window. Index
When 0 # New games
Command_new_game
When 1 # continue
Command_continue
When 2 # exit
Command_shutdown
When 3
Command_shutdown
End
End
End
Run it, sure enough! Press the game on it and quit. The visible command_shutdown is used to invoke the Exit command. Will it be something that has already been defined in Ruby? Let's look further down:
.
#--------------------------------------------------------------------------
# command: New games
#--------------------------------------------------------------------------
.
.
.
.
.
.
.
#--------------------------------------------------------------------------
# command: Continue
#--------------------------------------------------------------------------
.
.
.
.
.
.
.
#--------------------------------------------------------------------------
# command: Exit
#--------------------------------------------------------------------------
This seems to be a sort of discussion. They all start with Def xxxxxxx, and xxxxxxx's content is what the options in the previous disagreement are about. So it's not something that has been defined in Ruby. That leaves room for our customizations:
Copy CodeThe code is as follows:
def update
@command_window. Update
# when you press the C key
If Input.trigger? (INPUT::C)
Case @command_window. Index
When 0 # New games
Command_new_game
When 1 # continue
Command_continue
When 2 # exit
Command_shutdown
When 3
Abc
End
End
End
Copy the command: Exit the content, and paste after exiting, the pasted content of the Def Command_shutdown changed to def ABC. It works the same as before.
Well, as far as I am now, I can only do this, if you know some grammar, you can change the contents of Def ASD.
--------------------------------------------------------------------------
Summarize
Not completed ...
--------------------------------------------------------------------------
Homework
. What are the procedure development steps?
. Try using some shortcut keys in the Script Editor.
Raise the question:
Try to summarize some of the error types and their views that appear in the Script Editor's use.
Not completed ...
--------------------------------------------------------------------------------------------------------------- ----------
Answer
Slightly
Second article
Chapter I-Classes, objects and methods
--------------------------------------------------------------------------
Prepare knowledge
Now that Ruby is a purely object-oriented scripting language, I start with the object.
First, what is the object?
Everything in the game can be interpreted as an object.
The number of money is the object, the village head is the object, rogue armor is also like. Because the values that Ruby can handle are objects, the concept of classes is introduced to facilitate the manipulation of objects.
Second, what is the class?
The kind of object is called a class.
Village chiefs and Rogue armour belong to the NPC class, the number of money belongs to the natural number category (note: Here the class you can decide, you can also say that the village head and rogue a belong to human: belong to mammals: belong to ... )
Some special objects belonging to this class are called entities (or instances) of a class.
In daily life, we have a certain level of classification of all objects.
We know: creatures) mammals human
Village chiefs and rogue armor belong to human beings, it must have the properties of mammals, there must be animal properties. We call biology the parent of animals, animals are biological subclasses, it is not difficult to find that subclasses inherit the attributes of the parent class. Of course, there are cases where some properties of the parent class cannot be inherited by a particular subclass--although birds generally fly, penguins are a subclass of birds that do not fly.
Inspired by the laws of nature, Ruby introduced inheritance. It greatly reduces the workload of the programmer, we only need to define a parent class (or superclass) of the property, its subclass will automatically inherit its parent class properties. Of course, when you come across a special penguin case like the one mentioned above, add or redefine the difference between a subclass and a parent class. The meaning of inheritance is to preserve the nature of the heirs but to have their own unique characteristics at the same time.
The introduction of the class is just a convenient object operation, then how to manipulate the object? That's the way it's going to work.
Third, what is the method?
Method is a command that is specific to all objects in a class.
You simply invoke a method on an object to let the object execute the method represented by the method. The object being manipulated is called the "action instance" or "parameter of the method".
For example, you have a lot of dog, (dog is a class, the dog that you keep is its subclass) there is a name wong Choy (Wong Choy is an object, also an instance), you train them to sit down every day (this is to define a method), and then every time you send the number to sit down this command (which is called to sit this method) (You have done the work on the object by means of the method). If the dog you keep is divided into two classes of dogs and Pug, (the dog and Pug are the children of the Dogs you keep), they inherit the methods defined in their parent class. Of course a class can define many methods (you can teach a dog a lot of things).
Another example is to think of Ruby as CD player, all the things the CD machine operates--CD is all objects, class is the style of songs (such as R&m is a class, rap is another category); Of course, you can also be a singer as a class of sub-law (such as Jay Chou is a category, Eason Chan is another type), The calling method is to press the play button, stop button, pause button and so on.
Fundamentals of Practice
The steps to create an object and manipulate it in Ruby are as follows:
You first have to define the properties of the class--
I. Definition of Class
Format:
Class name
Content defined in the class
End
Note: The class name must begin with an uppercase letter.
If it is a subclass, write this:
Class Subclass name < parent class Name
Content defined in the class
End
or write directly behind the definition:
Class Subclass name < parent class Name
End
This simply lets the parent class inherit the parent class, not redefine it. Why do you say that?
Light has a class not yet, and then plug the method inside, so that later on the operation of the object.
Ii. definition of the method
The definition of a method is written in "content defined in a class" there
Format:
Def
Content defined in the method
End
Note: The method must start with a lowercase letter.
Of course, when you come across a special subclass of penguins like the one mentioned above, you'll also redefine the difference between subclasses and parent classes. This is called overloading, and the format is the same as above. The invocation of a method is defined repeatedly, whichever is the last.
In the case of a subclass method that calls the same method as the parent class, use the keyword super. Super will invoke the same name method in the parent class that is overridden by the current method.
Format:
Super (definition of content, ...)
Omitting parentheses and arguments will pass the parameters of the current method intact to the same name method in the parent class. If you do not want to use parameters when calling, use parentheses to explicitly mark them, like super ().
Finally create an object belonging to this class (example)--
Third, the creation of examples
Format:
Instance name = Class name. New
The instance has finally been created, and now it's time to manipulate it.
Iv. invocation of the method
In Ruby, we pass the dots. To invoke a method of an object
Format:
The created instance name. Method Name
If you want to create an instance of a temporary existence and add a method to it:
(class name. New). Method name
Or:
Class name. New method Name
Copy CodeThe code is as follows:
Class NPC
def intro
Print "I am NPC?"
End
End
Class LIOUMANG<NPC
Def talk
Print "XX in the south of the village"
End
End
Rogue Armour =lioumang.new
Rogue A. Talk #显示 I'm NPC?
Rogue armour. Intro #因为流氓甲继承了它的父类的方法, so show xx in the south of the village
Description
The definition of a class--NPC
-Defines the subclass of an NPC--lioumang
To the NPC define a method called Intro, print "" is used to display the "" in the things. (Don't care how it works)
~ ~ Defines a method called Talk for Lioumang.
Creation Example--Rogue armor
Method of calling talk for Rogue armor
Method of calling intro for Rogue Armor
Two
Copy CodeThe code is as follows:
Class NPC
def intro
Print "AAA"
End
End
Class LIOUMANG<NPC
def intro
Print "BBB"
End
End
Rogue Armour =lioumang.new
Rogue armour. Intro
Description
The display is BBB, visible method of the Shou Hammer cooking terminations 蛞 slaughter swear the Laos muscle 5 curled? 3 rows into Rogue armor =npc.new display AAA
Three
Copy CodeThe code is as follows:
Class NPC
def intro
Print "AAA"
End
Def talk
Print "CCC"
End
End
Class NPC
def intro
Print "BBB"
End
End
Rogue Armour =npc.new
Rogue armour. Intro
Rogue armor. Talk
Description
The first display is the BBB, and then the CCC, visible classes such as repeated definitions, to preserve their intersection of the complement, their intersection to the last time. But if 13 rows become rogue Armor =npc.new then the AAA is displayed.
Four
Copy CodeThe code is as follows:
Class NPC
Def talk (A=nil) #nil为空集
P A
End
End
Class Lioumang < NPC
Def talk (a)
Super (5) # called with 5 as a parameter
Super (a) # called with 5 as a parameter
Super # is called with 5 as a parameter, and the shorthand for super (a)
A = 1
Super # is called with 1 as a parameter, and the shorthand for super (a)
Super () # no parameter calls
End
End
Lioumang.new.talk 5
Description
The effect of the-~ method is to display the variable A, while the A=nil shows nil
Visible: When super parentheses and arguments are omitted, the parameters of the current method are passed intact to the same name method in the parent class. If you do not want to use parameters when calling, use parentheses to explicitly mark them, like super ().
Learn Ruby with Me (GO)