User:Jbeta/EventPageExample

From Multi Theft Auto: Wiki
Revision as of 17:13, 13 September 2007 by Jbeta (talk | contribs) (New page: __NOTOC__ <!-- replace with a template! --> <pageclass class="server" subcaption="Serverside event"></pageclass> <lowercasetitle></lowercasetitle> This event is triggered when a player cha...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

This event is triggered when a player chats inside the chat box.

Syntax

void onPlayerChat ( string message, int type )

Source

The source of this event is the player who sent the chatbox message.

Parameters

  • message: A string representing the message typed into the chat.
  • type: An integer value representing the message type:
    • 0: normal message
    • 1: action message (/me)
    • 2: team message

Canceling

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.

function blockChatMessage()
    cancelEvent()
end
addEventHandler( "onPlayerChat", getRootElement(), blockChatMessage )

local chatRadius = 2 --units

function sendMessageToNearbyPlayers( message, messageType )
    if messageType == 0 then
        local posX, posY, posZ = getElementPosition( source )
        
        local chatSphere = createColSphere( posX, posY, posZ, chatRadius )
        local nearbyPlayers = getElementsWithinColShape( chatSphere, "player" )
        destroyElement( chatSphere )
        
        for index, nearbyPlayer in ipairs( nearbyPlayers ) do
            outputChatBox( message, nearbyPlayer )
        end
    end
end
addEventHandler( "onPlayerChat", getRootElement(), sendMessageToNearbyPlayers )

See Also