GetEventHandlers: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
 
(14 intermediate revisions by 6 users not shown)
Line 1: Line 1:
__NOTOC__
__NOTOC__
{{Server client function}}
{{Server client function}}
{{New feature/item|4.0132|1.3.1|4973|
{{New feature/item|3.0140|1.4|4973|
This function gets the attached functions from the event and attached element only from current lua script.
This function gets the attached functions from the event and attached element from current lua script.
}}
}}


Line 9: Line 9:
table getEventHandlers ( string eventName, element attachedTo )
table getEventHandlers ( string eventName, element attachedTo )
</syntaxhighlight>
</syntaxhighlight>


===Required Arguments===  
===Required Arguments===  
Line 16: Line 15:


===Returns===
===Returns===
Returns table with attached functions, false otherwise.
Returns table with attached functions, empty table otherwise.


===Example===
===Example===
<section name="Server" class="server" show="true">
<section name="Server" class="server" show="true">
<syntaxhighlight lang="lua">[Lua]
<syntaxhighlight lang="lua">
function isEventHandlerAdded( sEventName, pElementAttachedTo, func )
function isEventHandlerAdded( sEventName, pElementAttachedTo, func )
if  
if type( sEventName ) == 'string' and isElement( pElementAttachedTo ) and type( func ) == 'function' then
type( sEventName ) == 'string' and  
    local aAttachedFunctions = getEventHandlers( sEventName, pElementAttachedTo )
isElement( pElementAttachedTo ) and  
type( func ) == 'function'  
then
local aAttachedFunctions = getEventHandlers( sEventName, pElementAttachedTo )
if type( aAttachedFunctions ) == 'table' and #aAttachedFunctions > 0 then
if type( aAttachedFunctions ) == 'table' and #aAttachedFunctions > 0 then
for i, v in ipairs( aAttachedFunctions ) do
for i, v in ipairs( aAttachedFunctions ) do
Line 34: Line 29:
end
end
end
end
return false
end
end
return false
end
end
return false
return false
Line 46: Line 39:
addEventHandler( 'onPlayerWasted', root, onPlayerWasted )
addEventHandler( 'onPlayerWasted', root, onPlayerWasted )


addCommandHandler( 'removeOnPlayerWastedEvent',
addCommandHandler( 'removeOnPlayerWastedEvent', function()
function()
    if isEventHandlerAdded( 'onPlayerWasted', root, onPlayerWasted ) then
if isEventHandlerAdded( 'onPlayerWasted', root, onPlayerWasted ) then
        outputChatBox( 'onPlayerWasted succesfully removed!' )
outputChatBox( 'onPlayerWasted succesfully removed!' )
        removeEventHandler( 'onPlayerWasted', root, onPlayerWasted )
removeEventHandler( 'onPlayerWasted', root, onPlayerWasted )
    end
end
end)
end
)
</syntaxhighlight>
</syntaxhighlight>
</section>
</section>


==See also==
{{Event functions}}
{{Event functions}}
/code>
===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===

Latest revision as of 05:33, 28 April 2020

This function gets the attached functions from the event and attached element from current lua 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)

See also

Shared