Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Global Variable Standard Check
#1
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...
Reply }


Possibly Related Threads…
Thread Author Replies Views Last Post
   Name Game Switch & Variable RG kyonides 0 580 06-27-2023, 09:17 PM
Last Post: kyonides
   Variable Extraction Generator DerVVulfman 0 2,523 07-20-2020, 06:58 PM
Last Post: DerVVulfman
   DoubleX RMVXA Variable Pointers DoubleX 0 3,661 06-01-2015, 03:45 PM
Last Post: DoubleX
   SDK - Standard Development Kit Trickster 5 13,906 06-28-2014, 08:08 PM
Last Post: Legacy
   Variable-Based Battle Themes v1.1 PK8 2 6,567 11-27-2009, 07:29 PM
Last Post: explocion200
   Variable Swap [Snippet] PK8 0 4,198 11-09-2009, 07:32 AM
Last Post: PK8
   Variable-Based Battle Victory MEs PK8 4 7,059 09-29-2009, 12:58 PM
Last Post: Alpha-Mad
   Variable Image Menu Mac 0 4,589 03-08-2008, 01:09 AM
Last Post: Mac
   Variable Logger and Debugger Trickster 0 3,939 03-02-2008, 07:11 AM
Last Post: Trickster



Users browsing this thread: