Making an ENTITY explode on dmg

Quick question, how do you make an entity explode once a certain level of damage has been done?

Example of what i’m asking about: If I was to shoot my entity a few times, that would cause it to blow up ( dealing damage to surrounding players, like a darkrp printer ) and the entity itself would be gone.

http://wiki.garrysmod.com/page/ENTITY/OnTakeDamage

what do i put into that function to make it explode?

util.Effect

List of effects: https://wiki.garrysmod.com/page/Engine_Effects

I used env_explosion and env_shake to make it damageable, here is code:



function ENT:OnTakeDamage(dmginfo)
	local explosion = ents.Create("env_explosion");				
	explosion:SetPos(self:GetPos());
	explosion:SetKeyValue("iMagnitude", "96");
	explosion:Spawn();
	explosion:Activate();
	explosion:Fire("Explode", 0, 0);
	
	local shake = ents.Create("env_shake");
	shake:SetPos(self:GetPos());
	shake:SetKeyValue("amplitude", "512");
	shake:SetKeyValue("radius", "256");
	shake:SetKeyValue("duration", "1.5");
	shake:SetKeyValue("frequency", "255");
	shake:SetKeyValue("spawnflags", "4");
	shake:Spawn();
	shake:Activate();
	shake:Fire("StartShake", "", 0);
	
	self:Remove();		
end;


i would like the entity to take more damage before i call the explosion and etc how do i do that?

I don’t know if env_explosion is better in anyway, but there’s also just util.BlastDamage which is fairly simple and comes with the effect and sound as well.

As to taking more hits, you’ll wanna set some health for the entity or just do some math in the damage hook. You have the damage info passed on, so you can just keep adding the damages to a total and explode once it goes over a threshold you decide.





function ENT:OnTakeDamage( dmginfo )


    self:Destruct()


end

function ENT:Destuct()


	local entPos = self:GetPos()
	
	local edata = EffectData()
	
	edata:SetStart( entPos )
	
	edata:SetOrigin( entPos )
	
	edata:SetScale( 1 )
	
	util.Effect( "Explosion", edata )
	
	self:Remove()


end




You can have it so when the entity hits 0 hp to explode. Figure it out yourself. :slight_smile: