IsEventHandlerAdded: Difference between revisions
Jump to navigation
Jump to search
mNo edit summary |
m (Return 'false' in some coditions, not 'nil') |
||
Line 20: | Line 20: | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
function isEventHandlerAdded( sEventName, pElementAttachedTo, func ) | function isEventHandlerAdded( 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 | end | ||
</syntaxhighlight> | </syntaxhighlight> |
Revision as of 01:47, 18 May 2020
This function checks added event or not.
Syntax
bool isEventHandlerAdded( 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 to this function. Returns false if the specified event could not be found or any parameters were invalid.
Code
Click to collapse [-]
Function sourcefunction isEventHandlerAdded( 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
Example
Click to collapse [-]
ClientThis is an example of the function.
bindKey ("num_2", "down", function() if not isEventHandlerAdded( 'onClientRender', root, open) then addEventHandler ("onClientRender", root, open) end end)
By nL~Enzo