alias placement question

Started by GasolineWaltz, December 29, 2010, 12:26:37 pm

Previous topic - Next topic

GasolineWaltz

so, I thought that I had a good grasp on aliasing, but then I started thinking about it a little more and confused myself...

example: a basic HUD alias
class Scene_Map
  alias mainHUD_main main
  alias mainHUD_update update
 
  def main
    @window_HUD = Window_HUDMain.new
    mainHUD_main
    @window_HUD.dispose
  end
  def update
    @window_HUD.update
    mainHUD_update
  end
end


so, what is the deal with the aliased method placement? It usually works out for me in the long run, because I can just switch them around if anything is acting up... but what exactly is going on here? Is it that @window_HUD has to be defined before the main method is called so it can be disposed? But if thats the case, why is update above mainHUD_update?

ForeverZer0

December 29, 2010, 12:29:48 pm #1 Last Edit: December 29, 2010, 12:32:48 pm by ForeverZer0
Because in the "main" method, the process is getting caught in a loop, so any code below the normal method will not execute until the loop is broken, ie. the scene ending.



ex.

def main
  blah blah
  loop { update things }
  dispose stuff
end

alias whatever_main main
def main
  aliased blah blah (this is before the normal method is called, which catches it in a loop)
  whatever_main  (calls normal method, starting loop)
  more aliased blah blah (does not execute until whatever_main's loop has broken.
end

I am done scripting for RMXP. I will likely not offer support for even my own scripts anymore, but feel free to ask on the forum, there are plenty of other talented scripters that can help you.

GasolineWaltz

ah, I think I got it.. @window_HUD.update is in #update which gets called in the main loop, so @window_HUD needs to be defined before #main is executed.

ForeverZer0

Yeah, pretty much anytime you ever alias the "main" method in a scene, you have to instantize any objects before calling the normal method. Anything after it will not execute until the scene ends. You can alias the initialize method of scenes if want to avoid the confusion. Although it may not be defined in your script, "new" is a inherited method of the Class class (I didn't mis-type that, by the way). Its called anytime a new instance is created.
I am done scripting for RMXP. I will likely not offer support for even my own scripts anymore, but feel free to ask on the forum, there are plenty of other talented scripters that can help you.

SBR*

Quote from: ForeverZer0 on December 29, 2010, 03:20:51 pm
Yeah, pretty much anytime you ever alias the "main" method in a scene, you have to instantize any objects before calling the normal method. Anything after it will not execute until the scene ends. You can alias the initialize method of scenes if want to avoid the confusion. Although it may not be defined in your script, "new" is a inherited method of the Class class (I didn't mis-type that, by the way). Its called anytime a new instance is created.


But what is the difference between the Class class and the Object class, then (lol, completely off-topic :D)?

Cya :urgh:!

ForeverZer0

Object is the superclass of Class. The only thing above object is Kernel. The hiearchy is basically something like this:

                               / - Class
Kernel --> Object --<
                               \ - Module


As you can see, this is what they mean by "everything is an object" when talking about Ruby. Almost every last little thing is an ancestor of the Object class.

To answer your question, class is really just a basic struct to define a couple (I think it is only 2 or 3) common functions for instances. The Module class is different, it has quite a few functions, but is used for static objects that do not have instances.
I am done scripting for RMXP. I will likely not offer support for even my own scripts anymore, but feel free to ask on the forum, there are plenty of other talented scripters that can help you.

Blizzard

The Class class is a class the describes the structure of a class. Basically when you create a new class, what actually happens is that a new instance of class Class is created into which all information about your class is stored.
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

GasolineWaltz

Quote from: Blizzard on December 29, 2010, 05:07:15 pm
The Class class is a class the describes the structure of a class. Basically when you create a new class, what actually happens is that a new instance of class Class is created into which all information about your class is stored.


brilliant!

just to bring another non sequitur into the mix, what isn't an object in Ruby? Only things that come to mind are blocks? methods?

ForeverZer0

No, Method is a class as well. If you want to learn more about Ruby, google some documentation on. It actually opened up a whole new level of coding for me. Try searching for Ruby 1.8 Core CHM file (what RMXP uses), or the much better 1.9, but you will find a lot that RMXP cannot use, but can be written in yourself if needed.
I am done scripting for RMXP. I will likely not offer support for even my own scripts anymore, but feel free to ask on the forum, there are plenty of other talented scripters that can help you.

GasolineWaltz

December 30, 2010, 02:12:27 am #9 Last Edit: December 30, 2010, 02:14:23 am by GasolineWaltz
hey thanks, I've actually been reading/scripting a lot more with ruby lately than RGSS. I've been cracking open the pick axe, pocket reference and also WHY's excellent reads.

my last post was kind of on an impulse, and so I did a little research... Someone on stackoverflow says that methods aren't objects but then this guy explains the whole sitch a little better

Zeriab

I suggest watching this excellent talk on the ruby object model: http://scotland-on-rails.s3.amazonaws.com/2A04_DaveThomas-SOR.mp4
I learned a bunch from it ^_^

*hugs*

ForeverZer0

Methods are objects. They belong to the Method class. Classes are objects.

http://www.ruby-doc.org/core/classes/Method.html
I am done scripting for RMXP. I will likely not offer support for even my own scripts anymore, but feel free to ask on the forum, there are plenty of other talented scripters that can help you.

Blizzard

You gotta love how they abbreviate it as meth.
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

SBR*

Quote from: Zeriab on December 30, 2010, 04:58:37 am
I suggest watching this excellent talk on the ruby object model: http://scotland-on-rails.s3.amazonaws.com/2A04_DaveThomas-SOR.mp4
I learned a bunch from it ^_^

*hugs*


Wow, such an awesome video! I'm gonna level you up for this :D!

Cya :urgh:!

GasolineWaltz

Quote from: ForeverZer0 on December 30, 2010, 08:51:10 am
Methods are objects. They belong to the Method class. Classes are objects.

http://www.ruby-doc.org/core/classes/Method.html



hahaha damn... that's me just overthinking. I've looked at that page so many times now, and it just never dawned on me in the course of this convo  :shy:

SBR*

December 30, 2010, 11:00:39 am #15 Last Edit: December 31, 2010, 04:23:32 am by SBR*
Quote from: ForeverZer0 on December 30, 2010, 08:51:10 am
Methods are objects. They belong to the Method class. Classes are objects.

http://www.ruby-doc.org/core/classes/Method.html



But Object is a class? Wow, Ruby really is a complicated language :D.

Cya :urgh:!

EDIT: All right, I lost it somewhere. So, if in this script:


class A
end


A is a constant, that is an instance of the Object class, what has happened with the Class class...? *confused*

Cya :urgh:!

Blizzard

Every language is complicated in structure. You shouldn't bother trying to understand things like this right now. It will come to you when you're more experienced.

Yes, the Object class is actually recursive. Object is the super class of Class. Class describes Object itself and the actual definition of Object is an instance of Class. The definition of Class is also an instance of Class. A bit confusing, I know. That's why I said that you shouldn't bother with this stuff now and concentrate on just learning how to use everything.
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.