Results 1 to 4 of 4

Thread: Setting Variables From A Text File

  1. #1

    Thread Starter
    Addicted Member scotty85_2000's Avatar
    Join Date
    Feb 2001
    Posts
    198

    Question

    In a text file which I am loading for commentary in a game, I have a line with "P# passes to R#." In the game, before this text file loads, I already set P# and R# as variables, except under the names T1P1 (as string) and T1R1 (ditto).

    How do I make it so that P# = T1P1 and R# = T1R1 when the text file loads? I am already using this code to load the file, so I'd be grateful if u could copy it and add in the code for doing what I want...

    Private Sub Form_Load()
    Randomize
    Dim i As Integer, intCount As Integer, o As Integer
    i = FreeFile
    Open "C:\WINDOWS\Profiles\Michael's Desktop\My Documents\comment.txt" For Input As #i
    Input #1, intCount
    For o = 1 To intCount
    Line Input #i, strEntranceOne(o)
    Next o

    End Sub

    Cheers...

  2. #2
    Fanatic Member
    Join Date
    Sep 1999
    Location
    Bethel, North Carolina, USA
    Posts
    987
    If you have VB6 you can use the Replace function (not sure about the exact syntax as I have VB5, so you may have to fool around with it)...

    Code:
    strEntrance(0) = Replace(strEntance(0), "P#", T1P1)
    strEntrance(0) = Replace(strEntrance(0), "R#", T1R1)
    {Insert random techno-babble here}

    {Insert quote from some long gone mofo here}

  3. #3

    Thread Starter
    Addicted Member scotty85_2000's Avatar
    Join Date
    Feb 2001
    Posts
    198
    when i run this, the debug highlights "replace" and says sub or function not defined

    any ideas?

    (i'm using VB5E)

  4. #4
    Fanatic Member
    Join Date
    Sep 1999
    Location
    Bethel, North Carolina, USA
    Posts
    987
    There is no Replace function for VB5, but here is a substitute I found on this message board on this thread.

    Drop this in your project and try the code again....
    Code:
    ' function originally written by Stevie
    
    Public Function Replace(ByVal strSource As String, _
                                      ByVal strWhat As String, _
                                      ByVal strWith As String) As String
    '***************************************************************************
    'Purpose:       Replaces all occurances of one string with another string.
    'Parameters:    strSource - String to be changed.
    '               strWhat   - What needs replacing.
    '               strWith   - What it should be replaced with.
    'Returns:       String    - The changed string.
    '***************************************************************************
    
    Dim intstrWhatLength As Integer
    Dim intReplaceLength As Integer
    Dim intStart As Integer
    
      intstrWhatLength = Len(strWhat)
      
      If intstrWhatLength = 0 Then
        
        Replace = strSource
        Exit Function
      
      End If
      
      intReplaceLength = Len(strWith)
      intStart = InStr(1, strSource, strWhat)
      
      Do While intStart > 0
          
        strSource = Left(strSource, intStart - 1) + strWith + Right(strSource, Len(strSource) - (intStart + intstrWhatLength - 1))
        intStart = InStr(intStart + intReplaceLength, strSource, strWhat)
      
      Loop
      
      Replace = strSource
    
    End Function
    {Insert random techno-babble here}

    {Insert quote from some long gone mofo here}

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width