First off, I wanna say, I'm not a conventional student, posting homework problems. I am a student, in the fact that I am learning BASIC/Qbasic. But I'm not going to school to learn it, just kinda feeling my way around learning. If that makes sense?

The main reason I am learning it is because I am trying to make a text-based adventure game for my father. Some of you may have played his games in the past, he use to make text-based games, way back when. Which of course is why I'm making him a text-based game for his birthday.

That being said, I have just about got the game completed, except for, what I see as, a crucial part. I'm not sure how to program it so that the user can save the game, and come back to it later? I've looked around online and found nothing that seems to answer my question. And unfortunately I've procrastinated on finding this information out till now, so I can't get a book at the library on it... His birthday is tomorrow (well today, technically), and I'm hoping to have this complete by sunday. So if anyone has any help for me I'd *greatly* appreciate it!

Thanks in advance!,
MoNkEe

Dani AI

Generated

Short expert note: saving a text-adventure in QBasic is just persisting the small set of game-state variables (player location, items, score, flags, etc.). As said, you write those values to a file; as pointed out, read them back in the same order (or use a tagged format). QBasic gives you two practical approaches: simple sequential text files (easy to debug) or RANDOM/BINARY records (fixed-layout, faster, but more work). (qbasic.net)

A practical, beginner-friendly pattern: write a tiny header (signature + version) and then one value per line. Use FREEFILE to get a safe file number, PRINT # to write and LINE INPUT # / VAL to read numbers back. That keeps parsing trivial and you can open the saved file in a text editor when something goes wrong. Example (condensed):

' save.bas
f% = FREEFILE
OPEN "MYGAME.SAV" FOR OUTPUT AS #f%
PRINT #f%, "MYGv1"         ' signature/version
PRINT #f%, score
PRINT #f%, lives
PRINT #f%, weapon$
PRINT #f%, posX
CLOSE #f%

' load.bas
f% = FREEFILE
OPEN "MYGAME.SAV" FOR INPUT AS #f%
LINE INPUT #f%, hdr$
IF hdr$ <> "MYGv1" THEN 'handle mismatch
LINE INPUT #f%, s$: score = VAL(s$)
LINE INPUT #f%, s$: lives = VAL(s$)
LINE INPUT #f%, weapon$
LINE INPUT #f%, s$: posX = VAL(s$)
CLOSE #f%

Use FREEFILE, LINE INPUT and VAL as shown for safety and clear conversions. (qbasic.com)

If you need binary or random access (save many records, or save large arrays), define a TYPE for a record and use PUT/GET with an OPEN ... FOR RANDOM AS ... LEN = LEN(record) or BINARY with GET/PUT; FIELD is useful for fixed string fields. Those modes require fixed record sizes and careful use of LEN/TYPE/GET/PUT. Add a version byte at the start so your loader can detect incompatible saves. (qb64.com)

Troubleshooting tips: always check for file existence and EOF/LOF when reading to avoid "input past end of file" errors; validate the header/version before trusting contents; catch I/O errors and fall back to a safe default if a save is corrupt or missing. Those checks make save/load robust across edits and program updates. (learn.microsoft.com)

Thanks to and for the pointers — start with the text approach above and move to RANDOM/BINARY later only if you need the extra efficiency.

Recommended Answers

All 4 Replies

Whoops, forgot to mention I am using QBASIC to do this ... not Visual Basic :) (not sure if that makes a difference?) ... Thanks again for any input!! And for helping to point me in the right direction! :)

Saving games is a matter of saving all of your variables that pertain to items, player status, location, events, etc to a file. This can easily be done with the OPEN statement to ope a file, and CLOSE to close it.

When I taught myself QBasic, I found that the best place to find answers was from the QB help. Look up the OPEN ... AS ... FOR BINARY entry. That's probably what you want. Be sure when you load a game that you read the data in the same order that you wrote it.

just wanted to say thank you for suggesting I check out Qbasic's help!! ... WoW!! I've learned so much more that way in just a few hours, then I had searching the net for tutorials and such... Thank you so much! ....

... and now back to your regularly scheduled answers ;)

I am curious what you meant though by being sure I read the data in the same order I wrote it?? Thanks ahead of time for the info! :)

- MoNkEe

Let's say you save your data (example data here) in this order:

Score
Number of Lives
Current Weapon
Screen location
Level

You then want to read in your data in the same order, so that you don't get 1,000,389 for the number of lives and not the score.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.