AddEvent: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
m (Simplified example)
m (Added template, sections, improved example)
Line 1: Line 1:
__NOTOC__  
__NOTOC__  
{{Server client function}}
This function allows you to register a custom [[event]]. Custom events function exactly like the built in events. See [[event system]] for more information on the event system.
This function allows you to register a custom [[event]]. Custom events function exactly like the built in events. See [[event system]] for more information on the event system.


==Syntax==  
==Syntax==  
<section name="Server and Client" class="both" show="true">
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
bool addEvent ( string eventName )   
bool addEvent ( string eventName )   
Line 12: Line 14:
===Returns===
===Returns===
Returns ''true'' if the event was added successfully, ''false'' otherwise.
Returns ''true'' if the event was added successfully, ''false'' otherwise.
</section>


==Example==  
==Example==  
<section name="Example" class="both" show="true">
This example will define a new event called ''onSpecialEvent''.
This example will define a new event called ''onSpecialEvent''.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
Line 19: Line 23:
addEvent ( "onSpecialEvent" )
addEvent ( "onSpecialEvent" )


-- Define our handler function, that takes a "text" parameter
-- Define our handler function, that takes a "text" parameter and outputs it to the chatbox
function specialEventHandler ( text )
function specialEventHandler ( text )
outputChatBox ( text )
outputChatBox ( text )
Line 30: Line 34:
You can then trigger this event later on using:
You can then trigger this event later on using:
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
triggerEvent ( "onSpecialEvent", rootElement, "test" )
triggerEvent ( "onSpecialEvent", getRootElement(), "test" )
</syntaxhighlight>
</syntaxhighlight>
This will cause the handler to be triggered, so "test" will be output to the chatbox.
</section>


==See Also==
==See Also==
{{Event functions}}
{{Event functions}}

Revision as of 21:44, 12 August 2007

This function allows you to register a custom event. Custom events function exactly like the built in events. See event system for more information on the event system.

Syntax

Click to collapse [-]
Server and Client
bool addEvent ( string eventName )   

Required Arguments

  • eventName: The name of the event you wish to create.

Returns

Returns true if the event was added successfully, false otherwise.

Example

Click to collapse [-]
Example

This example will define a new event called onSpecialEvent.

-- Add a new event called onSpecialEvent
addEvent ( "onSpecialEvent" )

-- Define our handler function, that takes a "text" parameter and outputs it to the chatbox
function specialEventHandler ( text )
	outputChatBox ( text )
end

-- Add it as a handler for our event
addEventHandler ( "onSpecialEvent", getRootElement(), specialEventHandler )

You can then trigger this event later on using:

	triggerEvent ( "onSpecialEvent", getRootElement(), "test" )

This will cause the handler to be triggered, so "test" will be output to the chatbox.

See Also

Shared