Advertisement
Archeia

Global Save File / Persistent Variables for RPG Maker VXA

Aug 25th, 2018
634
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 2.80 KB | None | 0 0
  1. #~ Dumps certain variables into a global save file. In order to them to be
  2. #~ dumped, tag their name (the name in the editor) with an * (asterisk).
  3.  
  4. #~ ~Kread
  5.  
  6. GLOBAL_SAVEFILE_NAME = 'Global_Data.rvdata2'
  7.  
  8. #==============================================================================
  9. # ** DataManager
  10. #==============================================================================
  11.  
  12. module DataManager
  13.   #--------------------------------------------------------------------------
  14.   # * Create the game objects
  15.   #--------------------------------------------------------------------------
  16.   class << self; alias_method(:krx_gsf_dl_cgo, :create_game_objects); end
  17.   def self.create_game_objects
  18.     krx_gsf_dl_cgo
  19.     $game_system.read_global_savefile(GLOBAL_SAVEFILE_NAME)
  20.   end
  21. end
  22.  
  23. #==============================================================================
  24. # ** Game_System
  25. #==============================================================================
  26.  
  27. class Game_System
  28.   #--------------------------------------------------------------------------
  29.   # * Write the global savefile
  30.   #--------------------------------------------------------------------------  
  31.   def write_global_savefile(filename)
  32.     contents = []
  33.     (1..$game_variables.size).each do |i|
  34.       next unless $data_system.variables[i].include?('*')
  35.       contents.push([i, $game_variables[i]])
  36.     end
  37.     File.open(filename, 'wb') do |file|
  38.       Marshal.dump(contents, file)
  39.     end
  40.     true
  41.   end
  42.   #--------------------------------------------------------------------------
  43.   # * Read the global savefile
  44.   #--------------------------------------------------------------------------  
  45.   def read_global_savefile(filename)
  46.     contents = nil
  47.     if FileTest.exist?(filename)
  48.       File.open(filename, 'rb') {|file| contents = Marshal.load(file)}
  49.     end
  50.     return false unless contents
  51.     contents.each {|array| $game_variables[array[0]] = array[1]}
  52.     true
  53.   end
  54. end
  55.  
  56. #==============================================================================
  57. # ** Game_Variables
  58. #==============================================================================
  59.  
  60. class Game_Variables
  61.   #--------------------------------------------------------------------------
  62.   # * Returns the data size
  63.   #--------------------------------------------------------------------------
  64.   def size
  65.     @data.size
  66.   end
  67.   #--------------------------------------------------------------------------
  68.   # * Assigns a value
  69.   #--------------------------------------------------------------------------
  70.   alias_method(:krx_gsf_gv_ass, :[]=)
  71.   def []=(variable_id, value)
  72.     krx_gsf_gv_ass(variable_id, value)
  73.     if $data_system.variables[variable_id].include?('*')
  74.       $game_system.write_global_savefile(GLOBAL_SAVEFILE_NAME)
  75.     end
  76.   end
  77. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement