Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Need Some Calrification On Saving
Index -> Programming, Turing -> Turing Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Archi




PostPosted: Tue Jul 22, 2003 12:42 pm   Post subject: Need Some Calrification On Saving

I've tried to look at a few of the tutorials on how to save something to a txt file, but I'm not qutie getting it.
I'll be using the save feature to save the progress of the game so people can start where they left off. The only thing is, I dunno what the code would look like as the tutorials weren't too clear to me. Could someone maybe post the code which I could use to save and also a few examples of how to save the various pieces of info that will have to be saved?
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Tue Jul 22, 2003 12:49 pm   Post subject: (No subject)

Well what you do is that you write the values of all the variables to a textfile and then to load you read the values in the same order.

By saving/loading variables such as X/Y position, inventory, status, etc you can "load" game to be to the same state as when the variables were saved.

Perhaps someone like Mazer could show you an example of how to save using read/write and encription.

code:

%save variables
put : fileStream, x
put : fileStream, y
put : fileStream, value

%load variables
open : fileStream, x
open : fileStream, y
open : fileStream, value
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Mazer




PostPosted: Tue Jul 22, 2003 2:26 pm   Post subject: (No subject)

tony... you ok?

code:

%save variables
put : fileStream, x
put : fileStream, y
put : fileStream, value

%load variables
%how about....
get : fileStream, x
get : fileStream, y
get : fileStream, value


in case you're wondering, fileStream is an integer. before you go and save/load stuff you use the variable to open the text file with this line
code:

open : fileStream, "whatever.txt", get

that'll open the file for getting data. instead of the ", get" you could have ", put" and that would allow you to write to the file. but keep in mind as soon as you do that it will clear everything inside the file.
Mazer




PostPosted: Tue Jul 22, 2003 2:35 pm   Post subject: (No subject)

tony wrote:

Perhaps someone like Mazer could show you an example of how to save using read/write and encription.


encryption... if you're referring to the encryption in the accounts for my game, i have to tell you it was really lazy Embarassed

just a simple encryption function
code:

function _cryption (text : string) : string % en/de cryptor
    var _crypted := ""
    for i : 1 .. length (text)
        _crypted += chr (255 - ord (text (i)))
    end for
    result _crypted
end _cryption


as for the strange and unreadable account files, it's just that i used binary file handling to read and write the data which was useful because that way people can't edit the file themselves.
Archi




PostPosted: Tue Jul 22, 2003 2:47 pm   Post subject: (No subject)

But in my case where I don't want to allow the player to edit the information i could use the write and read commands?
Mazer




PostPosted: Tue Jul 22, 2003 2:58 pm   Post subject: (No subject)

if you don't want the user to edit the information then yes, i would suggest considering binary file handling (read and write as opposed to get and put). binary file handling is used for storing data that's in a record more easily but i like it best because of the fact that if someone tries to f*** around with the file it gets screwed up.

anyways, have you're players information set up in a record like so:

code:

type playerrecord:
    record
        x, y : int
        name : string(40)
    end record

var player : playerrecord
var fs : int

player.x := 5
player.y := 10
player.name := "Sirindor"


that's just an example, you're probably storing more stuff so you'll have more variables.

now when you want to store everything in the file:
code:

open : fs, "playerstats.txt", write

write : fs, player
close : fs


note that you only have to write the player variable. that's really good if you have lots of variables to save and don't want to type out a put line for each one.

and to read it's pretty much similar:
code:

open : fs, "playerstats.txt", read

read : fs, player

put player.x, ", ", player.y, ", ", player.name
Archi




PostPosted: Tue Jul 22, 2003 3:43 pm   Post subject: (No subject)

Well I got that ot work. Thanks for the help. Now comes another question. Is there any possible way to put the entire contents of a file onto the screen? Like I have a starting story that is rather long. If i write it into a *.txt file, then is there anyway that i could use the read/write, get/put combinations to take the contents of that file and place it directly on the screen? Also in doing so is there any way to have it read so many lines, then wait for payer input before placing the next so many lines?

Also, how can i make the name of the file which the stats are saved to, dependant on the name of the player? So to allow multiple people to play on the same computer?
Mazer




PostPosted: Wed Jul 23, 2003 11:54 am   Post subject: (No subject)

for showing a starting story to a game, you'll want to use normal file handling as opposed to binary. maybe show 6 or so lines of the story at a time then wait for.

code:

colourback (7)
cls
View.Set ("offscreenonly")

var fs : int
var keybuffer : string (1)
var lines : array 1 .. 6 of string
var font := Font.New ("Garamond:16:bold")

open : fs, "story.txt", get

loop
    for i : 1 .. 6
        exit when eof (fs)
        get : fs, lines (i)
        for decreasing j : 31 .. 16
            Font.Draw (lines (i), maxx div 2 - Font.Width (lines (i)) div 2, maxy div 6 * i, font, j)
            delay (20)
            %EDIT: i used offscreenonly, but i forgot about View.Update...
            View.Update
        end for
    end for
    for i : 1 .. 6
        lines (i) := ""
    end for

    loop
        exit when hasch
    end loop
    getch (keybuffer)
    cls
end loop

that should do it Wink

as for the player's name and stuff, just save/load the file using the player's name. like:
code:

type playerrecord:
    record
        x, y : int
        name : string(40)
    end record

var player : playerrecord
var fs : int

get player.name:*

open : fs, player.name + ".txt", read
read : fs, player

that should load the player's info
Sponsor
Sponsor
Sponsor
sponsor
Archi




PostPosted: Wed Jul 23, 2003 10:03 pm   Post subject: (No subject)

How do I make it so that it puts more than one word on a line?
PaddyLong




PostPosted: Thu Jul 24, 2003 12:30 am   Post subject: (No subject)

same way you do to output to the run window, just put a .. after it
Archi




PostPosted: Thu Jul 24, 2003 12:45 am   Post subject: (No subject)

i'm not sure i get what you mean...
Well rather..i dunno where ot put the ".."
PaddyLong




PostPosted: Thu Jul 24, 2003 10:54 am   Post subject: (No subject)

well you know how when you're outputting to the run window and want two put statements to be on the same line...

you can do it as

put var1, var2
or
put var1 ..
put var2
Archi




PostPosted: Thu Jul 24, 2003 11:15 am   Post subject: (No subject)

That I understand. Its where to put it in the loop that mazer gave me...
Archi




PostPosted: Thu Jul 24, 2003 2:00 pm   Post subject: (No subject)

I don't know where I'm to put the ".." in the part of the code Mazer gave me....
PaddyLong




PostPosted: Fri Jul 25, 2003 2:16 pm   Post subject: (No subject)

o, it's because you don't need to... in mazer's he's using read/write rather than get/put. read/write makes a binary file that isn't really readable with a regular text editor (you can read it, but it will look a little wrong). so with read/write you don't need to worry about putting it on the same line or whatever
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 19 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: