R
Richard Mccormack
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)
savegame = File.new(name + ".tsl", "w")
savegame.puts varstosave.to_s
while varstosave.length != 0
savegame.puts varstosave[0]
savegame.puts eval(varstosave[0]).to_s
varstosave.delete_at(0)
end
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)
puts "Loading..."
loadgame = File.open(name + ".tsl", "r")
while varstoload.length != 0
# ?
end
loadgame.close
end
Where varstoload/varstosave is an array. Problem with the loading is
that I assumed there was a way to take the first variable name from the
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.
- Thanks in advance!
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)
savegame = File.new(name + ".tsl", "w")
savegame.puts varstosave.to_s
while varstosave.length != 0
savegame.puts varstosave[0]
savegame.puts eval(varstosave[0]).to_s
varstosave.delete_at(0)
end
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)
puts "Loading..."
loadgame = File.open(name + ".tsl", "r")
while varstoload.length != 0
# ?
end
loadgame.close
end
Where varstoload/varstosave is an array. Problem with the loading is
that I assumed there was a way to take the first variable name from the
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.
- Thanks in advance!