1

I am making a small text based game, and I want to save the game and load the data at a later date. I managed to have the game create a text file that stores all of the variables and their values in a format like this:

first_name = "Name"
last_name = "Last Name"
bank = 10000
...

When I select load, I want it to read the text and assign the appropriate variables. Can I do this?

sawa
  • 165,429
  • 45
  • 277
  • 381
KadoDragon
  • 31
  • 4

4 Answers4

1

One option: you could use IO.readlines, which would return an array of strings that you could parse. If you had a set of variables already in mind (and used by the code), you could just grab what's in quotes, for example. Take a look at what ruby can do with strings.

  • Note the change in behavior though from one version of Ruby (older version) to the current, using eval and friends in text to get local variables. http://stackoverflow.com/q/19321698/485864 – vgoff Oct 20 '13 at 02:26
1

You cannot do that with local variables unless you use meta-programming techniques like evaluating or parsing the file, as proposed in other answers. If you want to do it without meta-programming hack, you need to use other types of variables or constants. Preparing a module (which is a special type of constant) for setting may help.

In the main file:

module Setting
  def self.set h; @h = h end
  def self.call @h end
end
load(path_to_setting_file)

In the setting file:

Setting.set(
  first_name: "Name",
  last_name: "Last Name",
  bank: 10000,
)

To call it from the main file:

Setting.call
# => {
  first_name: "Name",
  last_name: "Last Name",
  bank: 10000,
}

If you don't even like that, then you should use YAML.

sawa
  • 165,429
  • 45
  • 277
  • 381
1

Fastest and easiest way is to use Marshal.

# Save data
data = Marshal.dump(game_state)

# Load data
game_state = Marshal.load(data)

If you expect your game data schema to keep changing, you might want to consider storing your data in a hash or a Hashie::Mash instead of using instance variables.

davogones
  • 7,321
  • 31
  • 36
  • Its fast alright. However, in the moment that you make changes to the class that game_state is an instance of, there is a good probability that old Marshal dumps won't load anymore. I would therefore suggest using some JSON or XML serialization. – moritz Oct 21 '13 at 09:08
  • Either way, you still have to make your code tolerant of backwards-incompatible changes. Inheriting from `Hashie::Mash` should work fine even if the class structure changes. Possibly you would deserialize into an intermediary representation (hash?) and then use that to populate the actual game state. It all depends how complex the requirements are, which isn't very clear from the question. – davogones Oct 21 '13 at 19:50
0

Roughly:

    variable_file = File.open("./myfile.txt")
    variable_file.each do |variable_line|
    variable_line.chomp!
    #split on " = "
    #gsub out the quotes
    #add to hash where name is key and value is value
    end
    #your program uses the hash to access the values
Jim Evans
  • 31
  • 3