ENT.OnTakeDamage

From GMod Wiki

Jump to: navigation, search
Event Hook
Hook NameOnTakeDamage
SyntaxENT:OnTakeDamage( CTakeDamageInfo dmginfo )
DescriptionCalled when something damages the entity.
ReturnsNil
Lua StateNewerServer.png
BBCode[b][url=wiki.garrysmod.com/?title=ENT.OnTakeDamage]ENT.OnTakeDamage [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b]


Examples

DescriptionStores a "health" variable and removes the entity if it's health gets to or below 0.
Used onNewerServer.png
Code
 ENT.OurHealth = 25; -- Amount of damage that the entity can handle - set to 0 to make it indestructible
 
 function ENT:OnTakeDamage(dmg)
	self:TakePhysicsDamage(dmg); -- React physically when getting shot/blown
 
	if(self.OurHealth <= 0) then return; end -- If the health-variable is already zero or below it - do nothing
 
	self.OurHealth = self.OurHealth - dmg:GetDamage(); -- Reduce the amount of damage took from our health-variable
 
	if(self.OurHealth <= 0) then -- If our health-variable is zero or below it
		self:Remove(); -- Remove our entity
	end
 end
OutputN/A


Additional Notes

See Also

Personal tools
Namespaces
Variants
Actions
Navigation
Lua Scripting
Functions
Hooks
Toolbox