Global Variable Standard Check - Yeyinde - 12-07-2008
Global Variable Standard Check
Is your game global friendly?
Version: 1.1
Introduction
After finally losing my patience to all the scripts I see now a days with too many global variables, I decided to make this. My original plan was to have the script delete bad global variables on-the-spot, but Ruby 1.8.1 does not have a global deletion method, so I just had it inform the user that their project has bad variables in it.
Script
Code: #==============================================================================
# ** Global Variable Standard Check
# Is your project global friendly?
#------------------------------------------------------------------------------
# ? Yeyinde, 2007
#------------------------------------------------------------------------------
# This script aims to teach scripters to limit their global variable usage to
# $game_* and $data_* globals. Certain variables like $scene and $DEBUG are
# permitted. What it does is scan for new globals intoduced every graphical
# update. If a bad global is introduced, it warns the user that there are
# globals that should not be in-use.
#==============================================================================
#==============================================================================
# ** ALLOWED_GLOBALS constant
#------------------------------------------------------------------------------
# Encrypted so you don't tamper to get your bad variables in the array
#==============================================================================
ALLOWED_GLOBALS = "JC1hICRzdGRpbiAkLiAkS0NPREUgJC1sICQsICRgICQtdyAkLUkgJD8gJD4g
JCEgJD0gJC1GICRTQUZFICQtcCAkJiAkLXYgJDogJCQgJHN0ZGVyciAkQCAk
QlRFU1QgJCogJDwgJC0wICQrICQ7ICQtZCAkIiAkLWkgJGRlZmVyciAkfiAk
VkVSQk9TRSAkc3Rkb3V0ICRfICQtSyAkUkdTU19TQ1JJUFRTICQwICQvICQn
ICRERUJVRyAkTE9BRF9QQVRIICRGSUxFTkFNRSAkZGVmb3V0ICRcICRzY2Vu
ZQ=="
#==============================================================================
# ** Graphics Module
#------------------------------------------------------------------------------
# Contains the inner graphical handling processes of RMXP
#==============================================================================
module Graphics
class << self
#---------------------------------------------------------------------------
# * Alias Stack Check
#---------------------------------------------------------------------------
unless self.public_method_defined?(:global_check_update)
#-------------------------------------------------------------------------
# * Create Alias (update -> global_check_update)
#-------------------------------------------------------------------------
alias_method :global_check_update, :update
end
#---------------------------------------------------------------------------
# * Frame Update
# This method MUST be called every frame to prevent a lockup
#---------------------------------------------------------------------------
def update
# Run original method
global_check_update
# Preform a global check
Kernel.check_globals
end
end
end
#==============================================================================
# ** Kernel Module
#------------------------------------------------------------------------------
# Hold base functions shared by all objects. Mixed in the Object class.
#==============================================================================
module Kernel
#---------------------------------------------------------------------------
# * Bad Global Class Variable
#---------------------------------------------------------------------------
@@current_bad_globals = []
#---------------------------------------------------------------------------
# * check global variables for bad names
#---------------------------------------------------------------------------
def check_globals
# create a blank array
bad_globals = []
# iterate over all global variables
global_variables.each do |global|
# if variable name does not have $data_, $game_ in it or is not in the
# encrypted array(created from splitting the encrypted constant)
unless (global.include?('$data_') || global.include?('$game_') ||
ALLOWED_GLOBALS.unpack("m")[0].split(' ').include?(global))
# append to the array
bad_globals << global
end
end
# if the current bad variables do not match the new ones
unless @@current_bad_globals === bad_globals
globals = bad_globals.dup
globals.delete_if {|global| @@current_bad_globals.include?(global)}
# Display a message and list all bad variables
print "The following global variable(s) have been flagged because they do not meet naming standards:
#{globals.join(', ')}
(#{globals.size} found)"
end
# set current variables to new ones
@@current_bad_globals = bad_globals
end
#---------------------------------------------------------------------------
# * retrieve current bad global variables
#---------------------------------------------------------------------------
def bad_globals
return @@current_bad_globals
end
# Allow methods to be called with Kernel extension
module_function :check_globals, :bad_globals
end
Instructions
Place this script above main and below the SDK, if using it. It will automatically run as your Graphics do. If at any time a popup... pops up with some global variables in it, close the game and search for those variable names. You should then update the script that uses the variable(s) to use CONSTANTS instead. There is no need for unnecessary globals like $rtab_detected or $default_fonttype.
FAQ
No questions have been frequently asked.
Credits and Thanks
Trickster, who tried to help look for a global deletion method.
Author's Notes
My next plan for this is to allow the script to forcefully remove all bad global variables from the scope. Perhaps I'll write a Ruby Extension and require it...
|