$imported = {} if $imported.nil? - What does it do

When I am using someone's script, say... Yami, Yanfly, Victor or Tsukihime's scripts...

I often notice this:

$imported = {} if $imported.nil?for example:

$imported = {} if $imported.nil?$imported["HeartFire"] = trueCan someone give me an elaboration what it does and what it means?

I tried erasing this line of code on Yami's script and tried to run the script and it still works...

so does it mean its something placed by the scripter to define their scripts?
The first line is to make sure that the global hash variable exist. If nil, then declare hash variable.

The second line is to notify other script, that hey this "LalaLand" script exists. Why notifying other scripts about your existence is useful?

By knowing what other script exists in the script panels, you can do compatible check and requirement check.

if $imported["LalaLand"] #if LalaLand script exist #do somethingelse #do something elseendCompatible check usually is done by doing more stuff than necessary because other script might alter the required data.

Requirement check is used to make sure that the core/parent/base script is there because child/derivative script can't work without their parents.
Are they best suited to be placed on Core Script / Base Script then, rather than placing it in every script?
Those hashes are there for compatibility issues between scripts of different people.


The first part (the hash creation) should be in any script from you where you think it might create compatibility problems with other people's scripts, so that they can check for your script when they write compatibility fixes for their scripts.


It might be in every of your scripts (even those that should not cause compatibility issues) as a courtesy and precaution.


The second part (checking for the presence of other scripts and providing compatibility fixes) should be there if you create the compatibility fix in your script.


Or it should be there if you check for other scripts you created yourself, for example if your current script is an add-on that requires your other script (or the other script of someone else if you write an add-on for another scripter's work.
Ahhh, so for example, I have a script called:

Animated Battlers

and created this:

$imported = {} if $imported.nil?$imported["AnimationBugFix"] = truethe AnimationBugFix is another set of script or a script that allows another configuration, or some sort of an add-on...?
I define it, but I typically don't use it.

If I need to do anything like

def myMethod #something do_this if $imported[:something] do_that if $imported[:something_else] #more codeendThen I am more often than not writing terrible code.f my script was meant to be an add-on for another script I would not even check these things in my method. I just assume it is there, or exit the game before it even begins if it's not there. If people want to use an add-on, and NOT use the required script, well, that's not how an add-on works.

I don't know why some people check for a whole bunch of different scripts, in one script. Just write separate scripts then!

the AnimationBugFix is another set of script or a script that allows another configuration, or some sort of an add-on...?
No, you're just putting a string in a hash. It doesn't do anything.
Think of thee $imported variable as giving your script a "name" that other scripts can recognize. If other scripts are checking for a particular name and find it, then they can do something instead of the default behavior(mainly compatibility insurance)

Overusing $imported checks can make very messy code, one way to use it would be for add-ons, checking to make sure the main script is there. Some people may wrap their code in an "if $imported[:some_script]", I settled on having something like the following just below customization options if needed.

Code:
$imported = {} if $imported.nil?$imported[:Sel_Equip_Leveling_Base] = trueunless $imported["TH_InstanceItems"]  msgbox("Tsukihime's Instance not detected, exiting")  exitend
The above is from my leveling base script, it initializes the $imported variable if needed, and runs a check for Tsukihime's Instance Items, if it's not there it displays a message telling you what's missing before exiting. If I didn't have that check and you used my script without Tsukihime's, you would get, I think an undefined method error, which isn't as helpful as the message I made.
That explains a lot. Thanks everyone. Now I understand why scripters put that in their scripts.
 You can use this code: 

Code:
# To save one script, for example($imported ||= {})[:symbol_name] = true# Method to verifying, if the script it is registered.class Object  def imported(symbol, &block)    return if $imported.nil?    block.call if $imported.has_key?(symbol) and block_given?  endend# Use so.imported(:symbol_name) { # Only execute the block, if have the script registered.msgbox("Ok, script registered!") }

Original thread: https://forums.rpgmakerweb.com/threads/imported-if-imported-nil-what-does-it-do.23387/