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

Milena

The woman of many questions
Veteran
Joined
Jan 26, 2014
Messages
1,281
Reaction score
106
First Language
Irish
Primarily Uses
N/A
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?
 

Mike

Veteran
Veteran
Joined
Aug 28, 2013
Messages
316
Reaction score
36
First Language
English
Primarily Uses
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.
 

Milena

The woman of many questions
Veteran
Joined
Jan 26, 2014
Messages
1,281
Reaction score
106
First Language
Irish
Primarily Uses
N/A
Are they best suited to be placed on Core Script / Base Script then, rather than placing it in every script?
 

Andar

Veteran
Veteran
Joined
Mar 5, 2013
Messages
31,365
Reaction score
7,676
First Language
German
Primarily Uses
RMMV
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.
 

Milena

The woman of many questions
Veteran
Joined
Jan 26, 2014
Messages
1,281
Reaction score
106
First Language
Irish
Primarily Uses
N/A
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...?
 

Tsukihime

Veteran
Veteran
Joined
Jun 30, 2012
Messages
8,564
Reaction score
3,846
First Language
English
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.
 
Last edited by a moderator:

Selchar

Veteran
Veteran
Joined
Dec 28, 2012
Messages
299
Reaction score
81
First Language
English
Primarily Uses
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.
 
Last edited by a moderator:

Milena

The woman of many questions
Veteran
Joined
Jan 26, 2014
Messages
1,281
Reaction score
106
First Language
Irish
Primarily Uses
N/A
That explains a lot. Thanks everyone. Now I understand why scripters put that in their scripts.
 

Kvothe

The Bloodness
Veteran
Joined
Jan 21, 2014
Messages
149
Reaction score
557
First Language
Brazil
Primarily Uses
N/A
 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!") }
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Profile Posts

People3_5 and People3_8 added!

so hopefully tomorrow i get to go home from the hospital i've been here for 5 days already and it's driving me mad. I miss my family like crazy but at least I get to use my own toiletries and my own clothes. My mom is coming to visit soon i can't wait to see her cause i miss her the most. :kaojoy:
Couple hours of work. Might use in my game as a secret find or something. Not sure. Fancy though no? :D
Holy stink, where have I been? Well, I started my temporary job this week. So less time to spend on game design... :(
Cartoonier cloud cover that better fits the art style, as well as (slightly) improved blending/fading... fading clouds when there are larger patterns is still somewhat abrupt for some reason.

Forum statistics

Threads
105,868
Messages
1,017,081
Members
137,582
Latest member
Spartacraft
Top