22-12-2006, 07:24 AM
Alright as many of you (especially Sise
) know... Binary Accounts can get tricky at times, and they're a pain to have to constantly update all the time to make sure that everything lines up right. The solution? This tutorial.
Tutorial Difficulty 2/5 - Easy
Okay so in Dave's tutorial (who i'm not downplaying.. dave is my hero), you would store every value individually into the binary file after you opened it, which is not a bad way of doing it. My way however, there is one simple line to both load and save, after the binary file is open.
The only current drawback, however, is that it also stores your AccountVariables (the temoraries), into your account along with all of your account stuff. Not that it's a huge deal... you store like an extra 20 Bytes or something per account... but if you want... (since i know that Dave will kill me if i don't mention it), is create another UDT that's something like Type TempPlayerRec, store the info there, and pull it from that rather than the Account or PlayerRecs. But, without further adieu... onto the easy tutorial.
First would be the... Save Player Sub. Now since this is all incredibly easy i'm not going to explain line by line, but simply post the code.
However, the Line, "Put #f, , Player(index)" should be of particular interest... that's the line that saves EVERYTHING. It basically goes through the UDT line at a time and stores it all into the binary file. After it has completed storing it... it closes the file. See how easy that was!!!!!!!!
Okay same thing, but using get, rather than put... yep, you guessed it! LoadPLayer!!!!!
Okay now, these are only minor adjustments that need to be made to these other files, just so it verifies your account data and a few other things, and whalla... you're done!
Replace the entire Function AccountExist, with this code:
Replace the entire PasswordOK Function, with this code:
Alright, that's all for this lesson. I hope this helps you out. I'd like to give all credit for this to Dave. Had he not written the original tutorial, i probably wouldn't have made an attempt to understand any of this stuff.
Post any questions/comments you might have.

Tutorial Difficulty 2/5 - Easy
Okay so in Dave's tutorial (who i'm not downplaying.. dave is my hero), you would store every value individually into the binary file after you opened it, which is not a bad way of doing it. My way however, there is one simple line to both load and save, after the binary file is open.
The only current drawback, however, is that it also stores your AccountVariables (the temoraries), into your account along with all of your account stuff. Not that it's a huge deal... you store like an extra 20 Bytes or something per account... but if you want... (since i know that Dave will kill me if i don't mention it), is create another UDT that's something like Type TempPlayerRec, store the info there, and pull it from that rather than the Account or PlayerRecs. But, without further adieu... onto the easy tutorial.
First would be the... Save Player Sub. Now since this is all incredibly easy i'm not going to explain line by line, but simply post the code.
Code:
Sub SavePlayer(ByVal index As Long)
Dim FileName As String
Dim f As Long
FileName = App.Path & "\accounts\" & Trim$(Player(index).Login) & ".bin"
f = FreeFile
Open FileName For Binary As #f
Put #f, , Player(index)
Close #f
End Sub
However, the Line, "Put #f, , Player(index)" should be of particular interest... that's the line that saves EVERYTHING. It basically goes through the UDT line at a time and stores it all into the binary file. After it has completed storing it... it closes the file. See how easy that was!!!!!!!!
Okay same thing, but using get, rather than put... yep, you guessed it! LoadPLayer!!!!!
Code:
Sub LoadPlayer(ByVal index As Long, ByVal name As String)
Dim FileName As String
Dim f As Long
Call ClearPlayer(index)
FileName = App.Path & "\accounts\" & Trim(name) & ".bin"
f = FreeFile
Open FileName For Binary As #f
Get #f, , Player(index)
Close #f
End Sub
Okay now, these are only minor adjustments that need to be made to these other files, just so it verifies your account data and a few other things, and whalla... you're done!
Replace the entire Function AccountExist, with this code:
Code:
Function AccountExist(ByVal name As String) As Boolean
Dim FileName As String
FileName = "accounts\" & Trim(name) & ".bin"
If FileExist(FileName) Then
AccountExist = True
Else
AccountExist = False
End If
End Function
Replace the entire PasswordOK Function, with this code:
Code:
Function PasswordOK(ByVal name As String, ByVal Password As String) As Boolean
Dim FileName As String
Dim RightPassword As String * NAME_LENGTH
Dim temp As String
Dim nLen As Integer
Dim nFileNum As Integer
PasswordOK = False
If AccountExist(name) Then
FileName = App.Path & "\Accounts\" & Trim$(name) & ".bin"
nFileNum = FreeFile
Open FileName For Binary As #nFileNum
Get #nFileNum, NAME_LENGTH, RightPassword
Close #nFileNum
If UCase$(Trim$(Password)) = UCase$(Trim$(RightPassword)) Then
PasswordOK = True
End If
End If
End Function
Alright, that's all for this lesson. I hope this helps you out. I'd like to give all credit for this to Dave. Had he not written the original tutorial, i probably wouldn't have made an attempt to understand any of this stuff.
Post any questions/comments you might have.