OnConsole: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
(example -> comments)
Line 13: Line 13:
This example adds the /me command into the script.  For example, if a player called Bob types "me likes pie" in console, it will display "* Bob likes pie" in the chatbox.
This example adds the /me command into the script.  For example, if a player called Bob types "me likes pie" in console, it will display "* Bob likes pie" in the chatbox.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
function onConsole ( text )
addEventHandler ( "onConsole", getRootElement(), "onConsole" ) -- add an event handler for onConsole
if ( getElementType ( source ) == "player" ) then
function onConsole ( text ) --when a player types in the console
    command = gettok ( text, 1, 32 )  
if ( getElementType ( source ) == "player" ) then --if the player
if ( command == "me" ) then
    command = gettok ( text, 1, 32 ) --split the command and get the first piece of text
local playername = getClientName ( source )
if ( command == "me" ) then --if the first piece of text was "me" then
local textage = string.gsub ( text, "me", "", 1 )
local playername = getClientName ( source )
outputChatBox ( "* " .. playername .. "" .. textage, root, 255, 255, 0 )
local textage = string.gsub ( text, "me", "", 1 )
outputChatBox ( "* " .. playername .. "" .. textage, root, 255, 255, 0 ) --announce the me command into the chatbox
end
end
end
end

Revision as of 00:07, 18 November 2006

This event is triggered when a player types any message into the console

Syntax

void onConsole ( string message )          

Variables

  • message: A string representing the message typed into the console

Example

This example adds the /me command into the script. For example, if a player called Bob types "me likes pie" in console, it will display "* Bob likes pie" in the chatbox.

addEventHandler ( "onConsole", getRootElement(), "onConsole" ) -- add an event handler for onConsole
function onConsole ( text ) --when a player types in the console
	if ( getElementType ( source ) == "player" ) then --if the player
	    command = gettok ( text, 1, 32 ) --split the command and get the first piece of text
		if ( command == "me" ) then --if the first piece of text was "me" then
			local playername = getClientName ( source )
			local textage = string.gsub ( text, "me", "", 1 )
			outputChatBox ( "* " .. playername .. "" .. textage, root, 255, 255, 0 ) --announce the me command into the chatbox
		end
	end
end

See Also

Split