GetEventHandlers: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
mNo edit summary
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.0132|1.3.1|4973|
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.
}}
}}

Revision as of 06:58, 22 April 2013

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, false otherwise.

Example

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

Requirements

Minimum server version 1.3.1-9.04973
Minimum client version 1.3.1-9.04973

Note: Using this feature requires the resource to have the above minimum version declared in the meta.xml <min_mta_version> section. e.g. <min_mta_version server="1.3.1-9.04973" client="1.3.1-9.04973" />

See also