Qbasicnews.com

Full Version: Game Saving
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
I want to be able to save games...in my game. All i need are number variables to go in and out at one point. I've found only one site that helped. it showed how to save high scores, but I can't get it to work for me.

Any help would be great. Thanks. :bounce:
Well, the basic method of saving games is...
Write a copy of your map to a file, but without the treasure that you've already eaten, enemies you've killed, etc.
Thats what i doesn't know what to do. I mean I know how to do it...basically...but somethings missing to keep it from working
Actually. If you wanted to save a variable such as wpn%, which stands for the weapon the person is carrying, how would you go about saving it in a ".DAT" file?

http://www.geocities.com/SiliconValley/B...nipulation

This site helps, but since its no exactly what I need, I haven't figured out how to alter it. (ands its driving me crazy!).

Thanks again
Code:
f%=FREEFILE
OPEN "FILE.DAT" FOR BINARY AS #f%

Opens a file.

Code:
PUT #f%,,variable%

Writes variable%'s value in the file.

Code:
GET #f%,,variable%

Gets a value from the file and writes it to variable%.

Code:
CLOSE #f%

Closes the file. Those are the basics. When you learn how they work you'll be able to figure this out. Check QB help for syntax, examples and usage.
Actually, Nathans example is a little bit complicated for beginners.

You could try this also:

Open a file "save.dat" for write
Code:
OPEN "C:\save.dat" FOR OUTPUT AS #1

Write the variable a% into it (note: the number: #1 must correspond to the number of the file you have open
Code:
PRINT #1, a%

Close the file
Code:
CLOSE


Then, to read that saved file, the code is rather similar:

Open file "save.dat" for reading
Code:
OPEN "C:\save.dat" FOR INPUT AS #1

Read the data in the file into a variable:
Code:
INPUT #1, a%

Close the file
Code:
CLOSE

You can put as many variables into the file as you want, by repeating the PRINT #1 step. Just make sure that the file number you use is the same as the file number that you opened it with.

Hope this helps
Hey thanks.

I like to keep things simple...otherwise things my games come out funky. I'm more of the text adv. kinda guy.

Thanks again to ya'll. :lol:
Code:
'TO SAVE:
INPUT "enter name for file: ", name$
OPEN name$ FOR OUTPUT AS #1
WRITE #1, char$, hp%, totalhp%, your_vars!
CLOSE #1

'TO OPEN SAVE:
INPUT "file name: ", name$
OPEN name$ FOR INPUT AS #1
INPUT #1, char$, hp%, totalhp%, your_vars!
CLOSE #1
this is it all together. also what mode are you saving in?
Thanks Dark one! your way was easy and works as good as i need it to. i've played w/ so much code trying to get it to work, but to no avail. thanks again! :bounce:
Well, again, I'll put in more stupid two cents...
Say you have a game that runs off an array as a board (as most games do).
So you have an array...
Code:
DIM map(1 to 100,1 to 80) AS STRING*1
And it's filled with the positions of everything, including you, treasure, etc. What I would do is:
Code:
OPEN "savegame.dat" FOR OUTPUT AS #1
PRINT #1,STR$(yourScore%)
PRINT #1,STR$(yourHealth%)
FOR y=1 to 80
FOR x=1 to 100
IF x=100 THEN
PRINT #1,map(x,y)
ELSE
PRINT #1,map(x,y);  'end of the line
END IF
NEXT
NEXT
CLOSE #1
And then you just normally load your map, again.
Pages: 1 2