User:Jbeta/EventPageExample: Difference between revisions
Jump to navigation
Jump to search
(New page: __NOTOC__ <!-- replace with a template! --> <pageclass class="server" subcaption="Serverside event"></pageclass> <lowercasetitle></lowercasetitle> This event is triggered when a player cha...) |
No edit summary |
||
Line 5: | Line 5: | ||
This event is triggered when a player chats inside the chat box. | This event is triggered when a player chats inside the chat box. | ||
== | ==Parameters== | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
string message, int messageType | |||
</syntaxhighlight> | </syntaxhighlight> | ||
*'''message''': A string representing the message typed into the chat. | *'''message''': A string representing the message typed into the chat. | ||
*''' | *'''messageType''': An integer value representing the message type: | ||
**'''0''': normal message | **'''0''': normal message | ||
**'''1''': action message (/me) | **'''1''': action message (/me) | ||
**'''2''': team message | **'''2''': team message | ||
=== | ==Source== | ||
The [[event system#Event source|source]] of this event is the [[player]] who sent the chatbox message. | |||
==Cancel effect== | |||
If this event is [[Event system#Canceling|canceled]], the game's chat system won't deliver the posts. You may use [[outputChatBox]] to send the messages then. | If this event is [[Event system#Canceling|canceled]], the game's chat system won't deliver the posts. You may use [[outputChatBox]] to send the messages then. | ||
==Example== | ==Example== | ||
This example limits receiving of chat messages to a spherical area around the player who sent the message. | This example limits receiving of chat messages to a spherical area around the player who sent the message, also blocking action and team text. | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
-- define a handler function that cancels the event so that the message won't be delivered | |||
function blockChatMessage() | function blockChatMessage() | ||
cancelEvent() | cancelEvent() | ||
end | end | ||
-- attach it as a handler to onPlayerChat | |||
addEventHandler( "onPlayerChat", getRootElement(), blockChatMessage ) | addEventHandler( "onPlayerChat", getRootElement(), blockChatMessage ) | ||
local chatRadius = | -- define our chat radius | ||
local chatRadius = 20 --units | |||
-- define another handler that will distribute the message to all nearby players | |||
function sendMessageToNearbyPlayers( message, messageType ) | function sendMessageToNearbyPlayers( message, messageType ) | ||
-- we will only send normal chat messages, action and team types will be ignored | |||
if messageType == 0 then | if messageType == 0 then | ||
-- get the chatting player's position | |||
local posX, posY, posZ = getElementPosition( source ) | local posX, posY, posZ = getElementPosition( source ) | ||
-- create a sphere of the specified radius in that position | |||
local chatSphere = createColSphere( posX, posY, posZ, chatRadius ) | local chatSphere = createColSphere( posX, posY, posZ, chatRadius ) | ||
-- get a table all player elements inside it | |||
local nearbyPlayers = getElementsWithinColShape( chatSphere, "player" ) | local nearbyPlayers = getElementsWithinColShape( chatSphere, "player" ) | ||
-- and destroy the sphere, since we're done with it | |||
destroyElement( chatSphere ) | destroyElement( chatSphere ) | ||
-- deliver the message to each player in that table | |||
for index, nearbyPlayer in ipairs( nearbyPlayers ) do | for index, nearbyPlayer in ipairs( nearbyPlayers ) do | ||
outputChatBox( message, nearbyPlayer ) | outputChatBox( message, nearbyPlayer ) | ||
Line 46: | Line 55: | ||
end | end | ||
end | end | ||
-- attach our new chat handler to onPlayerChat | |||
addEventHandler( "onPlayerChat", getRootElement(), sendMessageToNearbyPlayers ) | addEventHandler( "onPlayerChat", getRootElement(), sendMessageToNearbyPlayers ) | ||
</syntaxhighlight> | </syntaxhighlight> |
Latest revision as of 18:45, 13 September 2007
This event is triggered when a player chats inside the chat box.
Parameters
string message, int messageType
- message: A string representing the message typed into the chat.
- messageType: An integer value representing the message type:
- 0: normal message
- 1: action message (/me)
- 2: team message
Source
The source of this event is the player who sent the chatbox message.
Cancel effect
If this event is canceled, the game's chat system won't deliver the posts. You may use outputChatBox to send the messages then.
Example
This example limits receiving of chat messages to a spherical area around the player who sent the message, also blocking action and team text.
-- define a handler function that cancels the event so that the message won't be delivered function blockChatMessage() cancelEvent() end -- attach it as a handler to onPlayerChat addEventHandler( "onPlayerChat", getRootElement(), blockChatMessage ) -- define our chat radius local chatRadius = 20 --units -- define another handler that will distribute the message to all nearby players function sendMessageToNearbyPlayers( message, messageType ) -- we will only send normal chat messages, action and team types will be ignored if messageType == 0 then -- get the chatting player's position local posX, posY, posZ = getElementPosition( source ) -- create a sphere of the specified radius in that position local chatSphere = createColSphere( posX, posY, posZ, chatRadius ) -- get a table all player elements inside it local nearbyPlayers = getElementsWithinColShape( chatSphere, "player" ) -- and destroy the sphere, since we're done with it destroyElement( chatSphere ) -- deliver the message to each player in that table for index, nearbyPlayer in ipairs( nearbyPlayers ) do outputChatBox( message, nearbyPlayer ) end end end -- attach our new chat handler to onPlayerChat addEventHandler( "onPlayerChat", getRootElement(), sendMessageToNearbyPlayers )
See Also
- addEvent
- addEventHandler
- cancelEvent
- cancelLatentEvent
- getEventHandlers
- getLatentEventHandles
- getLatentEventStatus
- removeEventHandler
- triggerEvent
- wasEventCancelled