Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Loading data error

I always wonder how to do this.

I release demo of my game twice, and my beta testers complain that they cannot use old save files to continue a game in newer versions; they have to play a whole new game instead. Is it possible to overcome this?

Well, let's say in game v0.1 I have this class:

Code:
class Game_Time
  attr_reader :var1
  attr_reader :var2
  # And so on...
end

In v0.2, I modified the class to add one more data member:

Code:
class Game_Time
  attr_reader :var1
  attr_reader :var2
  [b]attr_reader :var3[/b] # now here's the new data member
  # And so on...
end

When loading save files from v0.1, the game will always crash because of end of file (EOF) error. It also applies when I added more classes to newer versions.

Or, if it doesn't come to EOF error, the game will be eventually stop when it tries to access var3 that is unexpectedly nil.

Any idea to counter this? I planned to release third demo which will include more additions to the script along with more storyline.

Anyway, how does Marshal.load($game_time) work? How can my $game_time be restored with each data members holding their previous values? I wish I had that method in Flash's ActionScript...

Thank you.
 
You really can't do anything about this.

Save files hold all your $game objects, their instances, etc. in them. By adding more attributes, then loading your save file, the added attributes will not work. Same goes for adding new $game objects, or trying to load them from a save file that doesn't have them.


What it would require you to do is remember the exact order or saved everything to a save file, load it, modify everything for your added attributes, then reload it, all via scripting.

You only other solution is to do this:

Code:
class Game_System
  def your_added_attribute
    return @your_added_attribute.nil? : put_your_default_here : @your_added_attribute
  end
end

Now, that method is virtually the same as an attr_reader (or the reader part of attr_accessor), but if your instance isn't defined, you can set a default.

Say you have this by default:
Code:
class Game_System
  attr_accessor :map_bgm
  attr_accessor :battle_bgm
end
You save, then add this:
Code:
class Game_System
  attr_accessor :map_bgm
  attr_accessor :battle_bgm
  attr_accessor :menu_bgm
end

Instead, make what you added this:
Code:
class Game_System
  attr_accessor :map_bgm
  attr_accessor :battle_bgm
  attr_writer :menu_bgm
  def menu_bgm
    return @menu_bgm.nil? : put_your_default_here : @menu_bgm
  end
end


Now, this isn't 100%, because if other methods try and read @menu_bgm instead of self.menu_bgm, it will always have @menu_bgm as nil and not the default you set in your menu_bgm method.


I am trying to think of a way where people could open a savefile, inspect everything in it and modify it easily. I am thinking of something like a cross between explorer and cmd promt, but I just don't have time to do it.

Basically, all you can do is delete your savefiles and go from there.


The Marshal.dump and Marshal.load methods (I believe) take your objects from your games memory, convert them to some data, dump them into a file, and the data is then converted back when loaded. However, with Marshal.dump and load, they work in the same order you put them in.

If you dump $game_system, then $game_temp, loading it back, the first object would be $game_system and the second would be $game_temp. That's just how it works.
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top