Text Saving and Loading Help?

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
User avatar
Mentat
Veteran
Posts: 409
Joined: Tue Aug 07, 2007 3:39 pm
Location: NC, US

Text Saving and Loading Help?

Post by Mentat »

1) How do I load data from notepad (.DAT) into a QB array?
B) How do I save in the best format to .DAT?
3) How do I save and load dialogue (RPG)?
4) Just general saving and loading techniques in QB. I'm not using binary saving and loading. I haven't much experience with it.
For any grievances posted above, I blame whoever is in charge . . .
Patz QuickBASIC Creations
Veteran
Posts: 399
Joined: Wed Mar 02, 2005 9:01 pm
Location: Nashville, Tennessee
Contact:

Re: Text Saving and Loading Help?

Post by Patz QuickBASIC Creations »

Not going in order, but it makes more sense this way.
Mentat wrote:2) How do I save in the best format to .DAT?
This matters on your application. If you are just doing an RPG, the best way might be to just to save strings containing your information, but it matters on what your needs are for this program.
Mentat wrote:3) How do I save and load dialogue (RPG)?
Yet again, it matters on your program.
Mentat wrote:1) How do I load data from notepad (.DAT) into a QB array?
Lets pretend that all your save data needs to be:

Code: Select all

GameData$(1) - Player's Name
GameData$(2) - Amount of gold
GameData$(3) - Experience
GameData$(4) - Location
etc...
Now, using this kind of format does require a little work, like using VAL on the values that you'll need to use as numbers, but it's one of the easiest to implement. Here's a quick save/load code I threw together. All you need to supply is a file name and array to store the save data to. Commented for your editing pleasure :) Will probably require editing to fix certain kinks, but functional nonetheless.

Code: Select all

SUB GameSave (FileName$, ArrayToSave$())
'FileName$ - File to save data to, i.e. "C:\SAVEDATA.DAT"
'ArrayToSave$() - Array containing information you wish to save.
'
' Example:  GameSave "C:\Game\Saves\RPG.GAM", PlayerInfo$()

FileHandle% = FREEFILE   'Make sure we have a new file number to open.
OPEN FileName$ FOR OUTPUT ACCESS WRITE AS #FileHandle%   'The actual command to open the file
FOR Dat% = 1 to 5   'Change 5 to number of elements in your array as needed
   PRINT #FileHandle, ArrayToSave$(Dat%)   'Write the array element to file.
NEXT Dat%
CLOSE #FileHandle   'Close our file.
END SUB
The load code looks similar :)

Code: Select all

SUB GameLoad (FileName$, ArrayToLoad$())
'FileName$ - File to load data from, i.e. "C:\SAVEDATA.DAT"
'ArrayToLoad$() - Array containing information you wish to load.
'
' Example:  GameLoad "C:\Game\Saves\RPG.GAM", PlayerInfo$()

FileHandle% = FREEFILE   'Make sure we have a new file number to open.
OPEN FileName$ FOR INPUT ACCESS READ AS #FileHandle%   'The actual command to open the file
FOR Dat% = 1 to 5   'Change 5 to number of elements in your array as needed
   LINE INPUT #FileHandle, ArrayToSave$(Dat%)   'Read the array element from the file.
NEXT Dat%
CLOSE #FileHandle   'Close our file.
END SUB
Look up the LINE INPUT and PRINT commands in help if you need more info :)
Last edited by Patz QuickBASIC Creations on Tue Sep 11, 2007 8:51 am, edited 1 time in total.
Patz QuickBASIC Creations
Veteran
Posts: 399
Joined: Wed Mar 02, 2005 9:01 pm
Location: Nashville, Tennessee
Contact:

Re: Text Saving and Loading Help?

Post by Patz QuickBASIC Creations »

(removed, double post)
Last edited by Patz QuickBASIC Creations on Tue Sep 11, 2007 8:25 am, edited 1 time in total.
User avatar
Mentat
Veteran
Posts: 409
Joined: Tue Aug 07, 2007 3:39 pm
Location: NC, US

Post by Mentat »

Thanks :D . I'm trying to save and load the vertices of cubes (3d) and lots of them. And I want to prewrite the data, as in writing directly to the .DAT before finishing the game, such as initial coordinates.

For instance, do I write in notepad:

Code: Select all

InitialVertice.DAT 

 x1   y1  z1    x2  y2  z2 ...... x6    y6  z6
...
150,200,37,   ,134,321,21, ... ,350,432,67  john's forearm, cube 2
140,300,24,   ,149,221,34, ....,300,400,50  john's wrist, cube 1
...
As you can see, this will be a TON of prewritten data, and I have to have copies for saving and other stuff. Not to mention dialogue, events, and how the cubes will be acted upon.
I want the vertice data more or less like just putting stuff in the DATA/READ comand.
And I wouldn't mind any help on how I'm going to organize this mass of data, and the diferent save files (one for vertices, dialougue, etc).
But once I finish the engines it'll be plug and chug.
For any grievances posted above, I blame whoever is in charge . . .
Patz QuickBASIC Creations
Veteran
Posts: 399
Joined: Wed Mar 02, 2005 9:01 pm
Location: Nashville, Tennessee
Contact:

Post by Patz QuickBASIC Creations »

Well, in that case it might be better to use the WRITE and INPUT # commands. For instance, to load/save your first set of the 6 coordinates would look something like this:

Code: Select all

'Saving code
FileHandle = FREEFILE
OPEN "YourFile.gam" FOR OUTPUT ACCESS WRITE AS #FileHandle
FOR A = 1 TO 6
   WRITE #FileHandle, x(A), y(A), z(A)
NEXT A
CLOSE #FileHandle
Your saved data will then look like this:

Code: Select all

1,2,3
1,2,3
4,8,3
6,5,2
4,3,2
1,8,5
where each number is a pseudo-coordinate :) This will make it easy for QB to load the data with a simple INPUT # command.

Code: Select all

'Loading code
FileHandle = FREEFILE
OPEN "YourFile.gam" FOR INPUT ACCESS READ AS #FileHandle
FOR A = 1 TO 6
   INPUT #FileHandle, x(A), y(A), z(A)
NEXT A
CLOSE #FileHandle
Look familiar?

After you are done loading your first coordinates, you can expand on the code with as many WRITE/INPUT # commands as you need.
Mac
Veteran
Posts: 151
Joined: Mon Aug 06, 2007 2:00 pm

Post by Mac »

Mentat wrote:I'm trying to save and load the vertices of cubes (3d) and lots of them.
You might want to study this program
http://www.network54.com/Forum/178387/m ... 1178554335

It is Pongg by Bob Seguin.

It shows how to build an array in one phase and save the array as one IO command (BSAVE). Then, in another phase when the array is to be loaded, BLOAD loads it in one IO command. Dramatically better than any scheme that writes out array entries one-at-a-time, such as PRINT or WRITE. But complicated as heck. I would never use it for string arrays or arrays with less that 1000 entries. Anyway, it may be worth your time.

Mac
User avatar
Mentat
Veteran
Posts: 409
Joined: Tue Aug 07, 2007 3:39 pm
Location: NC, US

Post by Mentat »

But aren't genuine loading screens clasic? 8)

And I want to write directly to the data itself via notepad. Or have another program help.

And the time I used BLoad, well, I had something look like a cross between blood running down a wall and a red barcode. And it was supposed to be only a blue point.
For any grievances posted above, I blame whoever is in charge . . .
Mac
Veteran
Posts: 151
Joined: Mon Aug 06, 2007 2:00 pm

classic?

Post by Mac »

Mentat wrote:But aren't genuine loading screens clasic?
Sorry, I don't follow. (Bit slow at times) I don't know what a "loading screen" is, nor what "classic" implies in this context.

If you are asking if there is a standard way to put data on the screen, the answer is "no". Each programmer dreams up a different way. TheBob's is the best I know of, but there may be many that are better.

You can read data as explained already in this thread. And you could either enter the data via notepad or have a program generate it, if you know a function. That is common knowledge, so I am having a problem determining what your real need is.

Sorry,


Mac
User avatar
Mentat
Veteran
Posts: 409
Joined: Tue Aug 07, 2007 3:39 pm
Location: NC, US

Post by Mentat »

Loading data, text, and events to the program. Like couple of rooms would be stored to the QB program, while all else is in notepad.
And when the person leaves, some data is saved, and everything else is loaded.
For any grievances posted above, I blame whoever is in charge . . .
Mac
Veteran
Posts: 151
Joined: Mon Aug 06, 2007 2:00 pm

Post by Mac »

Mentat wrote:Loading data, text, and events to the program. Like couple of rooms would be stored to the QB program, while all else is in notepad.
And when the person leaves, some data is saved, and everything else is loaded.
Well, Mentat, I decided to write a game just for you:
http://www.network54.com/Forum/190883/m ... 1189719394

Study that program. It reads and writes text files.

It will automatically generate the initial files, but you can edit them when the program is not running. For example
NOTEPAD S2.sve

That will allow you to modify room 2 data.

Mac
User avatar
Mentat
Veteran
Posts: 409
Joined: Tue Aug 07, 2007 3:39 pm
Location: NC, US

Post by Mentat »

Awesome. :D
Got stuck in the room with all the colorful pound signs. :oops:
For any grievances posted above, I blame whoever is in charge . . .
Mac
Veteran
Posts: 151
Joined: Mon Aug 06, 2007 2:00 pm

Post by Mac »

Mentat wrote:Got stuck in the room with all the colorful pound signs.
If you are in a room and the doors are closed, you have to get rid of all the pound-signs that are the same color as you are. Once you do that, you can go to another room.

If you enter a room with no pound-signs that are your color, then you have to generate one by running into a wall. Eat the resultant # and the doors will open.

But the point is that if you his ESC to stop the program, you can look at those SAV files and see that this program indeed loads text files into arrays, which was your original question. So by studying the code, you can see how that happens.

Hope it helps.

Mac
User avatar
Mentat
Veteran
Posts: 409
Joined: Tue Aug 07, 2007 3:39 pm
Location: NC, US

Post by Mentat »

It's awesome. :D
I was first annoyed with the slipping thing, but then I saw the point of it.
Not only that, but it's FB compatible. 8)

I read some of the code, so I'm guessing that:

Code: Select all

MYPROGRAM.BAS
...
CLS
PRINT LOADING
OPEN DATAV.DAT FOR INPUT AS #1
FOR R = 1 TO 600    'NUMBER OF POINTS
     FOR C = 1 TO 24   'NUMBER OF COORDINATES FOR CUBE
          INPUT VERT(R,C), #1
     NEXT C
NEXT R
CLOSE #1
...
will load the array from:

Code: Select all

some data in .DAT
DATAV.DAT

1,4,1,3,5 .... 5
3,4,7,3,6 .... 6
where there are 24 numbers in a row and there are 600 numbers in a column.

Pretty much, if you don't understand what I'm saying, is that: does QB read from .DAT like it reads from it's own DATA commands?

If it does, then it will make my life A LOT easier.
For any grievances posted above, I blame whoever is in charge . . .
User avatar
Mentat
Veteran
Posts: 409
Joined: Tue Aug 07, 2007 3:39 pm
Location: NC, US

Post by Mentat »

GOT MY TEST CODE TO WORK IN FB!!! :D

I'm back on track!!!

Code: Select all

OPEN "READA.DAT" FOR INPUT AS #1     'my test data file
DIM ARRAY(5,5)                                    'array I'm using

CLS         
FOR R = 1 TO 5
        FOR C = 1 TO 5
            INPUT #1,ARRAY(R,C)
            LOCATE R,3*C          'have to space by 3s otherwise it won't show
            PRINT ARRAY(R,C)
        NEXT C
NEXT R

DO
    LET KEY$=INKEY$
LOOP WHILE KEY$=""
Here's my file:

Code: Select all

01,02,03,04,05
06,07,08,09,10
11,12,13,14,15
16,17,18,19,20
21,22,23,24,25
I think it'll work in QB too, but I have to manually create the file, while FB automatically creates it.

I didn't know it was so easy. It's basically like the DATA statements.
COOL.
For any grievances posted above, I blame whoever is in charge . . .
Mac
Veteran
Posts: 151
Joined: Mon Aug 06, 2007 2:00 pm

Good Man

Post by Mac »

Glad you are back on track.

Mac
Post Reply