Advertisement
Black_Mage

Persistent Data Script RMXP

Nov 18th, 2015
1,594
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 5.16 KB | None | 0 0
  1. ################################################################################
  2. # Persistence Data Script By Black Mage (Credit required to use)
  3. # Version : 1.1
  4. #
  5. # https://burningwizard.wordpress.com/2015/11/19/persistent-data-script-rmxp-rgss/
  6. ################################################################################
  7.  
  8. # put [p] somewhere in the switches name or variables name.
  9. # Use script call "$scene = Save_System.new" to store those switches or variables
  10. # on System.rxdata
  11.  
  12. # Each time player enter the game, wether it's on the title screen, new game, or
  13. # continue, those switches or variables that have [p] in their name will have the
  14. # same value with those stored in System.rxdata. The value stored can be changed
  15. # normaly from inside the game. Don't forget to save the value again if changes
  16. # were made.
  17.  
  18. class Persistence_Data
  19.   def initialize
  20.     # Saving the switches
  21.     $game_persistence_switches = []
  22.     @a = 1
  23.     until @a == ($data_system.switches.size - 1)
  24.       if $data_system.variables[@a] == nil
  25.       elsif $data_system.switches[@a].include?("[p]")
  26.         $game_persistence_switches[@a] = $game_switches[@a]
  27.       end
  28.       @a = @a + 1
  29.     end
  30.     @switch = $game_persistence_switches
  31.     @switchlength
  32.    
  33.     # Saving the variables
  34.     $game_persistence_variables = []
  35.     @a = 1
  36.     until @a == ($data_system.variables.size - 1)
  37.       if $data_system.variables[@a] == nil
  38.       elsif $data_system.variables[@a].include?("[p]")
  39.         $game_persistence_variables[@a] = $game_variables[@a]
  40.       end
  41.       @a = @a + 1
  42.     end
  43.     @variable = $game_persistence_variables
  44.     @variablelength
  45.   end
  46.   def switch(number)
  47.     return @switch[number]
  48.   end
  49.   def switch_length
  50.     return  @switchlength
  51.   end
  52.   def variable(number)
  53.     return @variable[number]
  54.   end
  55.   def variable_length
  56.     return  @variablelength
  57.   end
  58. end
  59.  
  60. class Save_System
  61.   def main
  62.     file = File.open("System.rxdata", "wb")
  63.     $persistence_data = Persistence_Data.new
  64.     Marshal.dump($persistence_data, file)
  65.     file.close
  66.     $scene = Scene_Map.new
  67.     return
  68.   end
  69. end
  70.  
  71. class Scene_Load < Scene_File
  72.   #--------------------------------------------------------------------------
  73.   # * Read Party Change Data
  74.   #     file : file object for reading (opened)
  75.   #--------------------------------------------------------------------------
  76.   def read_save_data(file)
  77.     # Read character data for drawing save file
  78.     characters = Marshal.load(file)
  79.     # Read frame count for measuring play time
  80.     Graphics.frame_count = Marshal.load(file)
  81.     # Read each type of game object
  82.     $game_system        = Marshal.load(file)
  83.     $game_switches      = Marshal.load(file)
  84.     $game_variables     = Marshal.load(file)
  85.     $game_self_switches = Marshal.load(file)
  86.     $game_screen        = Marshal.load(file)
  87.     $game_actors        = Marshal.load(file)
  88.     $game_party         = Marshal.load(file)
  89.     $game_troop         = Marshal.load(file)
  90.     $game_map           = Marshal.load(file)
  91.     $game_player        = Marshal.load(file)
  92.     load_persistent    
  93.     # If magic number is different from when saving
  94.     # (if editing was added with editor)
  95.     if $game_system.magic_number != $data_system.magic_number
  96.       # Load map
  97.       $game_map.setup($game_map.map_id)
  98.       $game_player.center($game_player.x, $game_player.y)
  99.     end
  100.     # Refresh party members
  101.     $game_party.refresh
  102.   end
  103.  
  104.   def load_persistent
  105.     if FileTest.exist?("System.rxdata")
  106.       file = File.open("System.rxdata", "rb")
  107.       $persistence_data = Marshal.load(file)
  108.       file.close
  109.       @a = 1
  110.       until @a == ($data_system.switches.size - 1)
  111.         if $data_system.switches[@a].include?("[p]")
  112.           $game_switches[@a] = $persistence_data.switch(@a)
  113.         end
  114.         @a = @a + 1
  115.       end
  116.       @a = 1
  117.       until @a == ($data_system.variables.size - 1)
  118.         if $data_system.variables[@a].include?("[p]")
  119.           $game_variables[@a] = $persistence_data.variable(@a)
  120.         end
  121.         @a = @a + 1
  122.       end
  123.     end
  124.   end
  125.  
  126. end
  127.  
  128. class Scene_Title
  129.   alias black_main main
  130.   def main
  131.     $data_system        = load_data("Data/System.rxdata")
  132.     $game_switches      = Game_Switches.new
  133.     $game_variables     = Game_Variables.new
  134.     load_persistent
  135.     black_main
  136.   end
  137.  
  138.   alias black_new_game command_new_game
  139.   def command_new_game
  140.     black_new_game
  141.     load_persistent
  142.   end
  143.  
  144.   def load_persistent
  145.     if FileTest.exist?("System.rxdata")
  146.       file = File.open("System.rxdata", "rb")
  147.       $persistence_data = Marshal.load(file)
  148.       file.close
  149.       @a = 1
  150.       until @a == ($data_system.switches.size - 1)
  151.         if $data_system.variables[@a] == nil
  152.         elsif $data_system.switches[@a].include?("[p]")
  153.           $game_switches[@a] = $persistence_data.switch(@a)
  154.         end
  155.         @a = @a + 1
  156.       end
  157.       @a = 1
  158.       until @a == ($data_system.variables.size - 1)
  159.         if $data_system.variables[@a] == nil
  160.         elsif $data_system.variables[@a].include?("[p]")
  161.           $game_variables[@a] = $persistence_data.variable(@a)
  162.         end
  163.         @a = @a + 1
  164.       end
  165.     end
  166.   end
  167.  
  168. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement