• Make scripted entity have health and break
    4 replies, posted
I realized that I don't actually know how to do this without manually doing it. Is there a way?
Hook into EntityTakeDamage [lua]DONT_TAKE_DAMAGE = { "prop_vehicle_jeep", "prop_vehicle_airboat" } hook.Add( "EntityTakeDamage", "EntitiesCanHurtToo", function( ent, dmginfo ) if ( !IsValid( ent ) ) then return; end if ( table.HasValue( DONT_TAKE_DAMAGE, ent:GetClass( ) ) ) then return; end if ( SERVER ) then if ( !ent._InternalHealth ) then ent._InternalHealth = 100; end // Ensure that we have a health var set, otherwise set up default local _dmg = dmginfo:GetDamage( ); ent._InternalHealth = ent._InternalHealth - _dmg; print( ent ) print( _dmg ) print( ent._InternalHealth ) if ( ent._InternalHealth <= 0 ) then // Make an explosion or something -- util.Effect... // Make it play a broken sound -- ent.EmitSound( ) // Maybe spawn junk or salvage in its place -- local _junk = ents.Create( ... ) -- _junk:SetPos( ent:GetPos( ) ) -- _junk:SetAngles( ent:GetAngles( ) ) -- _junk:Spawn( ) -- _junk:Activate( ) -- _junk:SetColor( Color( 50, 50, 50, 255 ) ) //Remove it ent:Remove( ) end end end )[/lua] Untested, but it should work. Do may want to do stuff with damage type, and scaling of damage depending on the prop/entity type. Edit: Corrected using .Health, a function value. Tested and working.
Yeah, that's the manual way I was referring to. I was hoping I didn't have to do something like that.
You literally can't. On a side note, ENT:OnTakeDamage would be more elegant than using a hook.
Why are you saving health as _InternalHealth? Just do self:SetHealth(999) in ENT:Initialize and in ENT:OnTakeDamage subtract from it. If it's <= 0, call self:ExplodeNShit().
Sorry, you need to Log In to post a reply to this thread.