AddEvent: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(Needs checking)
No edit summary
 
(19 intermediate revisions by 9 users not shown)
Line 1: Line 1:
{{Needs_Checking|Event handlers aren't coded in the map structure anymore, so the second parameter is not necessary anymore (it doesn't limit the argument number either). --[[User:Jbeta|jbeta]]}}
__NOTOC__  
__NOTOC__  
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.
{{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.


==Syntax==  
==Syntax==  
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
bool addEvent ( string eventName, string arguments )   
bool addEvent ( string eventName [, bool allowRemoteTrigger = false ] )   
</syntaxhighlight>  
</syntaxhighlight>  


===Required Arguments===  
===Required Arguments===  
*'''eventName:''' The name of the event you wish to create
*'''eventName:''' The name of the event you wish to create.
*'''arguments:''' A string of comma seperated variable names indicating the arguments your event takes. These names are used for event handlers that are coded in the map structure. For example "message, playerName".
 
===Optional Arguments===
*'''allowRemoteTrigger:''' A boolean specifying whether this event can be called remotely using [[triggerClientEvent]] / [[triggerServerEvent]] or not.


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


==Example==  
==Example==  
This example will define a new event called ''onSpecialEvent''.
This example will define a new event called ''onSpecialEvent''.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
-- Get the root map element
-- Add a new event called onSpecialEvent
rootElement = getRootElement ()
addEvent ( "onSpecialEvent", true )
 
-- Add a new event called onSpecialEvent that has one parameter 'text'
addEvent ( "onSpecialEvent", "text" )


-- Add an event handler
-- Define our handler function, that takes a "text" parameter and outputs it to the chatbox
addEventHandler ( "onSpecialEvent", rootElement, "specialEventHandler" )
-- Define our handler function
function specialEventHandler ( text )
function specialEventHandler ( text )
outputChatBox ( text )
outputChatBox ( text )
end
end


-- Add it as a handler for our event
addEventHandler ( "onSpecialEvent", root, specialEventHandler )
</syntaxhighlight>
</syntaxhighlight>


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", root, "test" )
</syntaxhighlight>
</syntaxhighlight>
This will cause the handler to be triggered, so "test" will be output to the chatbox.


==See Also==
==See Also==
{{Event functions}}
{{Event functions}}
[[ru:addEvent]]

Latest revision as of 07:58, 4 November 2020

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

bool addEvent ( string eventName [, bool allowRemoteTrigger = false ] )   

Required Arguments

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

Optional Arguments

Returns

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

Example

This example will define a new event called onSpecialEvent.

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

-- 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", root, specialEventHandler )

You can then trigger this event later on using:

	triggerEvent ( "onSpecialEvent", root, "test" )

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

See Also