SakhaliaNetHome PageSahara TerritoryVorKutaAcceptance of cookiesAcceptance of cookies

~ DAGOVAR :: A VISUAL BASIC GAME ~

Visual Basic 6 - Source codes of algorithms

In this page I list a number of examples of source codes and algorithms, that do diverse tasks that are usually used in many applications. I used all of these source codes when programming the Dagovar games, but certainly, I have adapted the names of identificators to standard english, so you can understand them and guess what they are used for.

Saving to a binary file

This is the procedure that CharEdit application uses to save the data of a character in a .chr file. Note how the first part of the source code that revolves about a text field control is used to check if the user has wrote or not the extension (.chr) when writing the file name. Yes, I know people don't usually would write the extension when typing the filename, but if they would do, I don't want the program to name the file with a double-extension, like myfile.chr.chr. That wouldn't be professional. Also note that a binary file is much better to store data than a text file, because it will be lesser in size and it would prevent a regular user from hacking into the file data.

Private Sub Save()
On Error GoTo Fin
Dim intFile As Integer
intFile = FreeFile
If Right(txtFile.Text, 4) = ".chr" Then
If fso.FileExists(App.Path & "\" & Trim(txtFile.Text)) Then
fso.DeleteFile App.Path & "\" & Trim(txtFile.Text)
End If
Open App.Path & "\" & Trim(txtFile.Text) For Binary As intFile
Else
If fso.FileExists (App.Path & "\" & Trim(txtFile.Text) & ".chr") Then
fso.DeleteFile App.Path & "\" & Trim(txtFile.Text) & ".chr"
End If
Open App.Path & "\" & Trim(txtFile.Text) & ".chr" For Binary As intFile
End If
Put #intFile, , Character.strName
Put #intFile, , Character.bytModel
Put #intFile, , Character.bytResistance
Put #intFile, , Character.bytMaximumLoad
Put #intFile, , Character.bytActionPoints
Put #intFile, , Character.bytCuration
Put #intFile, , Character.bytFirearms
Put #intFile, , Character.bytGrenades
Put #intFile, , Character.bytMelee
Put #intFile, , Character.bytArmor
Put #intFile, , Character.sngLoad
Put #intFile, , Character.sngPoints
Put #intFile, , Character.sngLoadPoints
Dim i As Byte
For i = 1 To 16
Put #intFile, , Character.Inv_Items(i).bytId
Put #intFile, , Character.Inv_Items(i).intQuantity
Next i
Close intFile
Exit Sub
Fin:
Call MsgBox("There was an error when saving the file", vbOK, "Message")
End Sub

Loading from a binary file

This is the procedure that CharEdit application uses to load the data of a character from a .chr file.

Private Sub Load()
On Error GoTo Fin
Dim intFile As Integer
intFile = FreeFile
Open App.Path & "\" & filLoad.List(filLoad.ListIndex) For Binary As intFile
Get #intFile, , Character.strName
Get #intFile, , Character.bytModel
Get #intFile, , Character.bytResistance
Get #intFile, , Character.bytMaximumLoad
Get #intFile, , Character.bytActionPoints
Get #intFile, , Character.bytCuration
Get #intFile, , Character.bytFirearms
Get #intFile, , Character.bytGrenades
Get #intFile, , Character.bytMelee
Get #intFile, , Character.bytArmor
Get #intFile, , Character.sngLoad
Get #intFile, , Character.sngPoints
Get #intFile, , Character.sngLoadPoints
Dim i As Byte
For i = 1 To 16
Get #intFile, , Character.Inv_Items(i).bytId
Get #intFile, , Character.Inv_Items(i).intQuantity
Next i
Close intFile
Exit Sub
Fin:
Call MsgBox("There was an error when loading the file", vbOK, "Message")
End Sub

Rearranging an array

When we remove an object from a list, we have to use a procedure like this one to displace each of the subsequent objects one position 'to the left', so we leave no 'holes' on the list. This algorithm is used to rearrange the inventories of items. When an object is removed from the list, its Id is set to 0; then, when the loop finds an object whose Id is 0, it copies all the data of the object that is next on the list into its position, and then delete all the data of that next object, removing it. The loop that does this operation runs so many times as the number of cells that the array has minus one.

Private Sub Rearrange_Items()
Dim i As Byte
For i = 1 To 15
If Character.Inv_Items(i).bytId <= 0 Then
Character.Inv_Items(i).bytId = Character.Inv_Items(i + 1).bytId
Character.Inv_Items(i).intQuantity = Character.Inv_Items(i + 1).intQuantity
Character.Inv_Items(i + 1).bytId = 0
Character.Inv_Items(i + 1).intQuantity = 0
End If
Next i
End Sub

Swapping data

Swaping data between two entities involves the use of a third, hidden entity which will temporarily store data from one of the entities whose data will be swapped. Data from entity one is copied into the hidden entity, then data from entity two is copied into entity one and finally data from the hidden entity is copied into entity two. In Dagovar games, the two selected items on the interface bar can swap their positions by right clicking on any of them, and the following is the source code used for that purpose.

For i = 1 To MAX_HUMAN_CHARS
If blnSelected(i) = True Then
If Trim(Characters(i).strStatus) <> "Inactive1" And Trim(Characters(i).strStatus) <> "Inactive2" And Trim(Characters(i).strStatus) <> "Walking" Then Exit Sub
Dim Temp As Item
Temp.bytId = Characters(i).Selec_Items(1).bytId
Temp.bytLoad = Characters(i).Selec_Items(1).bytLoad
Temp.bytLoadType = Characters(i).Selec_Items(1).bytLoadType
Temp.bytMode = Characters(i).Selec_Items(1).bytMode
Temp.intQuantity = Characters(i).Selec_Items(1).intQuantity
Characters(i).Selec_Items(1).bytId = Characters(i).Selec_Items(2).bytId
Characters(i).Selec_Items(1).bytLoad = Characters(i).Selec_Items(2).bytLoad
Characters(i).Selec_Items(1).bytLoadType = Characters(i).Selec_Items(2).bytLoadType
Characters(i).Selec_Items(1).bytMode = Characters(i).Selec_Items(2).bytMode
Characters(i).Selec_Items(1).intQuantity = Characters(i).Selec_Items(2).intQuantity
Characters(i).Selec_Items(2).bytId = Temp.bytId
Characters(i).Selec_Items(2).bytLoad = Temp.bytLoad
Characters(i).Selec_Items(2).bytLoadType =Temp.bytLoadType
Characters(i).Selec_Items(2).bytMode = Temp.bytMode
Characters(i).Selec_Items(2).intQuantity = Temp.intQuantity
Exit Sub
End If
Next i

Determining if a number is even or odd

Determining if a number is even or odd can be necessary for some tasks. This function continuously subtract 2 units to the number given as parameter, until that number become 0 or 1; in the first case, it is a even number, otherwise it is an odd number. Combat Dolls games use this function to manage the alternate rows of the hex-based terrain.

Private Function Is_Odd(ByVal intNumber As Integer) As Boolean
Do While intNumber > 0
intNumber = intNumber - 2
Loop
If intNumber = 0 Then Is_Odd = False Else Is_Odd = True
End Function


About Dagovar

~ About Dagovar ~

Defining characters

~ Defining characters ~

Desert Vixens 2 maps

~ Desert Vixens 2 maps ~

Desert Vixens 3 maps

~ Desert Vixens 3 maps ~

Tile-based terrains

~ Tile-based terrains ~

Random maps

~ Random maps ~

Screenshots

~ Screenshots ~

API functions for games

~ API functions for games ~

Source code examples

~ Source code examples ~



Return To Index

Privacy Policy