OnPlayerWeaponSwitch: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
mNo edit summary
Line 17: Line 17:
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.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
function weaponSwitchDisableMinigun ( previousWeaponID, currentWeaponID ) --when a player switches his weapon
WeaponID = {
    if currentWeaponID == 38 then --if the weapon ID is minigun
[31] = true,
          toggleControl ( source, "fire", false ) --disable the fire button
[36] = true,
    else --otherwise
[38] = true,
          toggleControl ( source, "fire", true )  --enable it
}
    end
 
end
--add an event handler for onPlayerWeaponSwitch
--add an event handler for onPlayerWeaponSwitch
addEventHandler ( "onPlayerWeaponSwitch", getRootElement(), weaponSwitchDisableMinigun )
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
)
</syntaxhighlight>
</syntaxhighlight>


{{See also/Server event|Player events}}
{{See also/Server event|Player events}}

Revision as of 12:05, 14 November 2012

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.

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.

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