Load/save cache files
#1
Before I show you the routines that save and load data, let me just give you an example of the data to be saved or loaded:

Code:
class Game_File
  attr_accessor :a_record
  def initialize
    a_record = "This is a score"
  end
end
$game_file = Game_File.new
This routine creates an 'object' or class called Game_File. It has only 1 value that I call 'a_record' and once I initialize it, I have it set up with 'This is a score' as a default. After that, I attach this object/class to a global value called $game_file.

This is how they make $game_system, $game_temp, and all those others you access throughout the system.

* * *

Now, I'll show you a simple system you can use to save data.

Code:
file = File.open(filename, "wb")
  Marshal.dump($game_file, file)
file.close
  • This system merely opens a file... assigning a value to the 'file' value. The 'wb' means write in binary.
  • It then performs the 'Marshal.dump' which takes the data from your $game_file value and records it in Ruby's binary language.
  • Then it finally closes your file.
Simple.

* * *


Finally, the way to load your data.

Code:
file = File.open(filename, "rb")
  $game_file = Marshal.load(file)
file.close
  • This system merely opens a file... assigning a value to the 'file' value. The 'rb' means read in binary.
  • It then performs the 'Marshal.load' which gets your data from a file and stores it in the $game_file global value.
  • Then it finally closes your file.
Done
Reply }


Possibly Related Threads…
Thread Author Replies Views Last Post
   Map name as save file name Whisper 9 10,124 05-01-2020, 05:35 PM
Last Post: Whisper
   Weird error when using a custom save script. Steel Beast 6Beets 8 11,355 06-27-2015, 05:54 PM
Last Post: Steel Beast 6Beets
   Custom Save/Load screen Raou 2 7,508 04-01-2015, 08:18 PM
Last Post: Raou
   Script Request - Variables Specific Save File JayRay 2 5,090 04-28-2014, 05:15 AM
Last Post: JayRay
   Save Screen Screenshot Yin 2 6,178 07-05-2010, 06:31 PM
Last Post: Yin
   XP maps saved in RB files kyonides 1 5,997 05-02-2010, 04:43 AM
Last Post: vgvgf
   Help With Law's Save System Ryu-ka 0 3,802 04-06-2010, 06:37 PM
Last Post: Ryu-ka
   Need advice on best/easiest save setup rcg916 3 6,205 11-08-2009, 06:24 AM
Last Post: DerVVulfman
   How can I save a map? Charlie Fleed 3 6,669 07-08-2009, 09:55 PM
Last Post: dacloudster
   Forced Save? Yin 9 13,864 04-18-2009, 08:41 PM
Last Post: DerVVulfman



Users browsing this thread: 1 Guest(s)