zdearborn Posted February 19, 2011 Author Share Posted February 19, 2011 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! :PHere 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 SelectEnd 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 = NothingEnd 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 isthe answer is probably so simple I'll feel stupid.. wait, I do feel stupidThanks for any help! Link to comment Share on other sites More sharing options...
Joyce Posted February 19, 2011 Share Posted February 19, 2011 Are you sure the NPC message check is going out before the player message gets passed on by the server? Link to comment Share on other sites More sharing options...
zdearborn Posted February 19, 2011 Author Share Posted February 19, 2011 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 More sharing options...
Joyce Posted February 19, 2011 Share Posted February 19, 2011 I mean are you sure you do your NPC thingy before the server sends the player's message back? Link to comment Share on other sites More sharing options...
zdearborn Posted February 19, 2011 Author Share Posted February 19, 2011 Hmpf, no it does not it will just automatically pass the player msg and then run the checks…so I should just send the player msg back in the processNpcChat sub then? Link to comment Share on other sites More sharing options...
Joyce Posted February 19, 2011 Share Posted February 19, 2011 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 ;PAnyway, in pseudocode the order should be like:->Receive message? | |–> Yes? -----------------> Send to Client ------------> Process NPC speech Link to comment Share on other sites More sharing options...
zdearborn Posted February 19, 2011 Author Share Posted February 19, 2011 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 More sharing options...
Joyce Posted February 19, 2011 Share Posted February 19, 2011 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 SelectEnd Sub```Making a wild guess here though, but it's worth a shot. Link to comment Share on other sites More sharing options...
zdearborn Posted February 19, 2011 Author Share Posted February 19, 2011 Hahaha that was such a simple overlook. :embarrassed:Thank you so much for the help! Link to comment Share on other sites More sharing options...
Joyce Posted February 19, 2011 Share Posted February 19, 2011 No problem, sorry for the vagueness.. Thinking clearly isn't as easy as people think when you haven't slept in ~4 days. ^^ Hope it works now though. Link to comment Share on other sites More sharing options...
zdearborn Posted February 19, 2011 Author Share Posted February 19, 2011 Yes, it works beautifully. 4 days? sleeping pills ftw Link to comment Share on other sites More sharing options...
Joyce Posted February 19, 2011 Share Posted February 19, 2011 Actually it was by choice, my parents won't let me sleep over at my friend's house this week, so I'm going on a sleeping strike. :P Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now