RemoveEventHandler: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
mNo edit summary
 
(6 intermediate revisions by 5 users not shown)
Line 1: Line 1:
{{Needs_Checking|Why do we have to specify what element was the handler attached to? --[[User:Jbeta|jbeta]] 07:34, 3 June 2007 (CDT)
*Because multiple elements might use the same event and function --[[User:Kevuwk|Kevuwk]] 20:49, 22 June 2007 (CDT) }}
__NOTOC__  
__NOTOC__  
{{Server client function}}
This functions removes a handler function from an [[event]], so that the function is not called anymore when the event is triggered. See [[event system]] for more information on how the event system works.
This functions removes a handler function from an [[event]], so that the function is not called anymore when the event is triggered. See [[event system]] for more information on how the event system works.


==Syntax==  
==Syntax==  
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
bool removeEventHandler ( string eventName, element attachedTo, string functionName )  
bool removeEventHandler ( string eventName, element attachedTo, function functionVar )  
</syntaxhighlight>  
</syntaxhighlight>  


Line 12: Line 11:
*'''eventName:''' The name of the [[event]] you want to detach the handler function from.
*'''eventName:''' The name of the [[event]] you want to detach the handler function from.
*'''attachedTo:''' The [[element]] the handler was attached to.
*'''attachedTo:''' The [[element]] the handler was attached to.
*'''functionName:''' The name of the handler function that was attached.
*'''functionVar:''' The handler function that was attached.


===Returns===
===Returns===
Line 18: Line 17:


==Example==
==Example==
This example...
<section name="Client" class="client" show="true">
This example shows how to toggle a message on/off a screen with a command.
<syntaxhighlight lang="lua">
 
function drawText() -- A function to draw the text we want
dxDrawText(text, 10,100) -- creates a dx text 10 pixels from left, 100 from top of the screen
end
function doText(command, ...)
if command == "starttext" then -- if player wrote /starttext
text = table.concat({...}," ") -- then we retrieve the text
addEventHandler("onClientRender", getRootElement(), drawText) -- and since addEventHandler and removeEventHandler's syntax is the same, we just define the function we use later
elseif command == "stoptext" then
removeEventHandler("onClientRender", getRootElement(), drawText) -- this time we use removeEventHandler
end
end
addCommandHandler("starttext", doText) -- add two command handlers to doText function
addCommandHandler("stoptext", doText)
</syntaxhighlight>
</section>


==See Also==
==See Also==
{{Event_functions}}
{{Event_functions}}
[[Category:Incomplete]]
[[ru:removeEventHandler]]

Latest revision as of 21:41, 8 July 2010

This functions removes a handler function from an event, so that the function is not called anymore when the event is triggered. See event system for more information on how the event system works.

Syntax

bool removeEventHandler ( string eventName, element attachedTo, function functionVar ) 

Required Arguments

  • eventName: The name of the event you want to detach the handler function from.
  • attachedTo: The element the handler was attached to.
  • functionVar: The handler function that was attached.

Returns

Returns true if the event handler was removed successfully. Returns false if the specified event handler could not be found or invalid parameters were passed.

Example

Click to collapse [-]
Client

This example shows how to toggle a message on/off a screen with a command.


function drawText() -- A function to draw the text we want
	dxDrawText(text, 10,100) -- creates a dx text 10 pixels from left, 100 from top of the screen
end
function doText(command, ...)
	if command == "starttext" then -- if player wrote /starttext
		text = table.concat({...}," ") -- then we retrieve the text
		addEventHandler("onClientRender", getRootElement(), drawText) 		-- and since addEventHandler and removeEventHandler's syntax is the same, we just define the function we use later
	elseif command == "stoptext" then
		removeEventHandler("onClientRender", getRootElement(), drawText) 	-- this time we use removeEventHandler
	end
end
addCommandHandler("starttext", doText) -- add two command handlers to doText function
addCommandHandler("stoptext", doText)

See Also