• Creating an entity with a script, and then adding a hook to it?
    7 replies, posted
This snippet of code is from a Banana Peel entity I found on the Toybox forever ago, and I'm making a few modifications to make it work in TTT. [code] local ragdoll = ents.Create( "prop_ragdoll" ); --prop_ragdoll ragdoll:SetPos( ent:GetPos( ) + Vector( 0, 0, 5 ) ); ragdoll:SetAngles( Angle( 0, ent:GetAngles( ).Yaw, 0 ) ); ragdoll:SetModel( ent:GetModel( ) ); ragdoll:Spawn( ); ragdoll:SetOwner(ent) ragdoll:Activate( ); local store_health = ent:Health(); local store_credits = ent:GetCredits() ragdoll:hook.Add("OnTakeDamage", "damagehook", function(dmg) //I KNOW I'm not doing this right, but I've seen it done before and can't remember where so I could check it for myself. ragdoll:GetOwner():TakeDamage(dmg) store_health = ragdoll:GetOwner():Health() end) [/code] How would I add a hook to the player's ragdoll? I know it's possible, I've just never figured out how and the Wiki isn't any help at all.
Is [URL="http://wiki.garrysmod.com/page/ENTITY/OnTakeDamage"]this[/URL] what you're looking for? There's a [URL="http://wiki.garrysmod.com/page/Category:ENTITY_Hooks"]whole list[/URL] of hooks just for entities.
No, I'm trying to figure out how to add a hook to a specific entity from an outside script.
You can't.
What exactly are you trying achieve? Someone might be able to suggest an alternative solution.
I'm pretty sure I've seen it done before... you sure it can't be done?
You can kind of do what you describe with Entity:AddCallback [url]http://wiki.garrysmod.com/page/Entity/AddCallback[/url] but it only has 3 different things you can listen for. The approach I would take is: when spawning your entity/ragdoll set a variable on it, then add a hook normally that only affects entities with that specific variable. Like this (not 100% actual code) [lua]--in ragdoll spawning code newragdoll.myaddon_CanTakeDamage = true --the hook hook.Add("EntityTakeDamage", "my addon hook", function(ent,dmginfo) if IsValid(ent) and ent.myaddon_CanTakeDamage then --do your stuff here end end)[/lua]
Yeah I guess that'd work. I just could've sworn I've seen scripts that added hooks to individual entities... oh well, thanks wh1t3!
Sorry, you need to Log In to post a reply to this thread.