IsEventHandlerAdded

From Multi Theft Auto: Wiki


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 source
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
	  end
     end
     return false
end

Example

Click to collapse [-]
Client

This 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