OnPlayerWeaponSwitch: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
(Adding [Cancel Effect])
Line 1: Line 1:
__NOTOC__  
__NOTOC__  
{{Server event}}
{{Server event}}
This event is triggered whenever a player's equipped weapon '''slot''' changes. This means [[giveWeapon]] and [[takeWeapon]] will trigger this function if the equipped slot is forced to change.  
This event is triggered whenever a player's equipped weapon '''slot''' changes. This means [[giveWeapon]] and [[takeWeapon]] will trigger this event if the equipped slot is forced to change.  


==Parameters==  
==Parameters==  
Line 16: Line 16:
==Example==  
==Example==  
This example disables use of the minigun upon switch.  It should be noted that this can be done more efficiently clientside.
This example disables use of the minigun upon switch.  It should be noted that this can be done more efficiently clientside.
==Cancel effect==
If this event is [[Event system#Canceling|canceled]], then the player's weapon won't be switched. If this event has been caused by [[giveWeapon]] or [[takeWeapon]], then the player won't receive/lose the weapon.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
WeaponID = {
WeaponID = {

Revision as of 15:46, 14 June 2023

This event is triggered whenever a player's equipped weapon slot changes. This means giveWeapon and takeWeapon will trigger this event if the equipped slot is forced to change.

Parameters

int previousWeaponID, int currentWeaponID
  • previousWeaponID: An integer representing the weapon that was switched from.
  • currentWeaponID: An integer representing the weapon that was switched to.

Source

The source of this event is the player that switched his weapon.

Example

This example disables use of the minigun upon switch. It should be noted that this can be done more efficiently clientside.

Cancel effect

If this event is canceled, then the player's weapon won't be switched. If this event has been caused by giveWeapon or takeWeapon, then the player won't receive/lose the weapon.

WeaponID = {
	[31] = true,
	[36] = true,
	[38] = true,
}

--add an event handler for onPlayerWeaponSwitch
addEventHandler ( 'onPlayerWeaponSwitch', getRootElement ( ),
	function ( previousWeaponID, currentWeaponID )
		if ( WeaponID[currentWeaponID] ) then
			toggleControl ( source, 'fire', false ) --disable the fire button
		else
			toggleControl ( source, 'fire', true ) --enable it
		end
	end
)

See Also

Player events


Event functions