garrysmod icon indicating copy to clipboard operation
garrysmod copied to clipboard

Entity:HookOutput( ), Entity:UnhookOutput( )

Open Kogitsune opened this issue 10 years ago • 4 comments

Creates Entity:HookOutput( name, uid, func ) and Entity:UnhookOutput( name, uid ) to catch entity outputs, especially engine ones. Conforms to the behavior of hooks, where passing the entity as the uid causes it to be passed to the function.

Could obviously use some improvement, but it is currently the only vanilla way to do this that I know.

Kogitsune avatar Feb 24 '15 23:02 Kogitsune

I'm not sure about your EntityRemoved hook. The hook will not be called if the "self" parameter isn't valid, because that is when it'll be removed.

Should you be using ENT:OnRemove instead? If so, removing that hook would make this code good in my books.

The arguments passed to the hook callback also seem odd. If the targetted entity isn't valid you should probably be passing NULL rather than shifting all the arguments left (or not calling the hook at all).

You've also got ENT.Hooks defined yet unused.

Also, to be nitpicky, your localised k and v values aren't actually going to do anything on line 44. The for operation will actually create two new variables in another block for each iteration.

sohpeach avatar Feb 25 '15 18:02 sohpeach

A month later, you are right. on most parts - the k, v is force of habit so I removed it. The extra hook was removed and replaced with OnRemove.

The argument shifting is to match the expected behavior of hook.Add - if you pass an entity as the second parameter, it is passed to the function as the first argument.

ent1:HookOutput( "OnDamaged", ent2, ent2.OnEnt1Damaged )

versus

ent1:HookOutput( "OnDamaged", "When My Thing Gets Hurt", function( ) ... end )

Kogitsune avatar Mar 22 '15 18:03 Kogitsune

Fair enough :v:

sohpeach avatar Mar 22 '15 18:03 sohpeach

This is a good idea, but I do not like usage of dummy entity for this purpose. Perhaps this needs to be done on the C++ side.

robotboy655 avatar Oct 22 '19 17:10 robotboy655