Garry's Mod Wiki

Revision Difference

GM:HandlePlayerArmorReduction#552766

<function name="HandlePlayerArmorReduction" parent="GM" type="hook">⤶ <description>Called to handle player armor reduction, when player receives damage.⤶ ⤶ <validate>Clarify hook order with other damage hooks.</validate>⤶ </description>⤶ <added>2023.11.03</added>⤶ <realm>Server</realm>⤶ <args>⤶ <arg type="Player" name="ply">The player that took damage.</arg>⤶ <arg type="CTakeDamageInfo" name="dmginfo">The taken damage.</arg>⤶ </args>⤶ </function>⤶ ⤶ ⤶ <example>⤶ <description>The default Half-Life 2 armor reduction system.</description>⤶ ⤶ <code>⤶ function GM:HandlePlayerArmorReduction( ply, dmginfo )⤶ ⤶ -- If no armor, or special damage types, bypass armor ⤶ if ( ply:Armor() <= 0 || bit.band( dmginfo:GetDamageType(), DMG_FALL + DMG_DROWN + DMG_POISON + DMG_RADIATION ) != 0 ) then return end⤶ ⤶ local flBonus = 1.0 -- Each Point of Armor is worth 1/x points of health⤶ local flRatio = 0.2 -- Armor Takes 80% of the damage⤶ if ( GetConVar( "player_old_armor" ):GetBool() ) then⤶ flBonus = 0.5⤶ end⤶ ⤶ local flNew = dmginfo:GetDamage() * flRatio⤶ local flArmor = (dmginfo:GetDamage() - flNew) * flBonus⤶ ⤶ if ( !GetConVar( "player_old_armor" ):GetBool() ) then⤶ if ( flArmor < 1.0 ) then flArmor = 1.0 end⤶ end⤶ ⤶ -- Does this use more armor than we have?⤶ if ( flArmor > ply:Armor() ) then⤶ ⤶ flArmor = ply:Armor() * ( 1 / flBonus )⤶ flNew = dmginfo:GetDamage() - flArmor⤶ ply:SetArmor( 0 )⤶ ⤶ else⤶ ply:SetArmor( ply:Armor() - flArmor )⤶ end⤶ ⤶ dmginfo:SetDamage( flNew )⤶ ⤶ end⤶ </code>⤶ </example>⤶