Chaos Project

RPG Maker => RPG Maker Scripts => Topic started by: Seox on June 17, 2009, 04:47:36 pm

Title: Mixins
Post by: Seox on June 17, 2009, 04:47:36 pm
Hiya.

Does mixing in a module cause any serious delay, as far as processing speed?

In other words, is there any good reason why I should NOT include most anything that I can with configuration modules which make it FAR easier to script the stupid thing?

Please and thank you ^_^
Title: Re: Mixins
Post by: Ryex on June 17, 2009, 05:10:44 pm
well for one thing your don't have to use
include <module name here>

as long as you start the name with a capital letter, which is what you seemed to be saying.

in answer to your question, no it wont cause noticeable if any delay.
but modules are only good for static Configuration (if you plan on changing the data while in game do something else), and or SIMPLE global methods that you want to use without making a class instance.
Title: Re: Mixins
Post by: Seox on June 17, 2009, 05:28:36 pm
Quote from: Ryexander on June 17, 2009, 05:10:44 pm
well for one thing your don't have to use
include <module name here>

as long as you start the name with a capital letter, which is what you seemed to be saying.

in answer to your question, no it wont cause noticeable if any delay.
but modules are only good for static Configuration (if you plan on changing the data while in game do something else), and or SIMPLE global methods that you want to use without making a class instance.



Thank you ^_^


So I can just go:
class Chicken_pot_pie
Chicken
Vegetables
end


to include a class?

Funny that you should mention the need to use someting else if you want module data to change.... I've been having a problem with that. What I'm doing is trying to fill in a sound effect, which then gets merged into a weapon animation. What I WANT is for it to check the actor's weapon, and give a result based on that, but because it is not instanciated, I can't tell it to branch like that, and so it uses one default sound for all weapons. What should I use?

Thanks, Ryex ^_^ I'm learning a lot. *powers up*
Title: Re: Mixins
Post by: Ryex on June 17, 2009, 05:58:31 pm
Quote from: Seox on June 17, 2009, 05:28:36 pm

So I can just go:
class Chicken_pot_pie
Chicken
Vegetables
end


to include a class?


no lest say you have a module called Go
module Go
  def self.bla
    bla bal
  end
 
  TIME = 100
  NEED = 0

end


so you have the module Go and it has a method called bla and two constants TIME and NEED.

all you would need to do to use them in another class is this

class Chicken_pot_pie
  def bake
    cook_pie(Go::TIME + Go::NEED)
    Go.bla
  end
end
   
end


Go because it has a capital letter is both a constant and a module name this it call be called anywear in the script editor.

Quote from: Seox on June 17, 2009, 05:28:36 pm
Funny that you should mention the need to use someting else if you want module data to change.... I've been having a problem with that. What I'm doing is trying to fill in a sound effect, which then gets merged into a weapon animation. What I WANT is for it to check the actor's weapon, and give a result based on that, but because it is not instanciated, I can't tell it to branch like that, and so it uses one default sound for all weapons. What should I use?

Thanks, Ryex ^_^ I'm learning a lot. *powers up*


what I meant by modules being best for static data is that you should never do this
Go::TIME = 50

you CAN (i think) but it is better to store the data elsewhere if you need to do something like that. modules should hold static data like weapon stats and the like.
Title: Re: Mixins
Post by: Seox on June 17, 2009, 06:03:29 pm
Riiight. Thanks, Ryex ^_^.

I can definitely use the Chicken_pot_pie.bake shortcut. Thanks a ton!

So then, what SHOULD I use in the case of that animation thing? A class doesn't feel right, because I'm not creating any instances.
Title: Re: Mixins
Post by: Aqua on June 17, 2009, 06:09:33 pm
Use a case to check which weapon id?

Lol I didn't really get your explanation @.@
Title: Re: Mixins
Post by: Ryex on June 17, 2009, 06:11:40 pm
take a look at my Weapons unleash skills script i THINK the config might help you as i don't quite know what you want
Title: Re: Mixins
Post by: Seox on June 17, 2009, 06:22:49 pm
Spoiler: ShowHide
#==============================================================================
# Add-On: Guns 1.1
# by Kylock
#------------------------------------------------------------------------------
# This Add-On allows you to use additional animations for guns.
# To use this script, verify if your guns have the attribute with the same
# ID as GUN_ELEMENT in the weapon's tab of your database.
#==============================================================================

module N01
   # $game_actors.animation_id
  # Element for the weapon/skill that has the Gun Animation
  GUN_ELEMENT = 27
  ANIM = []
  # Attack Animation
         ranged_anime = {
         "GUNSHOT" => ["sound", "se",  100, 100, '357_magnum'],}

           
         ANIME.merge!(ranged_anime)
=begin
         "OBJ_ANIM_WEIGHT"
=end
         # Action Sequence
  RANGED_ATTACK_ACTION = {
    "GUN_ATTACK" => ["JUMP_AWAY","WPN_SWING_V","30", "OBJ_ANIM_WEAPON", "WPN_SWING_V",
        "12","WPN_SWING_VL","OBJ_ANIM_L","One Wpn Only",
        "16","Can Collapse","JUMP_TO","COORD_RESET"],}
  ACTION.merge!(RANGED_ATTACK_ACTION)
end

module RPG
  #------------------------------------------------------------------------------
  class Weapon
    alias kylock_guns_base_action base_action
    def base_action
      # If the "Gun" attribute is marked on the weapon's element,
      # the new attack sequence is used
      if $data_weapons[@id].element_set.include?(N01::GUN_ELEMENT)
        return "GUN_ATTACK"
      end
      kylock_guns_base_action
    end
  end
  #------------------------------------------------------------------------------
  class Skill
    alias kylock_sguns_base_action base_action
    def base_action
      # If the "Gun" attribute is marked on the weapon's element,
      # the new attack sequence is used
      if $data_skills[@id].element_set.include?(N01::GUN_ELEMENT)
        return "GUN_ATTACK"
      end
      kylock_sguns_base_action
    end
  end
end


In the VERY beginning, do you see "GUNSHOT" =>
?

That's the hash that defines the sound to play with "gun type" weapons. Problem is, most of the weapons in my game are guns, and I don't want them all to have the same sound. However, I've tried a case-when, where I went:

case whatever_weapon_id
       when 54
          GUNSHOT =>
ETC....


With each defining gunshot. No luck. If it were a class or something, I could give it attr_s, which would allow it to work. But it's not.

It seems like most everything that would work isn't available with a module.


Does that help?



Yeah, Ryex, I'll go check your script ^_^.

And btw, when I was younger, GS was my FAVORITE RPG, hands down.
Title: Re: Mixins
Post by: Aqua on June 17, 2009, 07:22:32 pm
Try this...


  # Attack Animation
weapon = $game_party.actors[0].weapon_id
case weapon
when 1 then return
         ranged_anime = {
         "GUNSHOT" => ["sound", "se",  100, 100, '357_magnum'],}
when 2 then return
         ranged_anime = {
         "GUNSHOT" => ["sound", "se",  100, 100, '358_magnum'],}
end
return          ranged_anime = {
         "GUNSHOT" => ["sound", "se",  100, 100, '350_magnum'],}

           
         ANIME.merge!(ranged_anime)


Might work... might not XD
Title: Re: Mixins
Post by: Seox on June 17, 2009, 07:28:12 pm
Quote from: Aqua on June 17, 2009, 07:22:32 pm
Try this...


  # Attack Animation
weapon = $game_party.actors[0].weapon_id
case weapon
when 1 then return
         ranged_anime = {
         "GUNSHOT" => ["sound", "se",  100, 100, '357_magnum'],}
when 2 then return
         ranged_anime = {
         "GUNSHOT" => ["sound", "se",  100, 100, '358_magnum'],}
end
return          ranged_anime = {
         "GUNSHOT" => ["sound", "se",  100, 100, '350_magnum'],}

           
         ANIME.merge!(ranged_anime)


Might work... might not XD


Line 19. Undefined method "actors" for nil; nilclass.

It's effectively telling me that $game_party is nil? Wtf???

XD. I've got a crazy workaround atm, but this'd be easier.

Meh.
Title: Re: Mixins
Post by: Blizzard on June 18, 2009, 04:13:19 am
Ok... I strongly, STRONGLY recommend avoid using mixins in scripts.

1. If you include it in a class that is dumped into a save file, the save file will be completely corrupted (that means practically beyond repair) when you take out the script.

2. They are a good example of bad coding. The class hierarchy becomes unclear and everything is intertwined. This becomes especially clear when you insert a module somewhere inbetween in the middle of a hierarchy. Use superclasses and subclasses, it's not hard to make a hierarchy that includes those modules' functionalities in a superclass. :/

3. Aliasing methods in a module is a pain and scripts are far less compatible because you need loads of code to check other modules that were included and you need to do it for each other script individually while with aliasing you don't need to know what other scripts there are.

4. The ONLY mixins that makes sense are modules like Enumerable and Comparable. And even those can be coded better without the module.
Title: Re: Mixins
Post by: Seox on June 18, 2009, 11:46:49 am
Quote from: Blizzard on June 18, 2009, 04:13:19 am
Ok... I strongly, STRONGLY recommend avoid using mixins in scripts.

1. If you include it in a class that is dumped into a save file, the save file will be completely corrupted (that means practically beyond repair) when you take out the script.

2. They are a good example of bad coding. The class hierarchy becomes unclear and everything is intertwined. This becomes especially clear when you insert a module somewhere inbetween in the middle of a hierarchy. Use superclasses and subclasses, it's not hard to make a hierarchy that includes those modules' functionalities in a superclass. :/

3. Aliasing methods in a module is a pain and scripts are far less compatible because you need loads of code to check other modules that were included and you need to do it for each other script individually while with aliasing you don't need to know what other scripts there are.

4. The ONLY mixins that makes sense are modules like Enumerable and Comparable. And even those can be coded better without the module.



THAT is what I was looking for. Thanks, Blizz! ^_^


So, with super and sub classes, I'm stuck with the idea that you CAN'T just go into a class and go
"blahblah = blah"
etc

for config, then make subclasses that work well, because of the fact that you'll never create an instance of the superclass. Even though, technically, the subclasses are, and SHOULDN'T need to do it. So, Blizz, could you give me a small example of making a "configuration class?"

And, also, how do you do that if a class is ALREADY a subclass?



Thanks a lot!
Title: Re: Mixins
Post by: Blizzard on June 18, 2009, 11:55:36 am
I usually make a module with configuration methods. *points to any script using module BlizzCFG*

Quote from: Seox on June 18, 2009, 11:46:49 am
And, also, how do you do that if a class is ALREADY a subclass?


What do you mean?
Title: Re: Mixins
Post by: Seox on June 18, 2009, 12:17:55 pm
Quote from: Blizzard on June 18, 2009, 11:55:36 am
I usually make a module with configuration methods. *points to any script using module BlizzCFG*

Quote from: Seox on June 18, 2009, 11:46:49 am
And, also, how do you do that if a class is ALREADY a subclass?


What do you mean?




Ohhhhhh. Ok, correct me if I'm wrong, but you're against mixing in anything BUT configuration? That makes a LOT more sense.

What I was saying is that, if you make a superclass for multiple classes, INSTEAD of mixing in, and you go to apply that to some other classes, but one of them ALREADY HAS a superclass, what do you do?
Title: Re: Mixins
Post by: Blizzard on June 18, 2009, 12:25:55 pm
No, no. Modules aren't just for mixins. You can use modules for so-called static classes.

If there is a class (let's call it A) that already has a superclass (let's call it B), you can simply make the new superclass (class C) and make B derive from A. Or imagine it like this:

class A
end
class B1 < A
end
class B2 < A
end
class B3
end


And now you want to add C somewhere? Simple.

class C
end
class A < C
end
class B1 < A
end
class B2 < A
end
class B3 < C
end


All classes now inherit C. B1 and B2 inherit C through A since A already inherited C. B3 didn't inherit anything so you need to make it inherit C.
Title: Re: Mixins
Post by: Seox on June 18, 2009, 01:04:20 pm
OHHHHH, so you just drop the cookie into the TOP of the chute, and let it fall to the bottom, instead of REBUILDING THE BOTTOM TO ACCEPT A COOKIE?

Thanks, Blizz. That's actually very simple...I feel retarded for not thinking of that. ^_^


Static classes?



So...did you take any college classes on ruby or sommat? How do you know all that you do? When did you start? I'm 16, 17 this month, right now.
Title: Re: Mixins
Post by: Blizzard on June 18, 2009, 01:10:10 pm
Static classes are classes that are never instantiated. I like to consider them utility classes with sets of logically grouped methods. (i.e. module Input which handles and updates user input).

I programmed the first time with 14, but I didn't do much until I was 19 (that's when college started for me). I didn't have Ruby in a course, but Object Oriented concepts are the same in each language. I pretty much know some stuff from college and some other through lots of coding and learning in the last 4 years.
Title: Re: Mixins
Post by: fugibo on June 18, 2009, 01:23:19 pm
Mixins are utilized a LOT more in Ruby proper. For example, when programming in any of Ruby's variants, you may come upon instances when you want to compare some property of an object with another object or Fixnum (the class of Ruby's hard-coded numbers), or you want to mask an object so that it will compare with numbers/strings as if it were one. To do this without mixins, you'd have to manually create each method (>, <, ==, etc) by hand. However, using Ruby's built-in Comparable module:

class Foo
  include Comparable

  def initialize
    @count = 0
  end
  def increase
    @count += 1 # BTW, Ruby could really do with some ++/-- magic; any particular reason it doesn't have this, Blizz?
  end

  # HERE BE THE COMPARISON METHODS!!!!
  def <=>(other)
    @count <=> other # This means that when you use an isntance of Foo "foo," and do "foo > 0" or "0 < foo," it
    # will use the @count variable within foo instead of foo. You can rewrite this method however you want, ie
    # perform mathematical operations, etc
  end
end

As you can see, the bulk of that is my comments ;) You may already know this, so sorry if I wasted my time/insulted you. It's very useful when programming for Ruby on Rails and such. Less so for RMXP, since we're already being forced to combat the horribly awful speed of the interpreter.
Title: Re: Mixins
Post by: Blizzard on June 18, 2009, 01:36:04 pm
No, no problem. But nobody here is going to make a super framework for whatever kind of applications.

And yeah, I'd like to see ++ and -- as well. xD
Title: Re: Mixins
Post by: Seox on June 18, 2009, 01:39:31 pm
Thanks, guys!

Yeah, Longfellow. I knew about comparable, but not completely. I mean, I only knew to the point that I could tell you that it existed, but not the name with 100% accuracy, and that it was an optionally mixin-able module.


Blizz...you started at 14??!?!

O_o
Title: Re: Mixins
Post by: Blizzard on June 18, 2009, 01:55:32 pm
Yeah. I made some kind of textual RPG engine for two players fighting each other in QBasic. The code sucked major ass (1500 lines I think). I also did some small select-a-choice adventures where you are trying to find some big treasure and mostly end up dying. I don't usually mention it since it wasn't really critical to learning how to code because I had forgotten most of it until college.

@WcW: Also:

Quote from: Blizzard on June 18, 2009, 04:13:19 am
4. The ONLY mixins that makes sense are modules like Enumerable and Comparable. And even those can be coded better without the module.


Well, they might not be coded better without the module. But then again, it all depends.
Title: Re: Mixins
Post by: fugibo on June 18, 2009, 02:14:54 pm
I started in RMXP at 12. I really, really sucked though, mainly because I was never willing to stop and learn, just play around with scripts all the time. :(
Title: Re: Mixins
Post by: Ryex on June 18, 2009, 02:20:23 pm
I started when i was 14 too I'm 17 now.

aaah static classes! that is what they're called!
Title: Re: Mixins
Post by: fugibo on June 18, 2009, 02:22:35 pm
There's a 9-year-old somewhere in Africa with a MS IT license thingy.

Also, split-topic-time?
Title: Re: Mixins
Post by: Seox on June 18, 2009, 03:06:18 pm
Meh. Blizz'd know.


So, aren't static classes pretty much ANY module? Since they're never instanciated? I'm sure there's more criteria than that, but not sure as to what. Are they stand alone, in that they're not just a bunch of attributes for mixing in? (Random tangent: I don't see the point of class methods, and modules have some of their own. What exactly is the difference between instance and class methods? I know that they're called Class.method, or similar, but not why they're different.)


Thanks for tolerating me, XD!
Title: Re: Mixins
Post by: fugibo on June 18, 2009, 03:08:05 pm
Class methods == purely organizational purposes. Class methods/variables are so that you can organize code relating to a class within the class itself.
Title: Re: Mixins
Post by: Seox on June 18, 2009, 04:41:13 pm
Quote from: Longfellow on June 18, 2009, 03:08:05 pm
Class methods == purely organizational purposes. Class methods/variables are so that you can organize code relating to a class within the class itself.


May I please have an example? I usually understand underlying concepts very well, but not the application of those same concepts until I have a tangible example to study.

Please and thank you ^_^
Title: Re: Mixins
Post by: fugibo on June 18, 2009, 04:42:15 pm
Look at File, IO, Time, etc in the standard library.

Also, what resource/resources are you using? It sounds as if you're using a book or something (or maybe Pragmatic Programmers?)
Title: Re: Mixins
Post by: Blizzard on June 19, 2009, 04:15:08 am
Class variables and methods can be called without instantiating the class. In other words as if the class was a module. They are practically static methods and they are the same for one class and all instances of that class. Instance variables and methods become available once you instantiate an object. Each object has its own instance variables.

class A
  @@common = 1
  def self.show_class
    p @@common
  end
  def set2
    @test = 2
  end
  def set3
    @test = 3
  end
  def mess_up(x)
    @@common = x
  end
  def show
    p @test
  end
end

a1 = A.new
a2 = A.new
a1.set2
a2.set3
a1.show # 2
a2.show # 3
A.show_class # 1
a2.mess_up(7)
A.show_class # 7
a1.mess_up(5)
A.show_class # 5



And modules are all static, yes.
Title: Re: Mixins
Post by: Seox on June 19, 2009, 11:28:56 am
Quote from: Longfellow on June 18, 2009, 04:42:15 pm
Look at File, IO, Time, etc in the standard library.

Also, what resource/resources are you using? It sounds as if you're using a book or something (or maybe Pragmatic Programmers?)



O_o. How'd you guess that I was using a book? No, not pragmatic. I didn't have $50 >.>. I just got some $35 one, but will prolly "upgrade" for my birthday (wednesday, W00t!). I've also used SEVERAL internet tuts. Ghetto, yes, but I really am learning. I just completed my game's sidearm system. It's RAWK.



Blizz, thank you for those examples ^_^. I think that clears it up!. That they were many different examples was a big factor, thanks.

Title: Re: Mixins
Post by: Blizzard on June 19, 2009, 12:24:48 pm
I'm going to cover that in my next e-book anyway. xD
Title: Re: Mixins
Post by: fugibo on June 19, 2009, 01:37:09 pm
The fact that you knew the word "mixin." Almost NOBODY who learns Ruby through the intrawabs knows that much about Ruby, at least not that early on (I've learned entirely through use of the 'net, but w/e)
Title: Re: Mixins
Post by: Blizzard on June 19, 2009, 02:19:01 pm
I've come across it a few times early already. I didn't know how that was called until about a year or two ago.
Title: Re: Mixins
Post by: Seox on June 19, 2009, 03:18:32 pm
Quote from: Longfellow on June 19, 2009, 01:37:09 pm
The fact that you knew the word "mixin." Almost NOBODY who learns Ruby through the intrawabs knows that much about Ruby, at least not that early on (I've learned entirely through use of the 'net, but w/e)


XD

Was that a compliment? ( :naughty: ) < XD

Why's poignant guide mentions it. That's the first guide that I used, but it didn't help me much. After I left here for 1-2 months, I looked at it again, and...here I am.

Meh.