IceDragon200

[VXA]variable_store.rb

Nov 14th, 2013
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.76 KB | None | 0 0
  1. #
  2. # kode-xchange/lib/variable_store.rb
  3. #   by IceDragon
  4. # Due to limitations in the RGSS3 Player, (finalizers are not called when
  5. # the player closes the Game Window)
  6. # You should place:
  7. #   DataManager.save_system_variables
  8. # In a [Script Command] whenever you wish to save the current variables
  9. # STORE.
  10. #
  11. # EG.
  12. #   After a cutscene event
  13. module VariableStore
  14.  
  15.   # add the variable_ids and switch_ids
  16.   # you wish to save and restore regardless of game state
  17.   #   eg.
  18.   # VARIABLES = [1, 2, 3, 4, 5, 6, 7]
  19.   # SWITCHES = [1, 2, 3, 4, 5, 6, 7]
  20.   SWITCHES  = [ 1 ]
  21.   VARIABLES = [ 1 ]
  22.  
  23. end
  24.  
  25. module DataManager
  26.   class << self
  27.  
  28.     SYS_VAR_FILENAME = "save_glob.rvdata2"
  29.  
  30.     def save_system_variables
  31.       var_hsh = Hash[VariableStore::VARIABLES.map { |v| [v, $game_variables[v]] }]
  32.       swi_hsh = Hash[VariableStore::SWITCHES.map { |v| [v, $game_switches[v]] }]
  33.       hsh = { variables: var_hsh, switches: swi_hsh }
  34.       save_data(hsh, SYS_VAR_FILENAME)
  35.     end
  36.  
  37.     def load_system_variables
  38.       if File.exist?(SYS_VAR_FILENAME)
  39.         hsh = load_data(SYS_VAR_FILENAME)
  40.         hsh[:variables].each { |(k, v)| $game_variables[k] = v.to_i }
  41.         hsh[:switches].each { |(k, v)| $game_switches[k] = v.to_i }
  42.       end
  43.     end
  44.  
  45.     alias :no_sys_var_create_game_objects :create_game_objects
  46.     def create_game_objects(*args, &block)
  47.       no_sys_var_create_game_objects(*args, &block)
  48.       load_system_variables
  49.     end
  50.  
  51.     alias :no_sys_var_save_game :save_game
  52.     def save_game(*args, &block)
  53.       no_sys_var_save_game(*args, &block)
  54.       save_system_variables
  55.     end
  56.  
  57.   end
  58. end
  59.  
  60. class Scene_Gameover
  61.  
  62.   alias :no_sys_var_start :start
  63.  
  64.   def start
  65.     no_sys_var_start
  66.     DataManager.save_system_variables
  67.   end
  68.  
  69. end
Advertisement
Add Comment
Please, Sign In to add comment