AddEventHandler: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 9: Line 9:


===Required Arguments===  
===Required Arguments===  
*'''eventName:''' The name of the event you want to attach the handler function to
*'''eventName:''' The name of the [[event]] you want to attach the handler function to.
*'''attachedTo:''' The [[element]] you wish to attach the handler to. Often this can be the root element.
*'''attachedTo:''' The [[element]] you wish to attach the handler to. The handler will only be called when the event it is attached to is triggered for this element, or one of its children. Often, this can be the root element (meaning the handler will be called when the event is triggered for ''any'' element).
*'''functionName:''' The name of the function you wish to call. This function should have the correct number of parameters defined for the event you are using.
*'''functionName:''' The name of the handler function you wish to call. This function should have the correct number of parameters defined for the event you are using, but it isn't required that it takes all of them.


===Returns===
===Returns===
Returns ''true'' if the event handler was attached successfully. Returns ''false'' if the event specified could not be found or the ''attachedTo'' [[element]] specified is invalid.
Returns ''true'' if the event handler was attached successfully. Returns ''false'' if the event specified could not be found or the ''attachedTo'' [[element]] specified is invalid.


==Example==  
==Example==
This example sends a message to everyone in the server when a player spawns.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
-- Get the root map element
-- Get the root element
rootElement = getRootElement ()
rootElement = getRootElement ()
-- Add an event handler to the onSpawnPointUse event (triggered when a player spawns)
-- Add an event handler to the onSpawnPointUse event (triggered when a player spawns at a spawnpoint)
addEventHandler ( "onSpawnpointUse", rootElement, "spawn" )
addEventHandler ( "onSpawnpointUse", rootElement, "onSpawnpointUseHandler" )
-- Define our handler function
-- Define our handler function
function spawn( thePlayer )
function onSpawnpointUseHandler( thePlayer )
-- Tell them in the chat box that they've spawned
-- Get the player's name
outputChatBox ( "You've spawned!", thePlayer )
local playerName = getClientName( thePlayer )
-- Output in the chat box that they've spawned
outputChatBox ( playerName .. "has spawned!" )
end
end
</syntaxhighlight>
</syntaxhighlight>

Revision as of 12:12, 3 June 2007

This template is no longer in use as it results in poor readability. This function will add an event handler. An event handler is a function that will be called when an event is triggered. See event system for more information on how the event system works.

Syntax

bool addEventHandler ( string eventName, element attachedTo, string functionName )    

Required Arguments

  • eventName: The name of the event you want to attach the handler function to.
  • attachedTo: The element you wish to attach the handler to. The handler will only be called when the event it is attached to is triggered for this element, or one of its children. Often, this can be the root element (meaning the handler will be called when the event is triggered for any element).
  • functionName: The name of the handler function you wish to call. This function should have the correct number of parameters defined for the event you are using, but it isn't required that it takes all of them.

Returns

Returns true if the event handler was attached successfully. Returns false if the event specified could not be found or the attachedTo element specified is invalid.

Example

This example sends a message to everyone in the server when a player spawns.

-- Get the root element
rootElement = getRootElement ()
-- Add an event handler to the onSpawnPointUse event (triggered when a player spawns at a spawnpoint)
addEventHandler ( "onSpawnpointUse", rootElement, "onSpawnpointUseHandler" )
-- Define our handler function
function onSpawnpointUseHandler( thePlayer )
	-- Get the player's name
	local playerName = getClientName( thePlayer )
	-- Output in the chat box that they've spawned
	outputChatBox ( playerName .. "has spawned!" )
end

See Also