Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

[EO v2] Help with SayMsg


zdearborn
 Share

Recommended Posts

Hello!

Today I've begun (trying) to write a NPC chat system which works based off keywords. The server is reacting to the keywords correctly, but there's one problem, the NPC text shows up before the player's does, which creates a problem: the npc is not a mind reader and cannot respond before the actual player msg! :P

Here is the code for my NPC processing the keywords:
```
Public Sub processNpcConversation(ByVal Index As Long, ByVal npcNum As Long, ByVal mapNum As Long, ByVal playerMsg As String, ByVal justGreeted As Boolean)
    Dim distanceX As Long, distanceY As Long, allowedRange As Long, validConvo As Boolean
    Dim playerName As String
    ' the range at which the player can talk to the npc
    allowedRange = 4

    distanceX = GetPlayerX(Index) - MapNpc(mapNum).Npc(npcNum).x
    distanceY = GetPlayerY(Index) - MapNpc(mapNum).Npc(npcNum).y

    validConvo = True
    ' checks ;p
    If Not IsPlaying(Index) Then validConvo = False
    If Not GetPlayerMap(Index) = mapNum Then validConvo = False
    If distanceX > allowedRange Or distanceX < -allowedRange Then validConvo = False
    If distanceY > allowedRange Or distanceY < -allowedRange Then validConvo = False

    If validConvo = False Then
        Player(Index).inNpcConvo = False
        Exit Sub
    End If

    ' convenience
    playerName = Trim$(Player(Index).Name)
    playerMsg = Trim$(LCase$(playerMsg))
    ' npc vars
    Dim npcGreeting As String, npcBye As String
    ' messy below... hardcoded npc interaction!!
    Select Case npcNum
        Case 2 ' test npc
            npcGreeting = "Hello, " & playerName & ", welcome to The Barracks!"
            npcBye = "Bye, then.."
            If justGreeted = True Then
                Call SayMsg_NPCChat(Index, npcGreeting, QBColor(White), 1, npcNum)
            End If
            Select Case playerMsg
                Case "barracks"
                    Call SayMsg_NPCChat(Index, "The barracks is where beginners like yourself train before going out to fight for our beloved King.", QBColor(White), 1, npcNum)
                Case "bye"
                    Call SayMsg_NPCChat(Index, npcBye, QBColor(White), 1, npcNum)
                    Player(Index).inNpcConvo = False
                    Exit Sub
            End Select
    End Select
End Sub

```
and heres sub saymsg_npcchat
```
Sub SayMsg_NPCChat(ByVal Index As Long, ByVal message As String, ByVal saycolour As Long, ByVal msgType As Long, ByVal npcNum As Long)
    Dim Buffer As clsBuffer

    Set Buffer = New clsBuffer

    Select Case msgType
        Case NPC_CHAT_PLAYERMSG ' player to npc
            Buffer.WriteLong SSayMsg
            Buffer.WriteString GetPlayerName(Index)
            Buffer.WriteLong GetPlayerAccess(Index)
            Buffer.WriteLong GetPlayerPK(Index)
            Buffer.WriteString message
            Buffer.WriteString "[NPC] "
            Buffer.WriteLong saycolour
            Buffer.WriteLong 3

            ' npc chat
            If Player(Index).inNpcConvo = True Then
                Call processNpcConversation(Index, npcNum, GetPlayerMap(Index), message, False)
            ElseIf Player(Index).inNpcConvo = False Then
                Call instantiateNpcConversation(Index, npcNum, GetPlayerMap(Index), message)
            End If

        Case NPC_CHAT_NPCMSG ' npc to player
            Dim npcName As String
            npcName = Trim$(Npc(npcNum).Name)

            Buffer.WriteLong SSayMsg
            Buffer.WriteString npcName
            Buffer.WriteLong 5
            Buffer.WriteLong 0
            Buffer.WriteString message
            Buffer.WriteString "[NPC] "
            Buffer.WriteLong saycolour
            Buffer.WriteLong 3

        Case Else
            Set Buffer = Nothing
            Exit Sub
    End Select

    SendDataTo Index, Buffer.ToArray()

    Set Buffer = Nothing
End Sub

```(the added long that at the end is sent for designating which chat channel it goes in, if anyone is confused by that)

Now I'm sure I have overlooked something, but I have tried rearranging some code, nothing seems to fix this.

To reiterate, the ultimate goal: have the npc text appear AFTER the player's.

The client side I'm sure is ok, because it's just a modified version of the original handlesaymsg, but if you need any more code, please just ask and I will post it asap.

Here's a screen:
![](http://img15.imageshack.us/img15/8850/97156657.png)
as you can see all responses are displayed before the player msg is

the answer is probably so simple I'll feel stupid.. wait, I do feel stupid

Thanks for any help!
Link to comment
Share on other sites

Hm a bit confused about the question, a little thick I guess :P, but I will try to answer, sorry if its way off
```
        Case 3 ' npc chat
            ' make sure player has valid npc targeted
            If npcNum > 0 And Npc(npcNum).Behaviour = NPC_BEHAVIOUR_SHOPKEEPER Then
                Call SayMsg_NPCChat(Index, Msg, QBColor(White), 0, npcNum)
            End If

```
is the HandleSayMsg check…
Link to comment
Share on other sites

Ooph, my bad. I was being stoopid here. You should send out the player message first.. Then the NPC message. But to be honest I can't see where you're sending the player messages out in those subs..

EDIT : Sorry for the idiocy and wacky writing, I'm exhausted ;P

Anyway, in pseudocode the order should be like:

->Receive message?
  |
  |–> Yes? -----------------> Send to Client ------------> Process NPC speech
Link to comment
Share on other sites

hey its no problem, I appreciate your help!

but, in saymsg:
```
        Case NPC_CHAT_PLAYERMSG ' player to npc
            Buffer.WriteLong SSayMsg
            Buffer.WriteString GetPlayerName(Index)
            Buffer.WriteLong GetPlayerAccess(Index)
            Buffer.WriteLong GetPlayerPK(Index)
            Buffer.WriteString message
            Buffer.WriteString "[NPC] "
            Buffer.WriteLong saycolour
            Buffer.WriteLong 3

```(player's msg)
is sent before the npc chat subs are called:

```
            ' npc chat
            If Player(Index).inNpcConvo = True Then
                Call processNpcConversation(Index, npcNum, GetPlayerMap(Index), message, False)
            ElseIf Player(Index).inNpcConvo = False Then
                Call instantiateNpcConversation(Index, npcNum, GetPlayerMap(Index), message)
            End If

```
Link to comment
Share on other sites

There's no SendData command after your playermessage, and thus it's being sent along with the NPC chat.. Meaning the client can receive them at the exact same time, and mess up the order. Might want to get rid of that SendData outside the case selection, and add one after every time you filled your buffer for it to be sent.

```
Sub SayMsg_NPCChat(ByVal Index As Long, ByVal message As String, ByVal saycolour As Long, ByVal msgType As Long, ByVal npcNum As Long)
    Dim Buffer As clsBuffer

    Set Buffer = New clsBuffer

    Select Case msgType
        Case NPC_CHAT_PLAYERMSG ' player to npc
            Buffer.WriteLong SSayMsg
            Buffer.WriteString GetPlayerName(Index)
            Buffer.WriteLong GetPlayerAccess(Index)
            Buffer.WriteLong GetPlayerPK(Index)
            Buffer.WriteString message
            Buffer.WriteString "[NPC] "
            Buffer.WriteLong saycolour
            Buffer.WriteLong 3

            SendDataTo Index, Buffer.ToArray()
            Set Buffer = Nothing

            ' npc chat
            If Player(Index).inNpcConvo = True Then
                Call processNpcConversation(Index, npcNum, GetPlayerMap(Index), message, False)
            ElseIf Player(Index).inNpcConvo = False Then
                Call instantiateNpcConversation(Index, npcNum, GetPlayerMap(Index), message)
            End If

        Case NPC_CHAT_NPCMSG ' npc to player
            Dim npcName As String
            npcName = Trim$(Npc(npcNum).Name)

            Buffer.WriteLong SSayMsg
            Buffer.WriteString npcName
            Buffer.WriteLong 5
            Buffer.WriteLong 0
            Buffer.WriteString message
            Buffer.WriteString "[NPC] "
            Buffer.WriteLong saycolour
            Buffer.WriteLong 3

            SendDataTo Index, Buffer.ToArray()
            Set Buffer = Nothing

        Case Else
            Set Buffer = Nothing
            Exit Sub
    End Select
End Sub

```
Making a wild guess here though, but it's worth a shot.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...