Garry's Mod Wiki

Entity:SetWeaponModel

  Entity:SetWeaponModel( string viewModel, Weapon weapon = NULL )

Description

Sets the model and associated weapon to this viewmodel entity.

This is used internally when the player switches weapon.

View models are not drawn without a weapons associated to them.
This will silently fail if the entity is not a viewmodel.

Arguments

1 string viewModel
The model string to give to this viewmodel. Example: "models/weapons/c_smg1.mdl"
2 Weapon weapon = NULL
The weapon entity to associate this viewmodel to.

Example

Sets the model of the second viewmodel to the smg and associates it with the player's current weapon.

Entity( 1 ):GetViewModel( 1 ):SetWeaponModel( "models/weapons/c_smg1.mdl", Entity( 1 ):GetActiveWeapon() )

Example

Initializes the extra viewmodel in Deploy and hides it again on Holster, also plays the attack animation on left and right click.

SWEP.ViewModel = "models/weapons/v_pistol.mdl" SWEP.UseHands = false SWEP.ViewModelFlip = false --the default viewmodel won't be flipped SWEP.ViewModelFlip1 = true --the second viewmodel will function SWEP:Deploy() --get the second viewmodel local viewmodel1 = self:GetOwner():GetViewModel( 1 ) if ( IsValid( viewmodel1 ) ) then --associate its weapon to us viewmodel1:SetWeaponModel( self.ViewModel , self ) end self:SendViewModelAnim( ACT_VM_DEPLOY , 1 ) return true end function SWEP:Holster() local viewmodel1 = self:GetOwner():GetViewModel( 1 ) if ( IsValid( viewmodel1 ) ) then --set its weapon to nil, this way the viewmodel won't show up again viewmodel1:SetWeaponModel( self.ViewModel , nil ) end return true end --since self:SendWeaponAnim always sends the animation to the first viewmodel, we need this as a replacement function SWEP:SendViewModelAnim( act , index , rate ) if ( not game.SinglePlayer() and not IsFirstTimePredicted() ) then return end local vm = self:GetOwner():GetViewModel( index ) if ( not IsValid( vm ) ) then return end local seq = vm:SelectWeightedSequence( act ) if ( seq == -1 ) then return end vm:SendViewModelMatchingSequence( seq ) vm:SetPlaybackRate( rate or 1 ) end function SWEP:PrimaryAttack() self:SendViewModelAnim( ACT_VM_PRIMARYATTACK , 0 )--target the first viewmodel self:SetNextPrimaryFire( CurTime() + 0.25 ) end function SWEP:SecondaryAttack() self:SendViewModelAnim( ACT_VM_PRIMARYATTACK , 1 )--target the second self:SetNextSecondaryFire( CurTime() + 0.25 ) end
Output: