CancelEvent: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
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.  


Line 17: Line 18:


==Example==  
==Example==  
<section name="Example 1" class="server" show="true">
This example stops the player ''huntedPlayer'' from entering a vehicle:
This example stops the player ''huntedPlayer'' from entering a vehicle:
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
Line 25: Line 27:
addEventHandler ( "onVehicleStartEnter", huntedPlayer, stopVehicleEntry )
addEventHandler ( "onVehicleStartEnter", huntedPlayer, stopVehicleEntry )
</syntaxhighlight>
</syntaxhighlight>
 
</section>
<section name="Example 2" class="client" show="true">
This example prevents any damage to a player clientside.
<syntaxhighlight lang="lua">
addEventHandler ( "onClientPlayerDamage", getRootElement(), cancelEvent )
</syntaxhighlight>
</section>
==See Also==
==See Also==
{{Event functions}}
{{Event functions}}

Revision as of 16:08, 15 August 2007

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 wasEventCanceled.

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 wasEventCanceled to check after your call to triggerEvent.

Syntax

bool cancelEvent ()   

Returns

Always returns true.

Example

Click to collapse [-]
Example 1

This example stops the player huntedPlayer from entering a vehicle:

-- call 'stopVehicleEntry' whenever hunterPlayer is about to enter a vehicle:
function stopVehicleEntry ( theplayer, seat, jacked )
   cancelEvent () -- stop the event from occuring
end
addEventHandler ( "onVehicleStartEnter", huntedPlayer, stopVehicleEntry )
Click to collapse [-]
Example 2

This example prevents any damage to a player clientside.

addEventHandler ( "onClientPlayerDamage", getRootElement(), cancelEvent )

See Also