Garry's Mod Wiki

Revision Difference

ENTITY:OnTakeDamage#529136

<function name="OnTakeDamage" parent="ENTITY" type="hook"> <ishook>yes</ishook> <description> Called when the entity is taking damage. <warning>Calling <page>Entity:TakeDamage</page>, <page>Entity:TakeDamageInfo</page>, <page>Entity:DispatchTraceAttack</page>, or <page>Player:TraceHullAttack</page> (if the entity is hit) in this hook on the victim entity can cause infinite loops since the hook will be called again. Make sure to setup recursion safeguards like the example below.</warning> ⤶ <note>This hook is only called for `ai` and `anim` type entities.</note>⤶ </description> <realm>Server</realm> <args> <arg name="damage" type="CTakeDamageInfo">The damage to be applied to the entity.</arg> </args> <rets> <ret type="number">Return a number to suppress default action. Basically `1` means took damage, `0` means did not take damage. Returning not a number (a bool) will perform the default action.</ret> <ret type="number">Basically `1` means took damage, `0` means did not take damage. For `ai` type entities, not returning a number will suppress default action and allow the NPC to die and ragdoll automatically.</ret> </rets> </function> <example> <description>All damage taken by this entity is applied twice. This will count the damage taken as two distinctive hits as opposed to just scaling it in <page>GM:EntityTakeDamage</page>.</description> <code> function ENT:OnTakeDamage( dmginfo ) -- Make sure we're not already applying damage a second time -- This prevents infinite loops if ( not self.m_bApplyingDamage ) then self.m_bApplyingDamage = true self:TakeDamageInfo( dmginfo ) self.m_bApplyingDamage = false end end </code> </example>