AddEventHandler: Difference between revisions
Jump to navigation
Jump to search
mNo edit summary |
(Added template, sections, minor corrections) |
||
Line 1: | Line 1: | ||
__NOTOC__ | __NOTOC__ | ||
{{Server client function}} | |||
{{Note_box|It is strongly advised that you do not use the same name for your handler function as the event name, as this can lead to confusion if multiple handler functions are used.}} | {{Note_box|It is strongly advised that you do not use the same name for your handler function as the event name, as this can lead to confusion if multiple handler functions are used.}} | ||
This function will add an [[event]] handler. An event handler is a function that will be called when | This function will add an [[event]] handler. An event handler is a function that will be called when the event it's attached to is triggered. See [[event system]] for more information on how the event system works. | ||
==Syntax== | ==Syntax== | ||
<section name="Server and Client" class="both" show="true"> | |||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
bool addEventHandler ( string eventName, element attachedTo, function handlerFunction ) | bool addEventHandler ( string eventName, element attachedTo, function handlerFunction ) | ||
Line 11: | Line 13: | ||
*'''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. 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). | *'''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). | ||
*'''handlerFunction:''' The handler function you wish to call. This function | *'''handlerFunction:''' The handler function you wish to call when the event is triggered. This function will be passed all of the event's parameters as arguments, 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 | Returns ''true'' if the event handler was attached successfully. Returns ''false'' if the specified event could not be found or any parameters were invalid. | ||
</section> | |||
==Example== | ==Example== | ||
This example sends a message to everyone in the server when a player spawns. | <section name="Example" class="server" show="true"> | ||
This serverside example sends a message to everyone in the server when a player spawns. | |||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
-- | -- get the root element | ||
rootElement = getRootElement () | rootElement = getRootElement() | ||
-- | -- define our handler function | ||
function onSpawnpointUseHandler(thePlayer) | function onSpawnpointUseHandler ( thePlayer ) | ||
-- | -- get the player's name | ||
local playerName = getClientName( thePlayer ) | local playerName = getClientName( thePlayer ) | ||
-- | -- output in the chat box that they've spawned | ||
outputChatBox ( playerName .. "has spawned!" ) | outputChatBox ( playerName .. " has spawned!" ) | ||
end | end | ||
-- | -- add the defined onSpawnpointUseHandler function as a handler to the onSpawnPointUse event (triggered when any player spawns at a spawnpoint) | ||
addEventHandler ( "onSpawnpointUse", rootElement, onSpawnpointUseHandler ) | addEventHandler( "onSpawnpointUse", rootElement, onSpawnpointUseHandler ) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
</section> | |||
==See Also== | ==See Also== | ||
{{Event_functions}} | {{Event_functions}} |
Revision as of 21:52, 12 August 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 the event it's attached to is triggered. See event system for more information on how the event system works.
Syntax
Click to collapse [-]
Server and Clientbool addEventHandler ( string eventName, element attachedTo, function handlerFunction )
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).
- handlerFunction: The handler function you wish to call when the event is triggered. This function will be passed all of the event's parameters as arguments, 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 specified event could not be found or any parameters were invalid.
Example
Click to collapse [-]
ExampleThis serverside example sends a message to everyone in the server when a player spawns.
-- get the root element rootElement = getRootElement() -- 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 -- add the defined onSpawnpointUseHandler function as a handler to the onSpawnPointUse event (triggered when any player spawns at a spawnpoint) addEventHandler( "onSpawnpointUse", rootElement, onSpawnpointUseHandler )
See Also
- addEvent
- addEventHandler
- cancelEvent
- cancelLatentEvent
- getEventHandlers
- getLatentEventHandles
- getLatentEventStatus
- removeEventHandler
- triggerEvent
- wasEventCancelled