OnPlayerChat: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
m (Cleaned up the second example, made it work as described.)
Line 61: Line 61:
-- sets colors when player join
-- sets colors when player join
function onJoin ()
function onJoin ()
g_Red, g_Green, g_Blue = math.random (50, 255), math.random (50, 255), math.random (50, 255)
local g_Red, g_Green, g_Blue = math.random (50, 255), math.random (50, 255), math.random (50, 255)
        setPlayerNametagColor(source, g_Red, g_Green, g_Blue)
end
end
-- checks if player has sent a message
-- checks if player has sent a message
function onChat ( message, messageType )
function onChat ( message, messageType )
if messageType == 0 then
if messageType == 0 then
local root = getRootElement()
                cancelEvent()
outputChatBox ( getPlayerName ( source ) .. ": #E0D0B0" .. message, getRootElement(), g_Red, g_Green, g_Blue, true )
                local g_Red, g_Green, g_Blue = getPlayerNametagColor(source)
cancelEvent()
outputChatBox ( getPlayerName ( source ) .. ": #E0D0B0" .. message, root, g_Red, g_Green, g_Blue, true )
outputServerLog( "CHAT: " .. getPlayerName ( source ).. ": " .. message )
outputServerLog( "CHAT: " .. getPlayerName ( source ).. ": " .. message )
end
end
end
end
addEventHandler ( "onPlayerJoin", getRootElement(), onJoin)
--root is a constant variable for getRootElement() - so these events will all be attatched to the root element.
addEventHandler ( "onPlayerChat", getRootElement(), onChat )
addEventHandler ( "onPlayerJoin", root, onJoin)
addEventHandler ( "onPlayerChat", root, onChat )
</syntaxhighlight>
</syntaxhighlight>


{{See also/Server event|Player events}}
{{See also/Server event|Player events}}

Revision as of 21:34, 8 October 2011

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. Cancelling this event also means the chat will not appear in the server console or logs. If you want chat logging, you will have to add a call to outputServerLog - See the second example.

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 )

This example sets random color to every player who joins.

-- sets colors when player join
function onJoin ()
	local g_Red, g_Green, g_Blue = math.random (50, 255), math.random (50, 255), math.random (50, 255)
        setPlayerNametagColor(source, g_Red, g_Green, g_Blue)
end
-- checks if player has sent a message
function onChat ( message, messageType )
	if messageType == 0 then
                cancelEvent()
                local g_Red, g_Green, g_Blue = getPlayerNametagColor(source)
		outputChatBox ( getPlayerName ( source ) .. ": #E0D0B0" .. message, root, g_Red, g_Green, g_Blue, true )
		outputServerLog( "CHAT: " .. getPlayerName ( source ).. ": " .. message )
	end
end
--root is a constant variable for getRootElement() - so these events will all be attatched to the root element.
addEventHandler ( "onPlayerJoin", root, onJoin)
addEventHandler ( "onPlayerChat", root, onChat )

See Also

Player events


Event functions