Revision Difference
Entity:SetWeaponModel#547970
<function name="SetWeaponModel" parent="Entity" type="classfunc">
<description>
Sets the model and associated weapon to this viewmodel entity.
This is used internally when the player switches weapon.
<note>View models are not drawn without a weapons associated to them.</note>
<warning>This will silently fail if the entity is not a viewmodel.</warning>
</description>
<realm>Shared</realm>
<args>
<arg name="viewModel" type="string">The model string to give to this viewmodel.
Example: "models/weapons/c_smg1.mdl"</arg>
<arg name="weapon" type="Weapon" default="NULL">The weapon entity to associate this viewmodel to.</arg>
</args>
</function>
<example>
<description>Sets the model of the second viewmodel to the smg and associates it with the player's current weapon.</description>
<code>Entity( 1 ):GetViewModel( 1 ):SetWeaponModel( "models/weapons/c_smg1.mdl", Entity( 1 ):GetActiveWeapon() )</code>
</example>
<example>
<description>Initializes the extra viewmodel in Deploy and hides it again on Holster, also plays the attack animation on left and right click.</description>
<code>
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.Owner:GetViewModel( 1 )
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.Owner:GetViewModel( 1 )
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.Owner:GetViewModel( index )
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
</code>
<output><image src="SetWeaponModelExample.jpg" alt="thumb|center"/></output>
</example>