AddEvent: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
(8 intermediate revisions by 5 users not shown) | |||
Line 1: | Line 1: | ||
__NOTOC__ | __NOTOC__ | ||
{{Server client function}} | {{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== | ||
Line 12: | Line 12: | ||
===Optional Arguments=== | ===Optional Arguments=== | ||
*'''allowRemoteTrigger:''' A boolean specifying whether this event can be called | *'''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'' | Returns ''true'' if the event was added successfully, ''false'' if the event was already added. | ||
==Example== | ==Example== | ||
Line 21: | Line 21: | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
-- Add a new event called onSpecialEvent | -- Add a new event called onSpecialEvent | ||
addEvent ( "onSpecialEvent" ) | addEvent ( "onSpecialEvent", true ) | ||
-- Define our handler function, that takes a "text" parameter and outputs it to the chatbox | -- Define our handler function, that takes a "text" parameter and outputs it to the chatbox | ||
Line 29: | Line 29: | ||
-- Add it as a handler for our event | -- Add it as a handler for our event | ||
addEventHandler ( "onSpecialEvent", | 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", | triggerEvent ( "onSpecialEvent", root, "test" ) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 41: | Line 41: | ||
==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
- allowRemoteTrigger: A boolean specifying whether this event can be called remotely using triggerClientEvent / triggerServerEvent or not.
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.