Home > RGSS Script Reference > Scene_Load
Scene_Load
Inherits from: Scene_File
Description: This class handles the aspects of the file selection process that are unique to loading a game.
class Scene_Load < Scene_File
# ------------------------------------
def initialize
$game_temp = Game_Temp.new
$game_temp.last_file_index = 0
latest_time = Time.at(0)
for i in 0..3
filename = make_filename(i)
if FileTest.exist?(filename)
file = File.open(filename, "r")
if file.mtime > latest_time
latest_time = file.mtime
$game_temp.last_file_index = i
end
file.close
end
end
super("Which file do you want to load?")
end
# ------------------------------------
def on_decision(filename)
unless FileTest.exist?(filename)
$game_system.se_play($data_system.buzzer_se)
return
end
$game_system.se_play($data_system.load_se)
file = File.open(filename, "rb")
read_save_data(file)
file.close
$game_system.bgm_play($game_system.playing_bgm)
$game_system.bgs_play($game_system.playing_bgs)
$game_map.update
$scene = Scene_Map.new
end
# ------------------------------------
def on_cancel
$game_system.se_play($data_system.cancel_se)
$scene = Scene_Title.new
end
# ------------------------------------
def read_save_data(file)
characters = Marshal.load(file)
Graphics.frame_count = Marshal.load(file)
$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)
if $game_system.magic_number != $data_system.magic_number
$game_map.setup($game_map.map_id)
$game_player.center($game_player.x, $game_player.y)
end
$game_party.refresh
end
end
|
This class has no properties.
What is the Marshalling Library?
The marshalling library allows objects to be stored so that they can be recovered for later use even after the script terminates. This is done using a process called serialization. The objects are converted into a stream of bytes using the Marshal.dump method, and the bytes are read and reconstituted using the Marshal.load method. If several serialized objects are dumped to the same file, as they are in a RPG Maker XP save, they must be reconstituted in the same order they were dumped in order to avoid errors.
|
Initialize
Arguments: None
Local Variables: None
How it Works: This method checks to see which files exist and sets up the data that will be shown on the load screen. First, this screen will be invoked from the title screen, so a new Game_Temp object needs to be created until the serialized data is loaded. The main body of this method determines which save file was created most recently so that the value of $game_temp.last_file_index can be set to the appropriate file. The latest time is first set to the value of Time.at(0), meaning 0 seconds since 1970. Then the for loop checks each of the four files to see if the they exist. If they do and the last modified time of any file is greater than the latest time at that point, then $game_temp.last_file_index is set the index of that file.
On_Decision
Arguments: None
Local Variables: None
How it Works: This method processes the effects of the user deciding on a file to load. If the file doesn't exist, then the buzzer sound effect is played. If it does, then the load sound effect is played, the file is opened, and the read_save_data method is called to reconstitute the serialized game objects (see below for more information). Control is then transferred to the map on which the game was saved, with the music and BGS from that map playing.
On_Cancel
Arguments: None
Local Variables: None
How it Works: Processes the effects of the user cancelling the load menu. The cancel sound effect is played and the user is returned to the title screen.
Read_Save_Data
Arguments: None
Local Variables: None
How it Works: This method reconstitutes the serialized objects that were saved. The "Marshal" library grabs each serialized object in turn and changes it from a string of bytes into an object that can be understood by the program. The if $game_system.magic_number != $data_system.magic_number clause checks the "magic number" against the magic number stored in the system data. This number is generated by doing some (unknown) transformation on the state of the game data at the time it's saved. This transformation is done again when the data is loaded. If the two values differ, then the game has been edited, so there can be no guarantee that the map is as it was when the game was saved, so the map data is recreated from scratch.
|
|