Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / Please help, problems with saving.

Author
Message
Death Stryke
20
Years of Service
User Offline
Joined: 19th Jun 2004
Location:
Posted: 28th Jul 2004 10:08
Hi, I am creating a text based RPG but i don't know how to save or load games. I heard you need to type in "open to write" or something but I don't understand how to do it all. Thanks to any replies.

---Deathstryke666---
the_winch
21
Years of Service
User Offline
Joined: 1st Feb 2003
Location: Oxford, UK
Posted: 28th Jul 2004 10:42 Edited at: 28th Jul 2004 10:47
First of all you need to work out every piece of infomation you need to save in order to recerate the game in the state it is in when the save was made. How difficult that is will depend on how you organised the game.

Then you need to decide what order to write the data into the file and what data types you will need to use.

WRITE BYTE - (1 byte) whole number from 0-255
WRITE WORD - (2 bytes) whole number from 0-65535
WRITE LONG - (4 bytes) whole number
WRITE FLOAT - (4 bytes) float
WRITE STRING - string

Once you have done that you need to open the file for writing, write the data and then close the file. Files have a number which has to be between 1 and 32, the same way that images and objects have numbers.
db is odd in that it requires that the file does not exist before opening it for writing, most languages will either overwite a file by default or have an option.
To open the file you need to check if the file you want to open exists and if it does delete it. Like this.

if FILE EXIST("save.dat") then DELETE FILE "save.dat"
OPEN TO WRITE 1,"save.dat"

save.dat is now open for writing as file 1.

Now you need to write data to the file. Say you had an extreamly simple game where all you needed to save was the players position and score. The position of the player is in the variables playerx#, playery# and playerz# while the score is in the variable score.
The format of the save file will be
float playerx#
float playery#
float playerz#
long score

To write the data you would do this.
if FILE EXIST("save.dat") then DELETE FILE "save.dat"
OPEN TO WRITE 1,"save.dat"
WRITE FLOAT 1,playerx#
WRITE FLOAT 1,playery#
WRITE FLOAT 1,playerz#
WRITE LONG 1,score
CLOSE FILE 1

Now you have a file that contains all the info you need to recreate the game in the state t was when the save was made. The code to read the file is similar to the code to write it except you use the READ commands instead of the WRITE command. To load the file saved above the code would be

OPEN TO READ 1,"save.dat"
READ FLOAT 1,playerx#
READ FLOAT 1,playery#
READ FLOAT 1,playerz#
READ LONG 1,score
CLOSE FILE 1

You also might want to add some error checking to make sure the file you want to load exists (use FILE EXISTS()) and to make sure the file isn't too short with FILE END(). You also might want to sanity check the data you are reading to prevent cheating, if the maximum health you can get in the game is 100 then check the value loaded from the file isn't greater than 100.

can i scream

Login to post a reply

Server time is: 2024-11-27 02:22:56
Your offset time is: 2024-11-27 02:22:56