On Nov 28, 5:44=A0pm, Richard Mccormack <br... / livingwiththehorde.com>
wrote:
> Hey everyone,
> I've been working on some sort of a test-based game to learn ruby (and
> programming in general :) and I got stuck with my saving/loading system.
> To show you what I mean, this is what I used to save the game. (I want
> the code to be as modular as I can, I have some sort of an addon system
> planned...)
>
> def savegame(name, varstosave)
> =A0 savegame =3D File.new(name + ".tsl", "w")
> =A0 =A0 =A0savegame.puts varstosave.to_s
> =A0 =A0 =A0while varstosave.length !=3D 0
> =A0 =A0 =A0 =A0 savegame.puts varstosave[0]
> =A0 =A0 =A0 =A0 savegame.puts eval(varstosave[0]).to_s
> =A0 =A0 =A0 =A0 varstosave.delete_at(0)
> =A0 =A0 =A0end
> =A0 savegame.close
> end
>
> So, now I am trying to to load the variables from the file. I came up
> with the start of a function:
>
> def loadgame(name, varstoload)
> =A0 puts "Loading..."
> =A0 loadgame =3D File.open(name + ".tsl", "r")
> =A0 =A0 while varstoload.length !=3D 0
> =A0 =A0 =A0 =A0 # ?
> =A0 =A0 end
> =A0 loadgame.close
> end
>
> Where varstoload/varstosave is an array. Problem with the loading is
> that I assumed there =A0was a way to take the first variable name from th=
e
> array and actually use it as a variable. So, if the array is
> ["$intellect", "$strength"], I thought I would be able somehow to turn
> a[0] into a variable object. It seems that there isn't a way to do this,
> so I was wondering if there was a better way to manage the
> saving/loading.
> =A0- Thanks in advance! :)
>
> --
> Posted viahttp://www.ruby-forum.com/.

Not sure if it helps, but here are a few tips.

First, if you simply want to dump all the data in a file and get it
back have a look at things like the YAML library: you can take your
objects, and just with one line of code dump it into a file. (you can
alternatively use Marshal but then the file will be written in binary
format: great if you need to save space but not if you want to be able
to open the file with a text editor to have a look).

Second, and possibly more relevant. :)
A common way to do what you are trying to do would be to read a line
at a time from the file, analyse it and store the result. Here a
couple of useful functions may be:

File::read (it reads the whole file and returns a String object)
File::readlines (it reads the whole file and returns an Array of
String with one line of the file per element of the Array)

Note that in both cases these are class methods, similar to the
File.open you used.

A maybe more elegant method, and more similar to what you were trying
to do would be to use File#gets, which returns the "next" line. Then
you could modify your code so:

loadgame =3D File.open(name + ".tsl", "r")
while !loadgame.eof?   # i.e. until it reaches the end of file
  line =3D loadgame.gets
  # more code
end
loadgame.close

Now, assuming you manage to get a line, you need to parse it to get
the data you want. The most generic way would be to use a regexp, but
for simple cases you can simply split the line into smaller Strings
via the function String#split. This would make it easier to pick the
name and the value.

So far I've assumed that you want to load all the variables in the
file. If you only want to load some, i.e. only those in the Array
varstoload you can use Array#include, for example:


loadgame =3D File.open(name + ".tsl", "r")
while !loadgame.eof?   # i.e. until it reaches the end of file
  line =3D loadgame.gets
  elements =3D line.split
  if varstoload.include? elements[0]
    # add elements[0] and elements[1] to some variable/object
  end
end
loadgame.close

Hope that help.

Feel free if you have other questions.
Diego