Advertisement
TheSixth

Global Variables

Jan 7th, 2018
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.98 KB | None | 0 0
  1. =begin
  2.  
  3. - Global Variables
  4. - Made by: Sixth
  5.  
  6. A pretty simple script which will make the specified variables global ones, so
  7. they will share the same value across all play-throughs.
  8.  
  9. Setup the filename and initial data for the variables in the script.
  10. Note that only the specified variables in the @data settings will be set to
  11. global, the rest of them will operate normally.
  12.  
  13. You can check and edit these global variables the same way like any others.
  14.  
  15. Be aware that constantly updating these variables might cause performance loss
  16. depending on the amount and data types of your global variables, and it is not
  17. a good idea to constantly write data onto the HDD in general.
  18. Use these variables with this in mind!
  19.  
  20. =end
  21.  
  22. module GlobVars
  23.  
  24.   # Setup the file path and name here.
  25.   # It can NOT be in an encrypted folder!
  26.   FName = "System/MyFile.rvdata2"
  27.  
  28.   def self.load_vars
  29.     begin
  30.       @data = load_data(FName)
  31.     rescue
  32.       # Setup the initial data for the global variables here.
  33.       # Format: variable_id => value,
  34.       @data = {
  35.         10 => 15,
  36.         13 => 'somedata2',
  37.         24 => [],
  38.         # And so on...
  39.       }
  40.       # Settings end here!
  41.       save_vars
  42.     end
  43.   end
  44.  
  45.   def self.save_vars
  46.     save_data(@data,FName)
  47.   end
  48.  
  49.   def self.data
  50.     return @data
  51.   end
  52.  
  53.   load_vars
  54.  
  55. end
  56.  
  57. class << DataManager
  58.  
  59.   alias load_glob_vars6241 extract_save_contents
  60.   def extract_save_contents(contents)
  61.     load_glob_vars6241(contents)
  62.     $game_variables.load_glob_vars
  63.   end
  64.  
  65. end
  66.  
  67. class Game_Variables
  68.  
  69.   alias load_glob_vars8266 initialize
  70.   def initialize
  71.     load_glob_vars8266
  72.     load_glob_vars
  73.   end
  74.    
  75.   def load_glob_vars
  76.     GlobVars.data.each {|vid,val| @data[vid] = val }
  77.   end
  78.  
  79.   alias save_glob_vars8882 []=
  80.   def []=(vid, val)
  81.     save_glob_vars8882(vid, val)
  82.     if GlobVars.data[vid]
  83.       GlobVars.data[vid] = @data[vid]
  84.       GlobVars.save_vars
  85.     end
  86.   end
  87.  
  88. end
  89.  
  90. # End of script! O.o
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement