Mixins

Started by Seox, June 17, 2009, 04:47:36 pm

Previous topic - Next topic

Seox

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 ^_^
... (<<<<<<<<<<<<<<< TEH DOTS OF DOOM. Hey, kinda catchy. :naughty:)

Ryex

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.
I no longer keep up with posts in the forum very well. If you have a question or comment, about my work, or in general I welcome PM's. if you make a post in one of my threads and I don't reply with in a day or two feel free to PM me and point it out to me.<br /><br />DropBox, the best free file syncing service there is.<br />

Seox

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*
... (<<<<<<<<<<<<<<< TEH DOTS OF DOOM. Hey, kinda catchy. :naughty:)

Ryex

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.
I no longer keep up with posts in the forum very well. If you have a question or comment, about my work, or in general I welcome PM's. if you make a post in one of my threads and I don't reply with in a day or two feel free to PM me and point it out to me.<br /><br />DropBox, the best free file syncing service there is.<br />

Seox

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.
... (<<<<<<<<<<<<<<< TEH DOTS OF DOOM. Hey, kinda catchy. :naughty:)

Aqua

Use a case to check which weapon id?

Lol I didn't really get your explanation @.@

Ryex

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
I no longer keep up with posts in the forum very well. If you have a question or comment, about my work, or in general I welcome PM's. if you make a post in one of my threads and I don't reply with in a day or two feel free to PM me and point it out to me.<br /><br />DropBox, the best free file syncing service there is.<br />

Seox

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.
... (<<<<<<<<<<<<<<< TEH DOTS OF DOOM. Hey, kinda catchy. :naughty:)

Aqua

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

Seox

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.
... (<<<<<<<<<<<<<<< TEH DOTS OF DOOM. Hey, kinda catchy. :naughty:)

Blizzard

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.
Check out our game brands:

Daygames
Game Night Games
Chugnar Games

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.

Seox

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!
... (<<<<<<<<<<<<<<< TEH DOTS OF DOOM. Hey, kinda catchy. :naughty:)

Blizzard

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?
Check out our game brands:

Daygames
Game Night Games
Chugnar Games

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.

Seox

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?
... (<<<<<<<<<<<<<<< TEH DOTS OF DOOM. Hey, kinda catchy. :naughty:)

Blizzard

June 18, 2009, 12:25:55 pm #14 Last Edit: June 18, 2009, 12:27:17 pm by Blizzard
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.
Check out our game brands:

Daygames
Game Night Games
Chugnar Games

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.

Seox

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.
... (<<<<<<<<<<<<<<< TEH DOTS OF DOOM. Hey, kinda catchy. :naughty:)

Blizzard

June 18, 2009, 01:10:10 pm #16 Last Edit: June 18, 2009, 01:11:23 pm by Blizzard
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.
Check out our game brands:

Daygames
Game Night Games
Chugnar Games

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.

fugibo

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.

Blizzard

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
Check out our game brands:

Daygames
Game Night Games
Chugnar Games

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.

Seox

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
... (<<<<<<<<<<<<<<< TEH DOTS OF DOOM. Hey, kinda catchy. :naughty:)