CancelEvent: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(Improve example.)
(Improve example and description.)
Line 32: Line 32:
==Example==  
==Example==  
<section name="Example 1 - Server" class="server" show="true">
<section name="Example 1 - Server" class="server" show="true">
This example stops the player ''huntedPlayer'' from entering a vehicle:
This example stops the player from entering a vehicle:
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
-- call 'stopVehicleEntry' whenever hunterPlayer is about to enter a vehicle:
-- call 'stopVehicleEntry' whenever player is about to enter a vehicle:


function stopVehicleEntry()
function stopVehicleEntry()
Line 45: Line 45:
This example prevents any damage to a player clientside by making [[cancelEvent]] an event handler for the [[onClientPlayerDamage]] event.
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">
addEventHandler("onClientPlayerDamage", root, function() cancelEvent() end)
function onClientPlayerDamage()
cancelEvent()
end
addEventHandler("onClientPlayerDamage", root, onClientPlayerDamage)
</syntaxhighlight>
</syntaxhighlight>
</section>
</section>

Revision as of 14:35, 2 November 2021

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

Click to collapse [-]
Server
bool cancelEvent ( [ bool cancel = true, string reason = "" ] )   
Click to collapse [-]
Client
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

Click to collapse [-]
Example 1 - Server

This example stops the player from entering a vehicle:

-- call 'stopVehicleEntry' whenever player is about to enter a vehicle:

function stopVehicleEntry()
   cancelEvent() -- stop the event, the player will can't enter in vehicle 
end
addEventHandler("onVehicleStartEnter", root, stopVehicleEntry)
Click to collapse [-]
Example 2 - Client

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)

See Also