Receiving Commands

As in the Agent Demo program, the Command event is very important in the I’m Thinking of a Color program. All input from the user is received and processed in this routine, as shown in Listing 14.14.

Listing 14.14: Agent1_Command Event in I’m Thinking of a Color

Private Sub Agent1_Command(ByVal UserInput As Object)
Dim i As Integer
If UserInput.Name = “yes” Then
   YesFlag = True
ElseIf UserInput.Name = “stop” Then
   YesFlag = True
   TheHost.StopAll
   Player(1).StopAll
   Player(2).StopAll
   Unload Me
ElseIf UserInput.Name = “cheat” Then
   TheHost.Speak “The color is “ & Combo1.List(TheColor) & “.”
ElseIf UserInput.Name = Text2.Text Then
   TheHost.Speak “Hi “ & UserInput.Name & “.”
ElseIf UserInput.Name = “” Then
   TheHost.Play “Confused”
   TheHost.Speak “I’m not sure what you said, “ & Text2.Text & _      “. Please try again.”
   
Else
   AColor = 0
   Do While UserInput.Name <> Combo1.List(AColor)
      AColor = AColor + 1
   Loop
End If
End Sub

If the user’s command is easy to process, like the “stop” command, I process it immediately. Otherwise, I set a global variable indicating that the input was received. Then I can look for this value elsewhere in the program.

If I receive a “stop” command, I set the YesFlag to True and then issue a StopAll command for each of the characters. This ensures that all of the queued requests are purged from the Agent server. Then I can stop the program using an Unload Me statement.

Passing along a color is just a matter of scanning through the list of colors in the combo box for the same value, and then setting the global variable AColor with the relative position in the list. When I’m ready to process this input, I just look for the value in the global variable.

© 1998 SYBEX Inc. All rights reserved.