When to Use the Module Class' include Feature
I have seen many times that people kind of abuse of the :: scope operator when they are fiddling with modules. That inspired me to revisit the modules to make sure they stop repeating themselves like crazy.
Basic Setup Modules
Code:
module SomeUser
module Config
DEFAULT_FONT_SIZE = 20
DEFAULT_TITLE = "Some Label"
end
end
So far there is nothing new to see here but everything changes once we start dealing with a specific Window class. Let us take a look at some fake Window_TitleCommand Add-on.
Code:
class Window_TitleCommand
alias :some_user_config_win_ttl_comm_draw_item :draw_item
def default_font_size
SomeUser::Config::DEFAULT_FONT_SIZE
end
def draw_item(index)
some_user_config_win_ttl_comm_draw_item(index)
contents.font.size = self.default_font_size
text = SomeUser::Config::DEFAULT_TITLE
contents.draw_text(4, 26, contents_width, 24, text, 1)
end
end
As you can see above, we had to use the :: scope operator several times in our scriptlet. Honestly, I got to tell you that it is not elegant at all. The more Constants you include the worse it will look like.
Let's make some modifications to the original code.
Code:
class Window_TitleCommand
include SomeUser::Config
alias :some_user_config_win_ttl_comm_draw_item :draw_item
def draw_item(index)
some_user_config_win_ttl_comm_draw_item(index)
contents.font.size = DEFAULT_FONT_SIZE
contents.draw_text(4, 26, contents_width, 24, DEFAULT_TITLE, 1)
end
end
Wunderbar! Wonderful!
Now it looks quite neat! And we can even skip the creation of needless methods only to call the same long line of code we could find inside the default_font_size over and over again.
There is a caveat, though. You should NOT do that if you are working on a class that inherits the methods you have altered there. This is especially true if at least one of its child classes does not need such modifications at all. I seriously recommend you to only include the modules in child classes and nowhere else unless you know what you are doing.
"For God has not destined us for wrath, but for obtaining salvation through our Lord Jesus Christ," 1 Thessalonians 5:9
Maranatha!
The Internet might be either your friend or enemy. It just depends on whether or not she has a bad hair day.
My Original Stories (available in English and Spanish)
List of Compiled Binary Executables I have published...
HiddenChest & Roole
Give me a free copy of your completed game if you include at least 3 of my scripts!
Just some scripts I've already published on the board...
KyoGemBoost XP VX & ACE, RandomEnkounters XP, KSkillShop XP, Kolloseum States XP, KEvents XP, KScenario XP & Gosu, KyoPrizeShop XP Mangostan, Kuests XP, KyoDiscounts XP VX, ACE & MV, KChest XP VX & ACE 2016, KTelePort XP, KSkillMax XP & VX & ACE, Gem Roulette XP VX & VX Ace, KRespawnPoint XP, VX & VX Ace, GiveAway XP VX & ACE, Klearance XP VX & ACE, KUnits XP VX, ACE & Gosu 2017, KLevel XP, KRumors XP & ACE, KMonsterPals XP VX & ACE, KStatsRefill XP VX & ACE, KLotto XP VX & ACE, KItemDesc XP & VX, KPocket XP & VX, OpenChest XP VX & ACE
Maranatha!
The Internet might be either your friend or enemy. It just depends on whether or not she has a bad hair day.
My Original Stories (available in English and Spanish)
List of Compiled Binary Executables I have published...
HiddenChest & Roole
Give me a free copy of your completed game if you include at least 3 of my scripts!
Just some scripts I've already published on the board...
KyoGemBoost XP VX & ACE, RandomEnkounters XP, KSkillShop XP, Kolloseum States XP, KEvents XP, KScenario XP & Gosu, KyoPrizeShop XP Mangostan, Kuests XP, KyoDiscounts XP VX, ACE & MV, KChest XP VX & ACE 2016, KTelePort XP, KSkillMax XP & VX & ACE, Gem Roulette XP VX & VX Ace, KRespawnPoint XP, VX & VX Ace, GiveAway XP VX & ACE, Klearance XP VX & ACE, KUnits XP VX, ACE & Gosu 2017, KLevel XP, KRumors XP & ACE, KMonsterPals XP VX & ACE, KStatsRefill XP VX & ACE, KLotto XP VX & ACE, KItemDesc XP & VX, KPocket XP & VX, OpenChest XP VX & ACE