CancelEvent: Difference between revisions
No edit summary |
mNo edit summary |
||
(21 intermediate revisions by 14 users not shown) | |||
Line 1: | Line 1: | ||
__NOTOC__ | __NOTOC__ | ||
{{Server client function}} | |||
This function is used to stop the automatic internal handling of events, for example this can be used to prevent an item being given to a player when they walk over a pickup, by canceling the [[onPickupUse]] event. | This function is used to stop the automatic internal handling of events, for example this can be used to prevent an item being given to a player when they walk over a pickup, by canceling the [[onPickupUse]] event. | ||
[[cancelEvent]] does not have an effect on all events, see the individual event's pages for information on what happens when the event is canceled. [[cancelEvent]] does not stop further event handlers from being called, as the order of event handlers being called is undefined in many cases. Instead, you can see if the currently active event has been cancelled using [[ | [[cancelEvent]] does not have an effect on all events, see the individual event's pages for information on what happens when the event is canceled. [[cancelEvent]] does not stop further event handlers from being called, as the order of event handlers being called is undefined in many cases. Instead, you can see if the currently active event has been cancelled using [[wasEventCancelled]]. | ||
The use of cancelEvent outside of an event handler has no effect. | The use of cancelEvent outside of an event handler has no effect. | ||
If you implement your own custom events and want to handle them being cancelled, you should call [[ | If you implement your own custom events and want to handle them being cancelled, you should call [[wasEventCancelled]] to check after your call to [[triggerEvent]]. | ||
==Syntax== | ==Syntax== | ||
<section name="Server" class="server" show="true"> | |||
<syntaxhighlight lang="lua"> | |||
bool cancelEvent ( [ bool cancel = true, string reason = "" ] ) | |||
</syntaxhighlight> | |||
</section> | |||
<section name="Client" class="client" show="true"> | |||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
bool cancelEvent () | bool cancelEvent () | ||
</syntaxhighlight> | </syntaxhighlight> | ||
</section> | |||
===Optional Arguments=== | |||
{{OptionalArg}} | |||
*'''cancel:''' True to cancel, false to uncancel. | |||
*'''reason:''' The reason for cancelling the event. | |||
===Returns=== | ===Returns=== | ||
Line 17: | Line 31: | ||
==Example== | ==Example== | ||
This example stops the player | <section name="Example 1 - Server" class="server" show="true"> | ||
This example stops the player from entering a vehicle. | |||
<syntaxhighlight lang="lua"> | |||
function onVehicleStartEnter() | |||
cancelEvent() | |||
end | |||
addEventHandler("onVehicleStartEnter", root, onVehicleStartEnter) | |||
</syntaxhighlight> | |||
</section> | |||
<section name="Example 2 - Client" class="client" show="true"> | |||
This example prevents any damage to a player clientside by making [[cancelEvent]] an event handler for the [[onClientPlayerDamage]] event. | |||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
function onClientPlayerDamage() | |||
cancelEvent() | |||
function | |||
end | end | ||
addEventHandler("onClientPlayerDamage", root, onClientPlayerDamage) | |||
</syntaxhighlight> | </syntaxhighlight> | ||
</section> | |||
==See Also== | ==See Also== | ||
{{Event functions}} | {{Event functions}} | ||
[[ru:cancelEvent]] |
Latest revision as of 11:23, 8 June 2022
This function is used to stop the automatic internal handling of events, for example this can be used to prevent an item being given to a player when they walk over a pickup, by canceling the onPickupUse event.
cancelEvent does not have an effect on all events, see the individual event's pages for information on what happens when the event is canceled. cancelEvent does not stop further event handlers from being called, as the order of event handlers being called is undefined in many cases. Instead, you can see if the currently active event has been cancelled using wasEventCancelled.
The use of cancelEvent outside of an event handler has no effect.
If you implement your own custom events and want to handle them being cancelled, you should call wasEventCancelled to check after your call to triggerEvent.
Syntax
bool cancelEvent ( [ bool cancel = true, string reason = "" ] )
bool cancelEvent ()
Optional Arguments
NOTE: When using optional arguments, you might need to supply all arguments before the one you wish to use. For more information on optional arguments, see optional arguments.
- cancel: True to cancel, false to uncancel.
- reason: The reason for cancelling the event.
Returns
Always returns true.
Example
This example stops the player from entering a vehicle.
function onVehicleStartEnter() cancelEvent() end addEventHandler("onVehicleStartEnter", root, onVehicleStartEnter)
This example prevents any damage to a player clientside by making cancelEvent an event handler for the onClientPlayerDamage event.
function onClientPlayerDamage() cancelEvent() end addEventHandler("onClientPlayerDamage", root, onClientPlayerDamage)