Black_Mage icon

Persistent Data Script RMXP

Black_Mage | PRO | 11/19/15 02:52:30 AM UTC | 0 ⭐ | 2104 👁️ | Never ⏰ | []
Ruby |

5.16 KB

|

None

|

0 👍

/

0 👎

################################################################################
# Persistence Data Script By Black Mage (Credit required to use)
# Version : 1.1
#
# https://burningwizard.wordpress.com/2015/11/19/persistent-data-script-rmxp-rgss/
################################################################################
 
# put [p] somewhere in the switches name or variables name.
# Use script call "$scene = Save_System.new" to store those switches or variables 
# on System.rxdata
 
# Each time player enter the game, wether it's on the title screen, new game, or 
# continue, those switches or variables that have [p] in their name will have the
# same value with those stored in System.rxdata. The value stored can be changed
# normaly from inside the game. Don't forget to save the value again if changes
# were made.
 
class Persistence_Data
  def initialize
    # Saving the switches
    $game_persistence_switches = []
    @a = 1
    until @a == ($data_system.switches.size - 1)
      if $data_system.variables[@a] == nil
      elsif $data_system.switches[@a].include?("[p]")
        $game_persistence_switches[@a] = $game_switches[@a]
      end
      @a = @a + 1
    end
    @switch = $game_persistence_switches
    @switchlength
    
    # Saving the variables
    $game_persistence_variables = []
    @a = 1
    until @a == ($data_system.variables.size - 1)
      if $data_system.variables[@a] == nil
      elsif $data_system.variables[@a].include?("[p]")
        $game_persistence_variables[@a] = $game_variables[@a]
      end
      @a = @a + 1
    end
    @variable = $game_persistence_variables
    @variablelength
  end
  def switch(number)
    return @switch[number]
  end
  def switch_length
    return  @switchlength
  end
  def variable(number)
    return @variable[number]
  end
  def variable_length
    return  @variablelength
  end
end
 
class Save_System
  def main
    file = File.open("System.rxdata", "wb")
    $persistence_data = Persistence_Data.new
    Marshal.dump($persistence_data, file)
    file.close
    $scene = Scene_Map.new
    return
  end
end
 
class Scene_Load < Scene_File
  #--------------------------------------------------------------------------
  # * Read Party Change Data
  #     file : file object for reading (opened)
  #--------------------------------------------------------------------------
  def read_save_data(file)
    # Read character data for drawing save file
    characters = Marshal.load(file)
    # Read frame count for measuring play time
    Graphics.frame_count = Marshal.load(file)
    # Read each type of game object
    $game_system        = Marshal.load(file)
    $game_switches      = Marshal.load(file)
    $game_variables     = Marshal.load(file)
    $game_self_switches = Marshal.load(file)
    $game_screen        = Marshal.load(file)
    $game_actors        = Marshal.load(file)
    $game_party         = Marshal.load(file)
    $game_troop         = Marshal.load(file)
    $game_map           = Marshal.load(file)
    $game_player        = Marshal.load(file)
    load_persistent    
    # If magic number is different from when saving
    # (if editing was added with editor)
    if $game_system.magic_number != $data_system.magic_number
      # Load map
      $game_map.setup($game_map.map_id)
      $game_player.center($game_player.x, $game_player.y)
    end
    # Refresh party members
    $game_party.refresh
  end
  
  def load_persistent
    if FileTest.exist?("System.rxdata")
      file = File.open("System.rxdata", "rb")
      $persistence_data = Marshal.load(file)
      file.close
      @a = 1
      until @a == ($data_system.switches.size - 1)
        if $data_system.switches[@a].include?("[p]")
          $game_switches[@a] = $persistence_data.switch(@a)
        end
        @a = @a + 1
      end
      @a = 1
      until @a == ($data_system.variables.size - 1)
        if $data_system.variables[@a].include?("[p]")
          $game_variables[@a] = $persistence_data.variable(@a)
        end
        @a = @a + 1
      end
    end
  end
 
end
 
class Scene_Title
  alias black_main main
  def main
    $data_system        = load_data("Data/System.rxdata")
    $game_switches      = Game_Switches.new
    $game_variables     = Game_Variables.new
    load_persistent
    black_main
  end
  
  alias black_new_game command_new_game
  def command_new_game
    black_new_game
    load_persistent
  end
 
  def load_persistent
    if FileTest.exist?("System.rxdata")
      file = File.open("System.rxdata", "rb")
      $persistence_data = Marshal.load(file)
      file.close
      @a = 1
      until @a == ($data_system.switches.size - 1)
        if $data_system.variables[@a] == nil
        elsif $data_system.switches[@a].include?("[p]")
          $game_switches[@a] = $persistence_data.switch(@a)
        end
        @a = @a + 1
      end
      @a = 1
      until @a == ($data_system.variables.size - 1)
        if $data_system.variables[@a] == nil
        elsif $data_system.variables[@a].include?("[p]")
          $game_variables[@a] = $persistence_data.variable(@a)
        end
        @a = @a + 1
      end
    end
  end
  
end

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎