GM/EntityTakeDamage

From Garry's Mod
(Difference between revisions)
Jump to: navigation, search
(hook.Add examples)
 
Line 20: Line 20:
 
{{Example
 
{{Example
 
|Description=Explosion damage is reduced to players only.
 
|Description=Explosion damage is reduced to players only.
|Code=function GM:EntityTakeDamage( target, dmginfo )
+
|Code=hook.Add( "EntityTakeDamage", "EntityDamageExample", function( target, dmginfo )
  
    if ( target:IsPlayer() and dmginfo:IsExplosionDamage() ) then
+
if ( target:IsPlayer() and dmginfo:IsExplosionDamage() ) then
+
        dmginfo:ScaleDamage( 0.5 ) // Damage is now half of what you would normally take.
+
  
    end
+
dmginfo:ScaleDamage( 0.5 ) // Damage is now half of what you would normally take.
 +
 
 +
end
  
end
+
end )
 
}}
 
}}
 
{{Example
 
{{Example
 
|Description=Players in vehicles takes halved damage.
 
|Description=Players in vehicles takes halved damage.
|Code=function GM:EntityTakeDamage( target, dmginfo )
+
|Code=hook.Add( "EntityTakeDamage", "EntityDamageExample2", function( target, dmginfo )
  
 
     if ( target:IsVehicle() ) then
 
     if ( target:IsVehicle() ) then
Line 46: Line 46:
 
     end
 
     end
  
end
+
end )
 
}}
 
}}

Latest revision as of 14:01, 17 May 2019

 GM:EntityTakeDamage( )

Contents

Description

Called when an entity takes damage. You can modify all parts of the damage info in this hook.

Arguments

Entity target

The entity taking damage

Arguments

CTakeDamageInfo dmg

Damage info

Returns

boolean

Return true to completely block the damage event

Examples

Example

Explosion damage is reduced to players only.

hook.Add( "EntityTakeDamage", "EntityDamageExample", function( target, dmginfo )

	if ( target:IsPlayer() and dmginfo:IsExplosionDamage() ) then

		dmginfo:ScaleDamage( 0.5 ) // Damage is now half of what you would normally take.

	end

end )


Examples

Example

Players in vehicles takes halved damage.

hook.Add( "EntityTakeDamage", "EntityDamageExample2", function( target, dmginfo )

    if ( target:IsVehicle() ) then
 
        local ply = target:GetDriver()
        if ( IsValid(ply) && dmginfo:GetDamage() > 1 ) then
            dmginfo:SetDamage(dmginfo:GetDamage() / 2)
            ply:TakeDamageInfo(dmginfo)
            dmginfo:SetDamage(0)
        
        end

    end

end )


Personal tools
Navigation