Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Very Simple Binary (Single File) Saving For All
#1
Alright, Dave made a great tutorial a while back. Showing everyone how big an optimization Binary really was (atleast when in comparison to the NI system that is currently used). His method was right-on, however, not overly difficult to implement and it worked very well. There was, however, something i struggled a lot with... and that was... everytime you added something new to a UDT (like the NPCRec for example), you had to go back in, and make sure you added in both the get/put line for it, as well as making sure it was cleared out. In this tutorial, the only that you'll be responsible for doing now... is making sure you clear it out correctly.

I will not be holding your hand throughout this tutorial, i really don't think it's necessary if you'll just think (and i know for some of you, that's really asking a lot). I'm going to post one sub, and then what my system looks like, and how it works... so you can make it work for you. I would like to point out however, and it was a problem even in dave's previous sytem... if you have a string, that is NOT a fixed string, you'll start running into problems later on.


Tutorial Difficulty 2/5 - Easy When Read


Here is an example of what your Sub SaveNPC would look like when you completed Dave's Binary Tutorial Correctly:

Code:
Sub SaveNpc(ByVal NpcNum As Long)
Dim FileName As String
Dim nFileNum As Integer
Dim StartByte As Long

    FileName = App.Path & "\data\npcs.bin"
    nFileNum = FreeFile
    
    Open FileName For Binary As #nFileNum
        StartByte = 292 * (NpcNum - 1) + 1

        Put #nFileNum, StartByte, Npc(NpcNum).Name
        Put #nFileNum, , Npc(NpcNum).AttackSay
        Put #nFileNum, , Npc(NpcNum).Sprite
        Put #nFileNum, , Npc(NpcNum).SpawnSecs
        Put #nFileNum, , Npc(NpcNum).Behavior
        Put #nFileNum, , Npc(NpcNum).Range
        Put #nFileNum, , Npc(NpcNum).DropChance
        Put #nFileNum, , Npc(NpcNum).DropItem
        Put #nFileNum, , Npc(NpcNum).DropItemValue
        Put #nFileNum, , Npc(NpcNum).STR
        Put #nFileNum, , Npc(NpcNum).DEF
        Put #nFileNum, , Npc(NpcNum).Speed
        Put #nFileNum, , Npc(NpcNum).MAGI
    Close #nFileNum
  
End Sub

Now, if you weren't sure what i meant earlier with all of the 'put' lines, that should be showing you more than enough. Now, the problem like i mentioned before, was that if you add something new, you'd have to update the StartByte Size, as well as having to add the put/get in the exact same spots on both, to make sure it was read back correctly. Here's an example of my new way of doing the EXACT same thing

Code:
Sub SaveNpc(ByVal NpcNum As Long)
Dim FileName As String
Dim nFileNum As Integer
Dim StartByte As Long

    FileName = App.Path & "\data\npcs.bin"
    nFileNum = FreeFile
    
    StartByte = LenB(Npc(NpcNum)) * (NpcNum - 1) + 1
    
    Open FileName For Binary As #nFileNum
        Put #nFileNum, StartByte, Npc(NpcNum)
    Close #nFileNum
End Sub

So basically, in this version. You get the length in bytes... which is what the LenB() function does... rather than having to go through and say okay the whole NPCRec has 19 Bytes... it does it all for you. Then, you just Put , , where the npc actually belongs, and you place the entire UDT there all at once. See how much more simple that is?

My "challenge" to you now, is to make the LoadNpc function do this same type of thing. It is very simple, and should jump out at everyone here as soon as they spend atleast 5 seconds looking at it. If you try several times with no luck, please feel free to let me know and i'll be able to help you out.

This system works for Everything, although accounts are slightly different. You can check my other tutorial on that, but again, challenge yourself and try and make it work for Items, Maps, Spells, and anything else you may need.
Reply


Messages In This Thread

Forum Jump:


Users browsing this thread: 1 Guest(s)