Valhalla Legends Archive

Programming => Battle.net Bot Development => Topic started by: Dyndrilliac on June 02, 2004, 01:41 AM

Title: Problem with Message Queue
Post by: Dyndrilliac on June 02, 2004, 01:41 AM
Ok. I was making a new message Queue to go with my bot's facelift. Here are the relevant parts:

Public Sub AddQ(Message As String)
   Do Until Queue(QNum) = vbNullString
       QNum = QNum + 1
   Loop
   Queue(QNum) = Message
End Sub
'// Message Queue -- Declarations
Public Queue() As String
Public QNum    As Integer
Public QDelay  As Single
Public Sub LoadConfig()
   QNum = 0
   ReDim Queue(0)
   Queue(0) = vbNullString
   ...
Private Sub tQueue_Timer()

   If Connected = False Then
       Exit Sub
   End If

   If Queue(QNum) = vbNullString Then
       Exit Sub
   End If

   Dim i As Integer
   For i = 0 To QNum
       QDelay = ((Len(Queue(i)) / 10) / 2)
       Pause QDelay, True
       Send Queue(i)
       Queue(i) = vbNullString
   Next i
End Sub
Sub Pause(ByVal fSeconds As Single, Optional ByVal AllowEvents As Boolean = True)
   Dim fTimer As Single
   If AllowEvents Then
       fTimer = Timer
       Do While Timer - fTimer < fSeconds
           Sleep 100
           DoEvents
           If Timer < fTimer Then
             fTimer = fTimer - 86400
           End If
       Loop
   Else
       Sleep fSeconds * 1000
   End If
End Sub


Now. The problem is when I enter too many message into the Queue too quickly, I get a Subscript out of Range error in the AddQ Sub, posted above. Apart from that it works perfectly.

I was thinking a temporary fix would be to On Error, re initialize the array, clear it, and return to the Queue's flow...but I want to know if there is a way to prevent the error from happening at all. Other than that the system works pretty much perfectly.
Title: Re:Problem with Message Queue
Post by: Dyndrilliac on June 02, 2004, 01:57 AM
Hm, the problem may already be fixed...I modified the AddQ Function to basically fix the problem on it's own when it errored.

Public Sub AddQ(Message As String)
   On Error GoTo ErrorHandle
   
TryAgain:
   
   Do Until Queue(QNum) = vbNullString
       QNum = QNum + 1
   Loop
   Queue(QNum) = Message
   Exit Sub
   
ErrorHandle:

   QNum = 0
   ReDim Queue(0)
   Queue(0) = vbNullString
   GoTo TryAgain
   
End Sub


P.S.: If any one would like to use this, by all means do so, and post any bugs you fix.

-- Edit --

Another bug I noticed is if someone enters a lot(like 4) small messages very quickly the Queue basically has no effect and the user flood off.

Public iFloodThresh As Integer
Public sFloodThresh As String

Public Sub AddQ(Message As String)
   On Error GoTo ErrorHandle
   If sFloodThresh = Message Or Len(sFloodThresh) = Len(Message) Then
       iFloodThresh = iFloodThresh + 1
       If iFloodThresh >= 4 Then
           Pause 2, True
       End If
   Else
       iFloodThresh = 0
   End If
TryAgain:
   Do Until Queue(QNum) = vbNullString
       QNum = QNum + 1
   Loop
   Queue(QNum) = Message
   sFloodThresh = Message
   Exit Sub
ErrorHandle:
   QNum = 0
   ReDim Queue(0)
   Queue(0) = vbNullString
   GoTo TryAgain
End Sub
Title: Re:Problem with Message Queue
Post by: CrAz3D on June 02, 2004, 09:35 AM
Wouldn't using ubound & such be easier than
Do Until Queue(QNum) = vbNullString
       QNum = QNum + 1
   Loop
   Queue(QNum) = Message
   Exit Sub


like Queue(Ubound(Queue))=message
redim preserve queue(ubound(queue)+1)
Title: Re:Problem with Message Queue
Post by: The-FooL on June 02, 2004, 02:52 PM
Your error is coming from the fact that your not checking to see if the que is full.  If it is full, then QNum will exceed the upper limit of the array, and the error occurs.
Title: Re:Problem with Message Queue
Post by: Dyndrilliac on June 02, 2004, 03:25 PM
Yea, I realized that and for the handler I just told it to clear the queue and try to add the message again.

Craz3d: Probably, I wasn't really thinking about that though.

Edit: Queue(UBound(Queue)) = Message
ReDim Preserve Queue(UBound(Queue) + 1)
Caused an infinite loop to take place making Visual Basic 6 to eat up 100% System Resources, Lol.
Title: Re:Problem with Message Queue
Post by: CrAz3D on June 02, 2004, 05:20 PM
How did that cause a loop?
Title: Re:Problem with Message Queue
Post by: Dyndrilliac on June 02, 2004, 05:55 PM
I don't know, but when I added a Message to the queue replacing my old code with what you posted, it went into an infinite loop and froze the bot.
Title: Re:Problem with Message Queue
Post by: CrAz3D on June 03, 2004, 11:30 AM
Quote from: Dyndrilliac on June 02, 2004, 05:55 PM
I don't know, but when I added a Message to the queue replacing my old code with what you posted, it went into an infinite loop and froze the bot.
That is really weird.
Title: Re:Problem with Message Queue
Post by: Lenny on June 03, 2004, 02:03 PM
I recommend you to not use Pause which uses the Sleep function.  This pauses the entire thread of your program and often causes jerky and unnatural response.  (Using Sleep alone pauses your entire thread)
Look into the SetTimer API.

IIRC, Pause can't be used within another loop.