OnPlayerChat: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
mNo edit summary
(Updated)
Line 1: Line 1:
[[Category:Incomplete Event]]
__NOTOC__
__NOTOC__
{{Server event}}
This event is triggered when a player chats inside the chat box.
This event is triggered when a player chats inside the chat box.


==Syntax==  
==Parameters==
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
void onPlayerChat ( string message, int type )         
string message, int messageType
</syntaxhighlight>  
</syntaxhighlight>  


==Variables==
*'''message''': A string representing the message typed into the chat.
*'''player''': A player element refering to the player who is chatting the message
*'''messageType''': An integer value representing the message type:
*'''message''': A string representing the message typed into the chat
**'''0''': normal message
*'''type''': An integer value representing the chat type:
**'''1''': action message (/me)
*''0'': normal
**'''2''': team message
*''1'': action (/me)
*''2'': team


===Canceling===
==Source==
If this event is [[Event system#Canceling|canceled]], the game's chatsystem won't deliver the posts. Use [[outputChatBox]] to send the messages then.
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.


==Example==  
==Example==  
This example adds a !createhydra command, which creates a hydra nearby
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">
addEventHandler ( "onPlayerChat ", getRootElement(), "onPlayerChat" ) -- add an event handler for onPlayerChat
-- define our chat radius
function onPlayerChat ( message, type )
local chatRadius = 20 --units
  if ( gettok ( message , 1, 32 ) == "!createhydra" ) then
 
    x, y, z = getElementPosition ( source )
-- define a handler that will distribute the message to all nearby players
    createVehicle ( 520, x + 5, y, z )
function sendMessageToNearbyPlayers( message, messageType )
     outputChatBox("You got a hydra",source)
    -- we will only send normal chat messages, action and team types will be ignored
  end
    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 )
 
-- define another handler function that cancels the event so that the message won't be delivered through the
function blockChatMessage()
    cancelEvent()
end
end
-- attach it as a handler to onPlayerChat
addEventHandler( "onPlayerChat", getRootElement(), blockChatMessage )
</syntaxhighlight>
</syntaxhighlight>


==See Also==
==See Also==
{{Event_functions}}
{{Event_functions}}
* [[Gettok]]
* [[Split]]

Revision as of 16:16, 14 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 our chat radius
local chatRadius = 20 --units

-- define a 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 )

-- define another handler function that cancels the event so that the message won't be delivered through the 
function blockChatMessage()
    cancelEvent()
end
-- attach it as a handler to onPlayerChat
addEventHandler( "onPlayerChat", getRootElement(), blockChatMessage )

See Also