Main Menu

Comprehensive Tutorial: Simple Ruby

Started by pacdiggity, August 01, 2011, 12:05:14 PM

0 Members and 1 Guest are viewing this topic.

LoganF

The initialize method gets called whenever the .new method is called on a class object. So doing something like Scene_Menu.new will implicitly invoke the initialized method. Because this method is usually concerned with setting up default properties, it's worth using it in that manner. You can, however, do what you want. Whatever state the object leaves the initialize method is how that object will appear after the .new is called - until you do something else with the object naturally.

The important thing to check is that the method name you use does not overwrite an existing method that shares the same name. For example, the Object class - which is the superclass of all classes in Ruby - has methods like clone and to_s. If you use those names yourself in a class - without aliasing them first - you will end up overwriting them effectively destroying whatever it used to do. So unless this is intentional, it's worth checking the help file to make sure that the name is free.

So to answer the question, yes you can use any name for a method and put anything in the definition. But if the name already exists, it will be overwritten, which can lead to unknown effects such as not setting up the object correctly or destroying the functionality of an object.
(Why do I always feel like it's the end of the world and I'm the last man standing?)

pacdiggity

Specific methods are designed to do specific things in RGSS. For example, in some Game classes and all Window and Scene classes, the update method is always used to keep the class' information up to date. The refresh method is called to change the data, what's displayed, etc.. In Scene classes, the main method is used to run the game (VXA handles this differently, though. It uses the SceneManager module to do this), and start is used to set up the scene. Thus, when creating a new Window or Scene class, if you overwrite update, main, or start, make sure you call super. Super calls the method with the same name from the superclass. I really should go over that in the tutorial.
it's like a metaphor or something i don't know

modern algebra

Quote from: Pacman on April 05, 2012, 09:15:27 AMIn Scene classes, the main method is used to run the game (VXA handles this differently, though. It uses the SceneManager module to do this)

What do you mean? In RMVXA, the #main method of Scene classes is still used to process scenes; it's just called from the SceneManager module.

pacdiggity

I meant that it differs in that VX's main loop calls several methods in its' run (all setting up for and shutting down after the game's processing), one of which is $scene.main, and VXA only calls one, rggs_main{SceneManager.run}. Everything is handled by the SceneManager, but in VX only the Scene's processing is handled by $scene.main.

In other words, I'm not quite sure what I meant.
it's like a metaphor or something i don't know

LoganF

You're confusing #main with the scripts called Main I think. These Mains are typically run first because the code is not bound within a method and is executed immediately - like the attr_x calls and alias_method are. If you do similar things, like the ever popular


($imported ||= {})[:CUSTOMSCRTIPXYZ] = true


then these end up running before Main script is compiled and executed, providing they are above that script. Any scripts below don't execute because Main has told it to call other methods and a cycle continues until the program is closed. That's why the scripts below it are never called - they never get chance to be seen.

The class method called #main isn't the same as calling the contents of the Main script. #main is a method that resides in Scene_Base and is called via $scene.main more often than not - where $scene is simply a variable containing the current Scene object. In #main are the methods for setting up the transition between one scene object and the next, the executing of the current $scene object and the final processing (like deleting any resources) before the object is deleted and the new scene object enters the - well - scene.

When you compare the two #main methods they may look different, but they aren't really. VXA just bundles parts of the method into other methods, whereas VX just lays it all out in #main.

Regarding the differences between the two Main script executions: VX places it's code in a begin/end layout, to show where essentially a block of code exists to be executed in which exceptions and errors can be watched out for. Should an error occur during runtime, it is caught and rescued to give the error notice.

In VXA, the built in function rgss_main is called with SceneManager.run as it's block argument. rgss_main is given it's name by Enterbrain to show where the code begins but all it really does is run the block contents once. You can use it as many times as you want to should you have a use for it. However, it does have the added ability to detect the F12 key being pressed, in which the block is then executed from the beginning again. If you used rgss_main after the default used one in Main, you would likely lose the capability to reset the game using F12, so extreme care should be taken if you ever wanted to use that method for something.
(Why do I always feel like it's the end of the world and I'm the last man standing?)

MagitekElite

I hope that since this is stickied I am allowed to post in it even if it is a few months old.  :-[

I wanted to say this was the most comprehensive tutorial on (basic) Ruby I have ever read, and I've been searching for a lot of them for a while now. All the other tutorials seemed to have been written towards people with some knowledge of Ruby, RGSS or whatnot, at least for the ones I've seen. I was surprised I understood it so well because I read things with difficulty (dyslexic). So I'm very happy!  ^-^

Thanks for writing and sharing with us!  :lol:

I hope to see some form of updates in the future!
Winter is Coming


pacdiggity

Thanks! The age of this topic is in no way relevant to replies.

I would definitely expand on this tutorial if I knew exactly where to go with it. I'm not sure what to do next.
it's like a metaphor or something i don't know

MagitekElite

I've seen some lists of "Basic Ruby" before on the web and have seen them mention things called range, loops and conditions included in it. Would they be basic? If so, maybe that...? :)
Winter is Coming


Irock

This is a good tutorial, Pacman. I don't know where you should expand this to, but you totally should.

pacdiggity

it's like a metaphor or something i don't know