GetEventHandlers: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
Line 17: | Line 17: | ||
===Returns=== | ===Returns=== | ||
Returns table with attached functions, false otherwise. | Returns table with attached functions, false otherwise. | ||
===Example=== | |||
<section name="Shared" class="Shared" show="true"> | |||
<syntaxhighlight lang="lua">[Lua] | |||
function isAddEventHandlerAdded( sEventName, pElementAttachedTo, func ) | |||
if | |||
type( sEventName ) == 'string' and | |||
isElement( pElementAttachedTo ) and | |||
type( func ) == 'function' | |||
then | |||
local aAttachedFunctions = getEventHandlers( sEventName, pElementAttachedTo ) | |||
if type( aAttachedFunctions ) == 'table' and #aAttachedFunctions > 0 then | |||
for i, v in ipairs( aAttachedFunctions ) do | |||
if v == func then | |||
return true | |||
end | |||
end | |||
return false | |||
end | |||
return false | |||
end | |||
return false | |||
end | |||
function onPlayerWasted() | |||
outputChatBox( getPlayerName( source ) .. ' died.' ) | |||
end | |||
addEventHandler( 'onPlayerWasted', root, onPlayerWasted ) | |||
addCommandHandler( 'removeOnPlayerWastedEvent', | |||
function() | |||
if isAddEventHandlerAdded( 'onPlayerWasted', root, onPlayerWasted ) then | |||
outputChatBox( 'onPlayerWasted succesfully removed!' ) | |||
removeEventHandler( 'onPlayerWasted', root, onPlayerWasted ) | |||
end | |||
end | |||
) | |||
</syntaxhighlight> | |||
{{Event functions}} | {{Event functions}} | ||
[[Category:Needs Example]] | [[Category:Needs Example]] |
Revision as of 08:52, 17 February 2013
Syntax
table getEventHandlers ( string eventName, element attachedTo )
Required Arguments
- eventName: The name of the event. For example ( "onPlayerWasted" ).
- attachedTo: The element attached to.
Returns
Returns table with attached functions, false otherwise.
Example
<section name="Shared" class="Shared" show="true">
[Lua] function isAddEventHandlerAdded( sEventName, pElementAttachedTo, func ) if type( sEventName ) == 'string' and isElement( pElementAttachedTo ) and type( func ) == 'function' then local aAttachedFunctions = getEventHandlers( sEventName, pElementAttachedTo ) if type( aAttachedFunctions ) == 'table' and #aAttachedFunctions > 0 then for i, v in ipairs( aAttachedFunctions ) do if v == func then return true end end return false end return false end return false end function onPlayerWasted() outputChatBox( getPlayerName( source ) .. ' died.' ) end addEventHandler( 'onPlayerWasted', root, onPlayerWasted ) addCommandHandler( 'removeOnPlayerWastedEvent', function() if isAddEventHandlerAdded( 'onPlayerWasted', root, onPlayerWasted ) then outputChatBox( 'onPlayerWasted succesfully removed!' ) removeEventHandler( 'onPlayerWasted', root, onPlayerWasted ) end end )