Code archives/File Utilities/Saving/Loading Types
This code has been declared by its author to be Public Domain code.
Download source code
| |||||
This was created in response to a request about saving and reloading a game. Someone demonstrated a custom save routine + corresponding loader. To me this seemed like too much work, when the Str$() function will format all fields in a type for printing, thus saving half the work. The challenge then becomes the writing of a parser for the text values of your type. The example below shows the reading of a couple of ints and a string. There is a code snippet (add's Parser Function) elsewhere in the Archives that could help with parsing other values such as floats... http://www.blitzbasic.com/codearcs/codearcs.php?code=161 | |||||
[code] ;------------------------------------------------------------------ ;---- demo of saving and reloading game data using Str$() ---- ;---- Blackjumper - Jan 2005 ---- ;------------------------------------------------------------------ Type test Field x Field y Field name$ End Type For count = 1 To 4 n.test = New test n\x = Rand(10) n\y = Rand(10)+100 n\name$ = Chr(Rand(26)+65) +Chr(Rand(26)+65) +Chr(Rand(26)+65) Next fileout = WriteFile("C:\storedgame.txt") For n.test = Each test Print Str$(n) WriteString (fileout, Str$(n)) Next CloseFile( fileout ) Print "game data written to file... press any key to continue" Print WaitKey Print "deleting all instances of type 'test'..." For n.test = Each test Delete n Next Print "printing all type information... Print "________________________________" For n = Each test Print Str$(n) Next Print "--------------------------------" WaitKey Print Print "... now reading from disk..." filein = ReadFile("C:\storedgame.txt") While Not Eof(filein) Read1$ = ReadString$( filein ) RestoreTestInfo(Read1$) Wend Print "printing all reloaded type information... Print "------------------------" For n.test = Each test Print Str$(n) Next Print "------------------------" WaitKey End Function RestoreTestInfo( SavedString$ ) Print "Read from file --> " + SavedString$ SavedString$ = Mid$( SavedString$, 2, Len(SavedString$)-2) ; remove end square brackets firstcomma = Instr(SavedString$, ",") firstvalue% = Left$(SavedString$, firstcomma-1) ; convert first value (up to comma) to an int SavedString$ = Mid$( SavedString$, firstcomma+1, Len(SavedString$)-firstcomma+1) ; eat up to 1st comma firstcomma = Instr(SavedString$, ",") secondvalue% = Left$(SavedString$, firstcomma-1) ; convert up to new first comma to another int SavedString$ = Mid$( SavedString$, firstcomma+1, Len(SavedString$)-firstcomma+1) ; eat up to new 1st comma ThirdString$ = Mid$( SavedString$, 2, Len(SavedString$)-2) ; remove quotes from string reloaded.test = New test ; make a new type reloaded\x = firstvalue% ; and assign the reloaded\y = secondvalue% ; reloaded values reloaded\name$ = ThirdString$ ; to the fields End Function [/code] |
Comments
| ||
I found this very useful for ripping out the code to get 3 bits of data from one line in the function by reading up to commas and reworking the string. Thanks for that. |
| ||
Someone (Sefery) was asking for an example of how to load and save object position by using the F6 and F7 keys. http://www.blitzbasic.com/Community/posts.php?topic=59739 I modified their code as follows to achieve this... AppTitle "My little box" Graphics3D 800,600,32,2 SetBuffer BackBuffer() Global message$ = "move the boxes... '[' and ']' to change box" Type test Field x Field y Field z Field name$ Field shape End Type For count = 1 To 4 n.test = New test n\x = Rand(10) n\y = Rand(10) n\z = Rand(10)+10 n\name$ = Chr(Rand(26)+65) +Chr(Rand(26)+65) +Chr(Rand(26)+65) n\shape = CreateCube() EntityColor n\shape, count*50, 0,0 Next camera=CreateCamera() light=CreateLight() RotateEntity light,0,0,90 Global reload.test Global control.test = Last test maxshape = control\shape control.test = First test minshape = control\shape Repeat If KeyDown(200)=True Then control\y = control\y + 1 If KeyDown(203)=True Then control\x = control\x - 1 If KeyDown(205)=True Then control\x = control\x + 1 If KeyDown(208)=True Then control\y = control\y - 1 If (KeyHit(26)=True And control\shape <> minshape) Then control = Before control If (KeyHit(27)=True And control\shape <> maxshape) Then control = After control For update.test = Each test PositionEntity update\shape, update\x, update\y, update\z Next If KeyHit(64) Then save If KeyHit(65) Then load UpdateWorld RenderWorld Text 20, 20, message$ Flip Until KeyHit(1) End ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Function save() fileout = WriteFile("Shay1.SM") For n.test = Each test Print Str$(n) WriteString (fileout, Str$(n)) ; WriteFloat (fileout,cube) ; WriteFloat (fileout,EntityX) ; WriteFloat (fileout,EntityY) ; WriteFloat (fileout,EntityZ) Next CloseFile( fileout ) Cls message$ = "game data written to file..." FlushKeys End Function ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Function load() Cls For destroy.test = Each test FreeEntity destroy\shape Delete destroy Next filein = ReadFile("Shay1.SM") While Not Eof(filein) Read1$ = ReadString$( filein ) ;Read1$ = ReadFloat( filein ) If Read1$<>"" Then reload.test = RestoreTestInfo(Read1$) EndIf Wend DebugLog "finished with loaded file" FlushKeys control = First test End Function ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Function RestoreTestInfo.test( SavedString$ ) message$ = "Read from file --> " + SavedString$ + " ... " DebugLog message$ ; typical type looks like ... [-3,-4,14,"LQT",18898368] SavedString$ = Mid$( SavedString$, 2, Len(SavedString$)-2) ; remove end square brackets firstcomma = Instr(SavedString$, ",") xvalue% = Left$(SavedString$, firstcomma-1) ; convert first value (up to comma) to an int SavedString$ = Mid$( SavedString$, firstcomma+1, Len(SavedString$)-firstcomma+1) ; eat up to 1st comma firstcomma = Instr(SavedString$, ",") yvalue% = Left$(SavedString$, firstcomma-1) ; convert up to new first comma to another int SavedString$ = Mid$( SavedString$, firstcomma+1, Len(SavedString$)-firstcomma+1) ; eat up to new 1st comma firstcomma = Instr(SavedString$, ",") zvalue% = Left$(SavedString$, firstcomma-1) ; convert up to new first comma to another int SavedString$ = Mid$( SavedString$, firstcomma+1, Len(SavedString$)-firstcomma+1) ; eat up to new 1st comma ThirdString$ = Mid$( SavedString$, 2, Len(SavedString$)) ; remove first quote from string firstcomma = Instr(SavedString$, ",") ThirdString$ = Mid$(SavedString$, 2, firstcomma-3) message$ = message$ + "x: " + xvalue + " y: " + yvalue + " z: " + zvalue + " name: " + ThirdString$ DebugLog message$ reloaded.test = New test ; make a new type reloaded\x = xvalue% ; and assign the reloaded\y = yvalue% ; reloaded values reloaded\z = zvalue% reloaded\name$ = ThirdString$ ; to the fields reloaded\shape = CreateCube() Return (reloaded) End Function ;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
Code Archives Forum