Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Binary Saving Question
#1
I changed the way MS was saving files and decided to save all the shops in one and spells in one etc.

After some testing I noticed that the server only clears the first shop, spell, item. It doesn't clear the rest after that. I think there is something wrong with my method?

Here is spells for example and since the SpellEditor LevelReq has to be set to 1 because the scroll bars minimum is 1 it gives me an error when I try to edit any spells after the first one. I hope this is making sense Tongue.

So I clear the spell.
Code:
Sub ClearSpell(ByVal Index As Long)
    Call ZeroMemory(ByVal VarPtr(Spell(Index)), LenB(Spell(Index)))
    Spell(Index).Name = vbNullString
    Spell(Index).LevelReq = 1 ' Needs to be 1 for spell editor
End Sub

Setting LevelReq to 1 like the original MS did. Then my saving and loading code.

Code:
Sub SaveSpells()
Dim i As Long
  
    Call SetStatus("Saving Spells... ")
        For i = 1 To MAX_SPELLS
            If Not FileExist(GENERAL_PATH & "spells.dat") Then
                Call SaveSpell(i)
            End If
        Next i
End Sub

Sub SaveSpell(ByVal SpellNum As Long)
Dim FileName As String
Dim f As Integer
Dim StartByte As Long

    FileName = App.Path & GENERAL_PATH & "spells.dat"
    f = FreeFile
  
    StartByte = LenB(Spell(SpellNum)) * (SpellNum - 1) + 1
  
    Open FileName For Binary As #f
        Put #f, StartByte, Spell(SpellNum)
    Close #f
End Sub

Sub LoadSpells()
Dim FileName As String
Dim StartByte As Long
Dim i As Long
Dim f As Long

  Call CheckSpells
  
    For i = 1 To MAX_SPELLS
        
        FileName = App.Path & GENERAL_PATH & "spells.dat"
        f = FreeFile
  
        StartByte = LenB(Spell(i)) * (i - 1) + 1
  
        Open FileName For Binary As #f
            Get #f, StartByte, Spell(i)
        Close #f

    Next i
End Sub

Thanks for the help! Big Grin
Reply
#2
Using the hex editor you said it's only showing that only one of the Spells is saved and that's the first one. It's the same with all the other files too, but that's not really surprising since they are all saved the same way.

So for some reason it's not even saving all the maximums of the data. It's just saving the first one then going to the next file.

Also, I was under the impression using LenB the way I did gathers all the bytes automatically from the UDT?
Reply
#3
So the reason it wasn't saving them all I guess was because I had that If Not FileExist check inside of the For loop. I moved it outside of it and it seems to be working fine now.
Reply
#4
So I just added Anthony's subs for saving 1 spell file, just for a little testing. Currently, all my spells are saved in separate binary files. All together, the the file siles for my spells folder is about 7kb. My single spells file though, is 14kb.

I haven't really done anything major, just added a command button to save the spells differently. Why is there such a gap in the file size?
Reply
#5
So now I am determined to find out whats going on and so far I have this.

Saving to multiple files as default in MS4, MAX_SPELLS = 255, default MS Spell UDT
Bytes - 7,395

Saving to one file, using LenB to determine the total bytes, MAX_SPELLS = 255, default MS Spell UDT.
Bytes - 12,220

Saving to one file, counting my own bytes from the UDT, MAX_SPELLS = 255, default MS Spell UDT.
Bytes - 7,395

So Dave.. How come LenB is screwing me haha.
Reply
#6
Using len() cut my 13.9kb file down to 8.21kb
Reply
#7
Using Len() is now the same size as if I were to count the bytes myself.

It is saving and loading correctly. Any way we can make it smaller? xD.
Reply
#8
Make sure to use the smallest types in your UDT?
Reply
#9
How do you save only the string size needed instead of the entire 20 bytes every time?
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)