OnConsole: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
mNo edit summary
No edit summary
 
(19 intermediate revisions by 12 users not shown)
Line 1: Line 1:
__NOTOC__  
__NOTOC__  
This event is triggered when a player types a message into the console.
{{Server event}}
This event is triggered when a player types a message into his console. It is also triggered when entering '/' commands via the chatbox.
{{Note|The event will not be triggered if the message can be processed by an existing command handler}}


==Syntax==  
==Parameters==
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
void onConsole ( string message )         
string theMessage
</syntaxhighlight>  
</syntaxhighlight>  


==Variables==
*'''theMessage''': a [[string]] representing the message entered into the console.
*'''message''': A string representing the message typed into the console
 
==Source==
The [[event system#Event source|source]] of this event is the [[player]] that entered the message in the console. This can be a player or the server console.


==Example==  
==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.
This example adds the ''yo'' command into the script. For example, if a player called Bob types "yo likes pie" in console, it will display "* Bob likes pie" in the chatbox.
:'''NOTE:''' this script is for example purposes only. This can be done in a more efficient way with [[addCommandHandler]].
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
addEventHandler ( "onConsole", getRootElement(), "onConsole" ) -- add an event handler for onConsole
function input_Console ( text ) --when a player types in the console
function onConsole ( text ) --when a player types in the console
-- if it's an ingame player,
if ( getElementType ( source ) == "player" ) then --if the player
if ( getElementType ( source ) == "player" ) then
    command = gettok ( text, 1, 32 ) --split the command and get the first piece of text
--split the command by spaces (ASCII 32) and get the first piece of text
if ( command == "me" ) then --if the first piece of text was "me" then
local command = gettok ( text, 1, 32 )
local playername = getClientName ( source )
--if the first piece of text was "yo",
local textage = string.gsub ( text, "me", "", 1 )
if ( command == "yo" ) then
outputChatBox ( "* " .. playername .. " " .. textage, root, 255, 255, 0 ) --announce the me command into the chatbox
--get the player's name
local playerName = getPlayerName ( source )
-- get the action text by substracting the first three characters ("yo ")
local actionText = string.sub ( text, 3 )
-- announce the yo command into the chatbox
outputChatBox ( "* " .. playerName .. " " .. actionText, getRootElement(), 255, 255, 0 )
end
end
end
end
end
end
addEventHandler ( "onConsole", getRootElement(), input_Console ) -- add an event handler for onConsole
</syntaxhighlight>
</syntaxhighlight>


==See Also==
{{See also/Server event|Client events}}
[[Split]]
 
[[ru:onConsole]]

Latest revision as of 02:55, 27 September 2018

This event is triggered when a player types a message into his console. It is also triggered when entering '/' commands via the chatbox.

[[{{{image}}}|link=|]] Note: The event will not be triggered if the message can be processed by an existing command handler

Parameters

string theMessage
  • theMessage: a string representing the message entered into the console.

Source

The source of this event is the player that entered the message in the console. This can be a player or the server console.

Example

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

NOTE: this script is for example purposes only. This can be done in a more efficient way with addCommandHandler.
function input_Console ( text ) --when a player types in the console
	-- if it's an ingame player,
	if ( getElementType ( source ) == "player" ) then
		--split the command by spaces (ASCII 32) and get the first piece of text
		local command = gettok ( text, 1, 32 )
		--if the first piece of text was "yo",
		if ( command == "yo" ) then
			--get the player's name
			local playerName = getPlayerName ( source )
			-- get the action text by substracting the first three characters ("yo ")
			local actionText = string.sub ( text, 3 )
			-- announce the yo command into the chatbox
			outputChatBox ( "* " .. playerName .. " " .. actionText, getRootElement(), 255, 255, 0 )
		end
	end
end
addEventHandler ( "onConsole", getRootElement(), input_Console ) -- add an event handler for onConsole

See Also

Client events


Event functions