PDA

View Full Version : File Loading Question



catventure
05-12-2005, 22:32
Hi,

How do I successfully load a file previously saved like this:




'save function works...
function savegamedata() as long
tempval=file_open("test" ,"BINARY")
for i=1 to 500
file_put(tempval,verb(i))
next
file_close(tempval)
end function


'I tried this but didn't work

function loadgamedata() as long
tempval=file_open("test","INPUT")
for i=1 to 500
file_get(tempval,verb(i))
next
end function


I want to load the data back into array verb()


Cheers,
catventure.

RobertoBianchi
05-12-2005, 22:54
Hi Catventure,

file_get(tempval,verb(i))
sorry but you couldn't load data back in this way due the File_Get() behavior.
I checked the online function help but isn't clear and unfortunatly I cannot take a look to the source code now.
If I'm not wrong the File_Get() function should returns a string that holds a requested number of bytes.
I'll check it tomorrow.

Regards,
Roberto

Petr Schreiber
05-12-2005, 23:43
Hi Catventure,

no problem File_Get is not working, try this save/load demo:


Uses "File"

global Verb(3) as string

dim i as long

Verb(1) = "Hi"
Verb(2) = "Hi2"
Verb(3) = "Hi3"

msgbox 0, Verb(1)+$CRLF+Verb(2)+$CRLF+Verb(3), %MB_ICONINFORMATION, "Content of Verb() Before Saving"

SaveGameData("test.txt")

for i=1 to 3
Verb(i) = "" ' Lets delete data
next

msgbox 0, Verb(1)+$CRLF+Verb(2)+$CRLF+Verb(3), %MB_ICONINFORMATION, "Verb() Content Deleted"

LoadGameData("test.txt")

msgbox 0, Verb(1)+$CRLF+Verb(2)+$CRLF+Verb(3), %MB_ICONINFORMATION, "Verb() Reloaded From File"

function SaveGameData( gFile as string ) as long

local tempval as long

tempval=file_open( gFile ,"BINARY")
for i=1 to 3
file_put(tempval, Verb(i)+$CRLF) ' CRLF is importan here
next
file_close(tempval)

end function

' This function is based on code by Eros, posted some time ago
function LoadGameData( gFile as string ) as long

DIM sInData AS STRING VALUE File_Load( gFile )
DIM nLines AS LONG

'---Determine the number of lines
nLines = PARSECOUNT(sInData, $CRLF)

'---This function will parse the input string for $CRLF delimiters
' filling and dimensioning Verb array
PARSE sInData, Verb, $CRLF

end function


It should work :)

Bye,
Psch

catventure
06-12-2005, 00:13
Hi Psch,

That code looks like it will solve my question. :)
I had realised it could be done the way you have shown above.. it is just I thought there was a simpler way with the TB file commands.

But on reflection this looks a very good way indeed as I shall need to save other strings that contain CRLF's so can always use a different ascii code symbol for the delimiter instead of CRLF for those.

Thanks Psch and Roberto for your assistance.

Goodnight,
catventure

RobertoBianchi
06-12-2005, 09:32
Catventure,

the File_Get() function syntax is the following:
FILE_GET(FileHandler as long, nBytesToRead as long) as string
So you can also consider to use a fixed record lenght for store and retrieve game data to file.

Bye,
Roberto

ErosOlmi
06-12-2005, 12:08
Hi,

File_Get(Channel, nBytesToread), File_Put(Channel, StringToWrite) and File_Seek(Channel [, NewPosition]) are used to manually handling read/write cursor inside a file. Help file is not complete for this area but can give you an idea.

To save data you can create a string buffer from the array putting a CHR$(0) [null char] between every element and than using File_Save to save the string buffer in one go into a file.

To load, use File_Load to load a string buffer in one go and than parse the string buffer using CHR$(0) as delimiter. This will allow you to use CRLF inside your strings.

In any case, I will implement both Arrays adding some of the suggestion catventure/Psch gave us and also implementing some new file functionalities. I will put into the development list.

Thanks
Eros

RobertoBianchi
06-12-2005, 13:45
Hi,

I changed the Psch code for handle fixed record len.



Uses "File"

global Verb(3) as string

dim i as long
dim MAX_LINE_LEN as long

MAX_LINE_LEN = 10

Verb(1) = "Hi "
Verb(2) = "Hi2"
Verb(3) = "Hi3"

msgbox 0, Verb(1)+"!"+$CRLF+Verb(2)+"!"+$CRLF+Verb(3)+"!", %MB_ICONINFORMATION, "Content of Verb() Before Saving"

SaveGameData("test1.txt")

for i=1 to 3
Verb(i) = "" ' Lets delete data
next

msgbox 0, Verb(1)+"!"+$CRLF+Verb(2)+"!"+$CRLF+Verb(3)+"!", %MB_ICONINFORMATION, "Verb() Content Deleted"

LoadGameData("test1.txt")

msgbox 0, Verb(1)+"!"+$CRLF+Verb(2)+"!"+$CRLF+Verb(3)+"!", %MB_ICONINFORMATION, "Verb() Reloaded From File"

function SaveGameData( gFile as string ) as long

local tempval as long

tempval=file_open( gFile ,"BINARY")
for i=1 to 3
' you can embed $SPACE and $CRLF and CHR$(0) and CHR$(255) here but not terminate the data with CHR$(255)
' off course if you don't need to use some space at the end of vars (like Verb(1)) you can remove CHR$(255) in both LSET$ and RTRIM$
file_put(tempval, LEFT$(LSET$(Verb(i), MAX_LINE_LEN using CHR$(255)), MAX_LINE_LEN))
next
file_close(tempval)

end function

' This function is based on code by Eros, posted some time ago
function LoadGameData( gFile as string ) as long

local tempval as long

tempval=file_open( gFile ,"BINARY")
for i=1 to 3
Verb(i) = RTRIM$(file_get(tempval, MAX_LINE_LEN), CHR$(255))
next
file_close(tempval)


end function


Bye,
Roberto

catventure
06-12-2005, 16:59
Hi,

Roberto: Thanks for amending Psch's code to demo how to do the fixed record way. I shall study this a bit more.

Eros: I had just finished working out how to save all my game data in one go into a single file - - including with numeric arrays with more than one element (double dimensioned array) using the file_load into string method you describe...

I was using CRLF as delimiter for arrays with numbers and string arrays without carriage returns in them, and then I put all the string array vars and string vars that DID or COULD contain CRLF's to be saved last and used a delimiter of ALT+0216 ( ? ) for parse'ing after each of them....The save worked perfectly. I checked the resultant file and all was OK. Hooray.

After reading your message reply I see I can use a NULL terminator - so that will make it even easier to do the loading gamedata file function. I think I will alter it to use that. Great!

Anyway, no problems now with it. Thanks All for the explanations.

Best wishes,
catventure.

catventure
06-12-2005, 18:39
Hi,

Save/Load gamedata done succesfully, I'm pleased to report.

Now to make that really cool, I'd like to show a progress bar whilst the data is loading/saving (even though it is pretty fast)

Presume I've to use 'control add' and Pbar update?

Anybody tried doing that yet?


catventure.

catventure
07-12-2005, 10:57
Hi,

I have an idea for a simulated progress bar.
Here is example:



USES "UI"

dim hdlg,i as long


DIALOG NEW 0, "PBar Example", 0, 0, 130, 15, _
%WS_DLGFRAME OR %DS_CENTER or %WS_DISABLED or %WS_OVERLAPPEDWINDOW, 0 TO hDlg

CONTROL ADD TEXTBOX , hdlg, 10, "", 2, 0, 126, 14, _
%WS_BORDER ,%WS_EX_CLIENTEDGE

dim hFont as dword = Font_Create("Arial Black",12)
dim hEdit as dword
CONTROL HANDLE hDlg,10 TO hEdit
SendMessage hEdit, %WM_SETFONT, hFont, 0
control handle hdlg,10 to hEdit
SendMessage hEdit, %WM_SETFONT, hFont, 0

'---Set colours for input textbox - OK
CONTROL SET COLOR hDlg, 10, rgb(0,0,0), rgb(127,255,255)


DIALOG SHOW MODELESS hDlg

'file loading code here....

for i=1 to 24
sleep (100)
control append text hdlg,10,"?"
next

deleteobject hFont
DIALOG END hDlg


It needs a bit of improving, I think.... ;)

Regards,
catventure.

Petr Schreiber
07-12-2005, 19:26
Hi Catventure,

this progress bar looks pretty good
To improve it little bit you could replace textbox with label:



CONTROL ADD LABEL , hdlg, 10, "", 2, 0, 126, 14, _
%WS_BORDER or %SS_LEFT or %SS_CENTERIMAGE ,%WS_EX_CLIENTEDGE

... the dots will be vertically centered with this style too

Other option is to display label with horizontally centered "Loading(number %)", where the percents could be updated by every 5 or 10% to avoid flickering
What do you think?

Bye,
Psch

catventure
07-12-2005, 20:40
Yeah Psch,

Both options are viable and easy to do with the step increments at various stages inside file loading/saving code...

I like your improvement to centre the dots...

I rather think it would look better without the minimize, maximize and close buttons but can't think of the %style flag to do this...?
Do you know it, thanks.
I think might use something like this in preference to common controls PBar.

Regards,
catventure.

Petr Schreiber
07-12-2005, 21:28
Hi Catventure,

for your progress bar you can use this


DIALOG NEW 0, "PBar Example", 0, 0, 130, 15, _
%WS_POPUP OR %DS_MODALFRAME OR %DS_SETFOREGROUND or %DS_CENTER or %WS_CAPTION , 0 TO hDlg

to display just caption with name and progress bar

or even


DIALOG NEW 0, "PBar Example", 0, 0, 130, 15, _
%WS_POPUP OR %DS_MODALFRAME OR %DS_SETFOREGROUND or %DS_CENTER, 0 TO hDlg


To display caption-less progress bar.

Hope you like it.

Psch

catventure
07-12-2005, 23:34
Thankyou, Psch.

I've made a webpage where I'm going to post updates of my progress regarding the ThinBasic Adventure Builder project.


http://www.staining.fslife.co.uk/tbasic.htm

Here is the tbasic code for the Editor I'm working on.

Remember its only at Alpha stage and the code is not heavily commented or finally structured yet ;)

Cheers,
catventure

Petr Schreiber
08-12-2005, 16:28
Hi Catventure,

the latest T.A.B. Preview looks pretty good!

I like the way the interface is made. Only thing which
could be confusing a bit is the "Exit" button, "Back" could
describe it better. But that is only my personal feeling.

The other things are terrific, I like the possibility of immediate "test"
of adventure. Maybe the main window could be minimized,
when the test is in running, because if I try to click any button of "parent
dialog", it makes my computer hang :(.

I will watch the page with T.A.B. periodically !

Thanks,
Psch

P.S. The code seems to be quite good structured already :)

catventure
10-12-2005, 14:39
Hi Psch,

I have changed the buttons to read "back" as suggested to avoid any confusion.

As regard the dialog window problem, I think I would rather be able to HIDE the editor window and re-show it after the game window is closed..
Be good if there was a

DIALOG HIDE hwnd
DIALOG SHOW hwnd

sort of command in TB.

Is there a way of doing it using 'Sendmessage'?

Thanks.

catventure

Petr Schreiber
10-12-2005, 18:03
Hi Catventure,

No need for SendMessage !
There already exists


DIALOG SHOW STATE hDlg, %SW_HIDE
DIALOG SHOW STATE hDlg, %SW_RESTORE
DIALOG SHOW STATE hDlg, %SW_MINIMIZE
DIALOG SHOW STATE hDlg, %SW_MAXIMIZE


So try it. Just be careful :), placing it on some places in code can make thinbasic hang :(

Bye,
Psch

catventure
10-12-2005, 21:50
OK, Psch. Will try that. Only thought 'show state' existed for controls..
Thanks again.

Regards,
catventure.