OnPlayerChat

From NWN Lexicon
Jump to: navigation, search

OnPlayerChat

The script attached to this event fires when a player issues a text chat message on the talk, whisper, shout, party or dm channel. A private tell between two players DOES NOT trigger this event.


Trigger

A player issues a non-tell based chat message.


Function(s)

SetPCChatVolume() sets the last player chat volume before it gets sent to other players.

Remarks

There is no default Bioware script for this event, and therefore the default behavior for this event is to do nothing at all. All player chat will work normally.

Dialog spoken by NPCs never triggers OnPlayerChat.

Dialog spoken during the execution of a conversation by either PC or NPC does not trigger this event. In order to react to that dialog, the code should be added directly into the conversation scripts.

Known Bugs:

While it is not a bug, careless use of this event may cause the server to hang or crash. This script could be used to force one or more PCs to say something by using SpeakString or ActionSpeakString. Doing that will cause OnPlayerChat to fire again when the lines are spoken by the PC. This in turn might cause the script to force them to speak again which will fire OnPlayerChat which makes them speak, which fires OnPlayerChat, etc.
For this reason, you should either refrain from forcing PCs to talk from here, or be very vigilant about filtering which specific chat phrases will force PCs to speak, so that this type of endless loop can be avoided. This is particularly important when SpeakString is being employed (rather than ActionSpeakString) because SpeakString will make the PC speak during script execution and that could potentially hang or crash the server or cause TMI errors.

Version

1.69

Example

// In this example script, the OnPlayerChat event is being used to support
// chat-based emotes. Players can type pre-determined commands into their chat
// window to get thier avatar to perform emotes instead of using their radial menu.
// This technique could be used to provide players with emote animations or even
// sequences of animations not normally available from their radial menu in addition
// to the ones that are there.
// In this example two commands are implemented: sitting on the ground, and
// worshipping. The pre-determined commands coded to be recognized for these are
// "*emote sit" and "*emote worship". To give further indication that the command
// was understood, and to prevent every other player in earshot from hearing the
// PC's emote command, the script echos the command back to the PC in his message
// window and lowers the volume of the echoed message so that only he will hear it.
// The example does not care what volume the chat was spoken at, but if it was
// desirable to treat different volumes of chat differently, the GetPCChatVolume
// function would be employed to make that determination.
 
 
<br />// OnPlayerChat script.
void main()
{
  // First determine which PC's chat triggered the event.
  // If the PC is not valid, perhaps his computer crashed, do nothing more.
  object oPC = GetPCChatSpeaker();
  if(!GetIsPC(oPC))
  {   return;   }
 
  // Next, determine what was spoken, and change it to all lower case to make
  // parsing through it easier and to have the commands be case insensitive. We
  // also trim all leading spaces from the front because we want to ignore them.
  string sCommand = GetStringLowerCase(GetPCChatMessage());
  while((sCommand != "") &amp;&amp; (GetStringLeft(sCommand, 1) == " "))
  {   sCommand = GetStringRight(sCommand, GetStringLength(sCommand) -1);   }
 
  // If the first seven letters of the chat spoken is not our pre-determined command
  // keyword "*emote ", then ignore the chat completely and do nothing more.
  // In this case, the chat will go out normally to everybody that can hear it just
  // like it would if no special OnPlayerChat script was being used at all.
  if(GetStringLeft(sCommand, 7) != "*emote ")
  {   return;   }
 
  // Next, strip off the "*emote " keyword from the front of the command and trim
  // all leading and trailing spaces present in what's left over. This lets the
  // players be a little less accurate with the spacing of their chat command and
  // still allows our script to recognize it as a valid emote request. What we end
  // up with is just the specific emote command requested by the player.
  string sEmote = GetStringRight(sCommand, GetStringLength(sCommand) -7);
  while((sEmote != "") &amp;&amp; (GetStringLeft(sEmote, 1) == " "))
  {   sEmote = GetStringRight(sEmote, GetStringLength(sEmote) -1);   }
 
  while((sEmote != "") &amp;&amp; (GetStringRight( sEmote, 1) == " "))
  {   sEmote = GetStringLeft(sEmote, GetStringLength(sEmote) -1);   }
 
  // Now we check the emote requested to see if it is one we provide for them, and
  // if so, set a variable to keep track of which animation sequence to play for the
  // emote requested. Should the requested emote not be something we support, simply
  // ignore it and let the chat go out like normal.
  int iAnimation = -1;
  if(sEmote == "sit")
  {   iAnimation = ANIMATION_LOOPING_SIT_CROSS;   }
  else if(sEmote == "worship")
  {   iAnimation = ANIMATION_LOOPING_WORSHIP;   }
  else
  {   return;   }  // Unsupported emote command, ignore it.
 
  // Next step is to force them to actually perform the animation sequence for five
  // game rounds.
  float fDuration = RoundsToSeconds(5);
  AssignCommand(oPC, ClearAllActions());
  AssignCommand(oPC, ActionPlayAnimation(iAnimation, 1.0, fDuration));
 
  // Finally, change the volume of the chat to a tell. Only the player whose chat
  // fired the event will hear the command and it will show up green in his message
  // window to give a further clue that the command was recognized and processed. We
  // will also change the text of what he originally said a little bit (just to see
  // how that is done) by prepending the string "Emote Command Received: " onto
  // the front end of what he orignially typed. Note that the GetPCChatMessage is
  // called again rather than using the sCommand variable which already contains the
  // text he typed in because we don't want the lowercase version of it to echo
  // back. We want him to see exactly what he originally typed in.
  sCommand = "Emote Command Received: " + GetPCChatMessage();
  SetPCChatMessage(sCommand);       // Change the text of the chat.
  SetPCChatVolume(TALKVOLUME_TELL); // Make it a tell.
 
  // After the script ends, the (new) chat message will be sent by the engine in accordance with the current volume setting.
}

See Also

functions: 

SetPCChatVolume()


author: Mistress, editor: Axe Murderer, contributor: Cereborn