Saving a Game to a txt file
Author |
Message |
Aange10
|
Posted: Fri Oct 14, 2011 5:37 pm Post subject: Saving a Game to a txt file |
|
|
What is it you are trying to achieve?
Okay so I've implemented a good amount of new stuff to my game. Now I'm working on a txt file that will hold the data (all the thing's i'll need to have when i make a save button)... I was wondering if you guys could nudge me in the direction, or preferably give me an example that I can work off of.
Describe what you have tried to solve this problem
^ Going to explain what I know:
I know how to creat/put/get etc. from a file. All of that is basic stuff covered in the Turing Walkthrough . I'm pretty sure to get more than one variable i'm going to need two tools: Indexing and String Manipulation. Mostly indexing. I've read around on the help section about saving, but the responses i've seen we're A: They had one variables (high scores) or B: They made more than one text file. Now I'm sure that 30 txt files would be stupid. How can I go about inserting the variables in a neat order, and more importantly: extracting the variables in the correct order?
Also should I manually write the data in the file, or should I have my podule export a procedure to check if the File.Exists then [grab data] else [make file, input default data]?
Thanks in advance!
P.S I'm very proud of this game, almost my first fully working, somewhat eye appealing game!
Please specify what version of Turing you are using
4.1 |
|
|
|
|
|
Sponsor Sponsor
|
|
|
mirhagk
|
Posted: Fri Oct 14, 2011 6:27 pm Post subject: RE:Saving a Game to a txt file |
|
|
Save games are always a tricky thing if your game isn't structured right. The easiest way to store data is to simply go through everything you need, and put it to the file, then do the reverse for reading it.
If you are using types or classes (classes would be best) you can just create 2 functions, one called Read, and one called Write, and all they do is read and write their values respectively to a file passed in as an argument.
If you have a more complex system, you may want to look at formal ways of storing data, xml tables, yml etc, but for your purposes, and since Turing does not support it, I'd suggest staying simple. |
|
|
|
|
|
Aange10
|
Posted: Fri Oct 14, 2011 8:16 pm Post subject: Re: Saving a Game to a txt file |
|
|
Ok so i have something that looks like this:
Turing: |
if File.Exists (player.name + extension ) then
else
open : stream,player.name + extension, put
put : stream, "nill"
get : stream, cheat_code
.
.
.
put : stream, "20"
get : stream, player.x
.
.
end if
|
What i'm doing if i'm putting in the default value of each piece of data, and then getting what I just put in. (The default value is hard coded) But it's giving me the error saying that it attempted to read past the end of the file. Seek and tell doesn't stop it, or at least i wasn't using it right. How do i make it stop? I can't get it all with a flexible array, because it gives me all one typeSpec. (because the array needs to be of typeSpec) Also on the http://compsci.ca/v3/viewtopic.php?t=12972&highlight=save tutorial, i have no idea why myText stops at the end of the line, therefore i can't use that to fix my problem. Should I just make a flexible array that gathers everything as a string, and then use strint etc. to get the values I need? ... But if I do that i'll have 20,000 statements saying
code: |
this_variable := strint(my_flexible_array(1))
other_variable := strint(my_flexible_array(2))
.
.
|
I'm sure there is a more efficient way of doing this...? At least, it'll be 100 lines of code if I have to do the above, though that would add a saving system. Please help |
|
|
|
|
|
RandomLetters
|
Posted: Fri Oct 14, 2011 8:32 pm Post subject: RE:Saving a Game to a txt file |
|
|
To my knowledge, there is no more efficient way. However, you shouldn't have THAT many variables. Most should be kept in arrays, which can be looped through.
arr_vars(i) := strint(arr_in(10+i))
You could also force the player to finish a level before saving, many games do this, which would probably let you get rid of almost all of the variables.
As for the end of file, I don't think you're allowed to use get from a "put" stream, but I haven't used turing in awhile. |
|
|
|
|
|
mirhagk
|
Posted: Fri Oct 14, 2011 9:20 pm Post subject: RE:Saving a Game to a txt file |
|
|
you need to close the file, and reopen it. |
|
|
|
|
|
Aange10
|
Posted: Fri Oct 14, 2011 11:08 pm Post subject: RE:Saving a Game to a txt file |
|
|
Thank you for the response, Mirhagk. Didn't know that was why. |
|
|
|
|
|
mirhagk
|
Posted: Sat Oct 15, 2011 11:35 am Post subject: RE:Saving a Game to a txt file |
|
|
Every time you put it moves the index forward, so when you read, your already at the end of the file. |
|
|
|
|
|
|
|