Ooops
March 01, 2021, 10:54:57 PM

Author Topic: [bb] Saving/Loading Types by BlackJumper [ 1+ years ago ]  (Read 547 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Saving/Loading Types by BlackJumper [ 1+ years ago ]
« on: June 29, 2017, 12:28:40 AM »
Title : Saving/Loading Types
Author : BlackJumper
Posted : 1+ years ago

Description : 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...

<a href="codearcsb4d9.html?code=161" target="_blank">http://www.blitzbasic.com/codearcs/codearcs.php?code=161[/url]


Code :
Code: BlitzBasic
  1. [code]
  2. ;------------------------------------------------------------------
  3. ;----   demo of saving and reloading game data using Str$()    ----
  4. ;----             Blackjumper  - Jan 2005                      ----
  5. ;------------------------------------------------------------------
  6.  
  7. Type test
  8.         Field x
  9.         Field y
  10.         Field name$
  11. End Type
  12.  
  13. For count = 1 To 4
  14.         n.test = New test
  15.         nx = Rand(10)
  16.         ny = Rand(10)+100
  17.         n
  18. ame$ = Chr(Rand(26)+65) +Chr(Rand(26)+65) +Chr(Rand(26)+65)
  19. Next
  20.  
  21.  
  22. fileout = WriteFile("C:storedgame.txt")
  23. For n.test = Each test
  24.         Print Str$(n)
  25.         WriteString (fileout, Str$(n))
  26. Next
  27. CloseFile( fileout )
  28.  
  29. Print "game data written to file... press any key to continue"
  30. Print
  31. WaitKey
  32.  
  33. Print "deleting all instances of type 'test'..."
  34. For n.test = Each test
  35.         Delete n
  36. Next
  37. Print "printing all type information...
  38. Print "________________________________"
  39. For n = Each test
  40.         Print Str$(n)
  41. Next
  42. Print "--------------------------------"
  43. WaitKey
  44. Print
  45.  
  46.  
  47. Print "... now reading from disk..."
  48. filein = ReadFile("C:storedgame.txt")
  49. While Not Eof(filein)
  50. Read1$ = ReadString$( filein )
  51. RestoreTestInfo(Read1$)
  52. Wend
  53.  
  54. Print "printing all reloaded type information...
  55. Print "------------------------"
  56. For n.test = Each test
  57.         Print Str$(n)
  58. Next
  59. Print "------------------------"
  60.  
  61. WaitKey
  62. End
  63.  
  64. Function RestoreTestInfo( SavedString$ )
  65.         Print "Read from file --> " + SavedString$
  66.         SavedString$ = Mid$( SavedString$, 2, Len(SavedString$)-2) ; remove end square brackets
  67.        
  68.         firstcomma = Instr(SavedString$, ",")
  69.         firstvalue% = Left$(SavedString$, firstcomma-1)   ; convert first value (up to comma) to an int
  70.        
  71.         SavedString$ = Mid$( SavedString$, firstcomma+1, Len(SavedString$)-firstcomma+1)  ; eat up to 1st comma
  72.  
  73.         firstcomma = Instr(SavedString$, ",")
  74.         secondvalue% = Left$(SavedString$, firstcomma-1)  ; convert up to new first comma to another int
  75.        
  76.         SavedString$ = Mid$( SavedString$, firstcomma+1, Len(SavedString$)-firstcomma+1)  ; eat up to new 1st comma
  77.         ThirdString$ = Mid$( SavedString$, 2, Len(SavedString$)-2)                        ; remove quotes from string
  78.  
  79.         reloaded.test = New test                        ; make a new type
  80.         reloadedx = firstvalue%                 ; and assign the
  81.         reloadedy = secondvalue%                        ; reloaded values
  82.         reloaded
  83. ame$ = ThirdString$             ; to the fields
  84.        
  85. End Function
  86.  
  87.  
[/code]

Comments :


Xerra(Posted 1+ years ago)

 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.


BlackJumper(Posted 1+ years ago)

 Someone (Sefery) was asking for an example of how to load and save object position by using the F6 and F7 keys. <a href="../Community/posts7e11-2.html?topic=59739" target="_blank">http://www.blitzbasic.com/Community/posts.php?topic=59739[/url]I modified their code as follows to achieve this...
Code: [Select]
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
nx = Rand(10)
ny = Rand(10)
nz = Rand(10)+10
n
ame$ = Chr(Rand(26)+65) +Chr(Rand(26)+65) +Chr(Rand(26)+65)
nshape = CreateCube()
EntityColor nshape, count*50, 0,0
Next

camera=CreateCamera()

light=CreateLight()

RotateEntity light,0,0,90

Global reload.test
Global control.test = Last test
maxshape = controlshape
control.test = First test
minshape = controlshape


Repeat

If KeyDown(200)=True Then controly = controly + 1
If KeyDown(203)=True Then controlx = controlx - 1
If KeyDown(205)=True Then controlx = controlx + 1
If KeyDown(208)=True Then controly = controly - 1

If (KeyHit(26)=True And controlshape <> minshape) Then control = Before control
If (KeyHit(27)=True And controlshape <> maxshape) Then control = After control

For update.test = Each test
    PositionEntity updateshape, updatex, updatey, updatez
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 destroyshape
    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
reloadedx = xvalue%    ; and assign the
reloadedy = yvalue%     ; reloaded values
reloadedz = zvalue%
reloaded
ame$ = ThirdString$ ; to the fields
reloadedshape = CreateCube()

Return (reloaded)

End Function
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal