OnConsole: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
mNo edit summary
No edit summary
Line 7: Line 7:
</syntaxhighlight>  
</syntaxhighlight>  


==Variables==
==Parameters==
* The source of this event refers to the player who typed into the console
* The '''source''' of this event refers to the player who typed into the console.
*'''message''': A string representing the message typed into the console
*'''message''': a string representing the message typed into the 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 ''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.
:'''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(), "input_Console" ) -- add an event handler for onConsole
addEventHandler ( "onConsole", getRootElement(), "input_Console" ) -- add an event handler for onConsole
function input_Console ( text ) --when a player types in the console
function input_Console ( text ) --when a player types in the console
if ( getElementType ( source ) == "player" ) then --if the player
-- if it's an ingame player,
    command = gettok ( text, 1, 32 ) --split the command and get the first piece of text
if ( getElementType ( source ) == "player" ) then
if ( command == "me" ) then --if the first piece of text was "me" then
--split the command by spaces (ASCII 32) and get the first piece of text
local playername = getClientName ( source )
local command = gettok ( text, 1, 32 )
local textage = string.gsub ( text, "me", "", 1 )
--if the first piece of text was "me",
outputChatBox ( "* " .. playername .. " " .. textage, root, 255, 255, 0 ) --announce the me command into the chatbox
if ( command == "me" ) then
--get the player's name
local playerName = getClientName ( source )
-- get the action text by substracting the first three characters ("me ")
local actionText = string.sub ( text, 3 )
-- announce the me command into the chatbox
outputChatBox ( "* " .. playerName .. " " .. actionText, getRootElement(), 255, 255, 0 )
end
end
end
end
Line 29: Line 36:
==See Also==
==See Also==
{{Event_functions}}
{{Event_functions}}
* [[Gettok]]
* [[Split]]

Revision as of 18:56, 22 April 2007

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

Syntax

void onConsole ( string message )          

Parameters

  • The source of this event refers to the player who typed into the console.
  • 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.

NOTE: this script is for example purposes only. This can be done in a more efficient way with addCommandHandler.
addEventHandler ( "onConsole", getRootElement(), "input_Console" ) -- add an event handler for onConsole
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 "me",
		if ( command == "me" ) then
			--get the player's name
			local playerName = getClientName ( source )
			-- get the action text by substracting the first three characters ("me ")
			local actionText = string.sub ( text, 3 )
			-- announce the me command into the chatbox
			outputChatBox ( "* " .. playerName .. " " .. actionText, getRootElement(), 255, 255, 0 )
		end
	end
end

See Also

Shared