CUserCmd:SelectWeapon

From Garry's Mod
Jump to: navigation, search
 CUserCmd:SelectWeapon( )

Contents

Description

Forces the associated player to select a weapon.
This is used internally in the default HL2 weapon selection HUD.

WARNING Due to a bug, you will have to force this function to run until Player:GetActiveWeapon returns the chosen weapon, although it is also advised to add a time limit in case the switch fails for any reason
NOTE This is the ideal function to use to create a custom weapon selection HUD, as it allows prediction to run properly for WEAPON:Deploy and GM:PlayerSwitchWeapon

Arguments

Weapon weapon

The weapon entity to select.

Examples

Example

Example of properly switching weapons on the client, supporting prediction.

local meta = FindMetaTable( "Player" )
function meta:SelectWeapon( class )
	if ( !self:HasWeapon( class ) ) then return end
	self.DoWeaponSwitch = self:GetWeapon( class )
end

hook.Add( "CreateMove", "WeaponSwitch", function( cmd )
	if ( !IsValid( LocalPlayer().DoWeaponSwitch ) ) then return end

	cmd:SelectWeapon( LocalPlayer().DoWeaponSwitch )

	if ( LocalPlayer():GetActiveWeapon() == LocalPlayer().DoWeaponSwitch ) then
		LocalPlayer().DoWeaponSwitch = nil 
	end
end )

-- Somewhere in your code: LocalPlayer():SelectWeapon( "weapon_pistol" )


Personal tools
Navigation