the black hole 
it all ends here. 
bhC login
name:
pass:
 
home
 
 the black hole's front page. all the latest vb rpg development and vbgaming news, as well as this week's feature rpg project!
articles
 
rpg theory (7)
directx in vb (3)
vb rpg programming (4)
miscellaneous (4)
files
 
rpg / tile engine source (23)
full vb games w/ source (5)
'helper' libraries and utilities (13)
effect samples (5)
miscellaneous (10)
rpg projects
 
 vb rpg projects under development by members of the bhC. show them your support, and check out their games!
message board
 
 a forum to interact with your fellow vb rpg programmers. you do *not* have to be a bhC member to post!
links
 
vb rpg programming (5)
vb gaming (14)
general game programming (6)
vb sites (6)
miscellaneous (2)
general rpg programming (2)
contact
 
 you want to contact the guy behind the black hole? here's where to do it!


 
 


<< Back to Category List

Simple Game Scripting Part 1:
Storing/Reading Your Scripts
- page 4


pages: prev | 1 | 2 | 3 | 4 | 5 | next


Actually Loading the Script File

Now that we've got all of our storage structures created, we can actually load our script file. Put this function into your class module:

Public Function LoadScriptFile(sFileName As String) As Integer
  'If the file is empty/doesn't exist, don't open it 
  'and exit the function with a return value of 1.
  If FileLen(App.Path & "\" & sFileName) = 0 Then
    LoadScriptFile = 1
    Exit Function
  End If

  'sTemp: a temporary string variable - stores the input from the file
  'bEndLoop + bEndLoop2: booleans for controlling when to stop reading
  'aTemp(): array of strings for splitting up parameters.
  Dim sTemp As String, bEndLoop As Boolean, bEndLoop2 As Boolean
  Dim aTemp() As String

  'Initialize the boolean control variables
  bEndLoop = False
  bEndLoop2 = False

  'Load up the specified script file
  Open App.Path & "\" & sFileName For Input As #1

  Do
    'Input a line from the file into sTemp
    Line Input #1, sTemp

    'This is to check whether this is the end of the file or not...
    'Checks against everything before the first occurance of a 'space'
    Select Case Left$(sTemp, InStr(sTemp, " ") - 1)
    'If we're at the end of the file set the flag to end the reading loop
      Case "*EOF*"
        bEndLoop = True
        'Otherwise we're at the start of a block...
      Case "*Block*"
        iLastBlock = iLastBlock + 1
        'Remove the "*Block* " at the beginning of the string
        sTemp = Right$(sTemp, Len(sTemp) - Len("*Block*") - 1)
        'Put everything before the first 'space' character into the BlockType
        CommandBlocks(iLastBlock).BlockType = Left$(sTemp, InStr(sTemp, " ") - 1)
        'Put everything after the first 'space' character into the BlockName
        CommandBlocks(iLastBlock).BlockName = Right$(sTemp, Len(sTemp) - InStr(sTemp, " "))
        With CommandBlocks(iLastBlock)
        'Initialize the Commands() array
          ReDim .Commands(0)
          Do
            'Read a command line
            Line Input #1, sTemp
            'If we're at the end of the block...
            If sTemp = "*EndBlock* " & .BlockName Then
              'Get rid of the empty element of the Commands() array
              ReDim Preserve .Commands(UBound(.Commands()) - 1)
              'Set the boolean control variable to exit the inner loop
              bEndLoop2 = True
            Else
              'Read in the command name as everything before the left bracket
              .Commands(UBound(.Commands())).Command = Left$(sTemp, InStr(sTemp, " (") - 1)
              'Split up everything between the left and right brackets using commas
              aTemp() = Split(Mid(sTemp, InStr(sTemp, "(") + 1, Len(sTemp) - InStr(sTemp, "(") - 1), ",")
              'Increase the Parameters() array's size to how many parameters there are
              ReDim .Commands(UBound(.Commands())).Parameters(UBound(aTemp))
              'Copy the parameters from the 'splitted' array
              .Commands(UBound(.Commands())).Parameters() = aTemp()
              'Add an empty element to the Commands() array
              ReDim Preserve .Commands(UBound(.Commands()) + 1)
            End If
          Loop Until bEndLoop2
          'Reset the control variable
          bEndLoop2 = False
        End With
      Case Else
        'This should *never* happen because all commands
        'should be contained within blocks.
        Beep
        LoadScriptFile = 1
        Exit Function
    End Select
  Loop Until bEndLoop
  Close #1
  'Return a value of 0
  LoadScriptFile = 0
End Function



pages: prev | 1 | 2 | 3 | 4 | 5 | next