GetEventHandlers: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
m (→‎Example: add client side exmaple)
(add important note)
 
(2 intermediate revisions by 2 users not shown)
Line 4: Line 4:
This function gets the attached functions from the event and attached element from current lua script.
This function gets the attached functions from the event and attached element from current lua script.
}}
}}
{{Important Note|This function only checks the current script.}}


==Syntax==
==Syntax==
Line 49: Line 51:


<section name="Clientside example" class="client" show="true">
<section name="Clientside example" class="client" show="true">
This example removes all onClientMarkerHit event in current script .
This example removes all onClientMarkerHit event in current script.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
local events = getEventHandlers ( "onClientMarkerHit", resourceRoot )
local events = getEventHandlers ( "onClientMarkerHit", resourceRoot )
    for i,v in ipairs(events) do  
for i,v in ipairs(events) do  
removeEventHandler ( "onClientMarkerHit", resourceRoot, v)  
    removeEventHandler ( "onClientMarkerHit", resourceRoot, v)  
    end
end
end
</syntaxhighlight>
</syntaxhighlight>

Latest revision as of 15:22, 2 January 2026

This function gets the attached functions from the event and attached element from current lua script.


[[{{{image}}}|link=|]] Important Note: This function only checks the current script.

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, empty table otherwise.

Example

Click to collapse [-]
Server
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

function onPlayerWasted()
	outputChatBox( getPlayerName( source ) .. ' died.' )
end
addEventHandler( 'onPlayerWasted', root, onPlayerWasted )

addCommandHandler( 'removeOnPlayerWastedEvent', function()
    if isEventHandlerAdded( 'onPlayerWasted', root, onPlayerWasted ) then
        outputChatBox( 'onPlayerWasted succesfully removed!' )
        removeEventHandler( 'onPlayerWasted', root, onPlayerWasted )
    end
end)
Click to collapse [-]
Clientside example

This example removes all onClientMarkerHit event in current script.

local events = getEventHandlers ( "onClientMarkerHit", resourceRoot )
for i,v in ipairs(events) do 
    removeEventHandler ( "onClientMarkerHit", resourceRoot, v) 
end

See also