$imported and What it Does
From PocketWiki
by Shanghai
Those of you who have taken some time out of your day to look at a KGC Software script or a Yanfly Engine may have noticed that there's a piece of code in it that goes something like this:
$imported = {} if $imported == nil $imported["SomeRandomName"] = true
Even new and experienced programmers or scripters would look at this and think, "What the heck does that do?" Well, on its own, it does nothing. However, with multiple scripts that use these, they serve as something rather majestic. They function as little flags for compatibility.
How is it Used?
The $imported global hash is mostly used by Japanese RPG Maker XP/VX scripters. Since it's impossible to know what kinds of scripts will be made out there and when they'll be made, it's difficult to plan ahead to shape your script accordingly to maintain maximum compatibility. With the $imported global hash, scripters use it to mark their own scripts with a certain keyword. This keyword allows other scripters to check and see if it's available. If it is, then they can code a different way. If it isn't, they will work around it.
$imported in Action
Let's look at a few examples. We'll be taking a look at Yanfly's Battle Engine Melody and my Defend Heal scripts. Both scripts use the $imported global hash. However, Yanfly's keyword looks like such:
$imported = {} if $imported == nil $imported["BattleEngineMelody"] = true
whereas mine looks like such:
$imported = {} if $imported == nil $imported["DefendHPMPGain"] = true
Defend Heal is a script where if an actor or enemy defends, that battler will regain a bit of HP and MP back. While this works on its own with Battle Engine Melody, I wanted to do a bit more with it by making HP and MP pop up, a feature that exists with Battle Engine Melody but not the default battle system. And so, I make the following code:
def execute_action_guard hp_healed = @active_battler.maxhp * SSS::HP_GAIN / 100 mp_healed = @active_battler.maxmp * SSS::MP_GAIN / 100 @active_battler.hp += hp_healed @active_battler.mp += mp_healed hptext = sprintf(YEM::BATTLE_ENGINE::POPUP_SETTINGS[:hp_heal], hp_healed) mptext = sprintf(YEM::BATTLE_ENGINE::POPUP_SETTINGS[:mp_heal], mp_healed) @active_battler.create_popup(hptext, "HP_HEAL") @active_battler.create_popup(mptext, "MP_HEAL") execute_action_guard_sss_dhmg return unless SSS::SHOW_DEFEND_MESSAGE format = SSS::DEFEND_MESSAGE_FORMAT text = sprintf(format, @active_battler.name, hp_healed, Vocab.hp, mp_healed, Vocab.mp) @message_window.add_instant_text(text) wait(45) end
If I run this code by itself inside of the default battle system without Battle Engine Melody installed, I will get errors saying the YEM::BATTLE_ENGINE module does not exist or other such errors. Since Battle Engine Melody might not necessarily be installed in every RPG Maker project, I cannot always assume that Defend Heal will be compatible. So, inside of Defend Heal, I made a small check to see if Battle Engine Melody is present inside of the execute_action_guard method.
def execute_action_guard hp_healed = @active_battler.maxhp * SSS::HP_GAIN / 100 mp_healed = @active_battler.maxmp * SSS::MP_GAIN / 100 @active_battler.hp += hp_healed @active_battler.mp += mp_healed if $imported["BattleEngineMelody"] end execute_action_guard_sss_dhmg return unless SSS::SHOW_DEFEND_MESSAGE format = SSS::DEFEND_MESSAGE_FORMAT text = sprintf(format, @active_battler.name, hp_healed, Vocab.hp, mp_healed, Vocab.mp) @message_window.add_instant_text(text) wait(45) end
Do you see where the $imported global hash is? It's replaced these four lines of code for the time being:
hptext = sprintf(YEM::BATTLE_ENGINE::POPUP_SETTINGS[:hp_heal], hp_healed) mptext = sprintf(YEM::BATTLE_ENGINE::POPUP_SETTINGS[:mp_heal], mp_healed) @active_battler.create_popup(hptext, "HP_HEAL") @active_battler.create_popup(mptext, "MP_HEAL")
Now, I'm not going to get rid of those four lines of code. Not at all. Instead, I'm going to place it inside of the the if $imported["BattleEngineMelody"] portion of the method so it will look like such:
def execute_action_guard hp_healed = @active_battler.maxhp * SSS::HP_GAIN / 100 mp_healed = @active_battler.maxmp * SSS::MP_GAIN / 100 @active_battler.hp += hp_healed @active_battler.mp += mp_healed if $imported["BattleEngineMelody"] hptext = sprintf(YEM::BATTLE_ENGINE::POPUP_SETTINGS[:hp_heal], hp_healed) mptext = sprintf(YEM::BATTLE_ENGINE::POPUP_SETTINGS[:mp_heal], mp_healed) @active_battler.create_popup(hptext, "HP_HEAL") @active_battler.create_popup(mptext, "MP_HEAL") end execute_action_guard_sss_dhmg return unless SSS::SHOW_DEFEND_MESSAGE format = SSS::DEFEND_MESSAGE_FORMAT text = sprintf(format, @active_battler.name, hp_healed, Vocab.hp, mp_healed, Vocab.mp) @message_window.add_instant_text(text) wait(45) end
Now, the way this method will function is, when the actor or enemy guards, it will perform its usual guard stuff along with the healing. When it reaches the $imported portion of the code, the game will check to see if the Battle Engine Melody keyword exists inside of it. If it does, then it will perform the four extra lines to produce an HP and MP popup for the defending battler.
Finishing Words
This technique was adopted from the Japanese RPG Maker scripters. While it may not be the best or cleanest method, it is still widely used amongst the more well known scripters to boost the compatibility amongst their scripts. This is the "secret" as to why so many of KGC Software scripts and Yanfly Engine scripts are so compatible with themselves and each other.
