sourcemod icon indicating copy to clipboard operation
sourcemod copied to clipboard

[Feature Request] SDKHook for entity removals

Open dysphie opened this issue 2 years ago • 3 comments

I think it would be nice to have a hook that listens for specific entity removals, I usually find myself doing this:

#define MAX_EDICTS (1<<11)
bool isCat[MAX_EDICTS+1];

int CreateCat()
{
	int cat = CreateEntityByName(...);
	isCat[cat] = true;
}

public void OnEntityDestroyed(int entity)
{
	if (0 <= entity < sizeof(isCat) && isCat[entity])
	{		
		isCat[entity] = false;
		// Code
	}
}

which is a bit wasteful and annoying to maintain. Instead, it would be nicer to be able to do something like this:

int CreateCat()
{
	int cat = CreateEntityByName(...);
	SDKHook(cat, SDKHook_Removed, OnCatRemoved)
}

void OnCatRemoved(int cat)
{
	// Code
}

I'm not familiar with the CPP side of things so I don't know if this is technically possible though

dysphie avatar Aug 23 '22 16:08 dysphie

DHooks have this feature, to follow entity: https://sm.alliedmods.net/new-api/dhooks/DHookAddEntityListener https://sm.alliedmods.net/new-api/dhooks/DHookRemoveEntityListener

https://sm.alliedmods.net/new-api/dhooks/ListenCB

~~How about create SM plugin to do this ? I made a example. Haven't test in long usage. Hook OnEntityDestroyed Helper *edit If you need this feature to just for one plugin, try use ArrayList, store entity Reference ID and check again from SDKHooks OnEntityDestroyed. Then do your own function. Not require much code or looping.~~

ambaca avatar Aug 24 '22 15:08 ambaca

DHooks have this feature, to follow entity [...]

That's basically OnEntityDestroyed with extra steps. It doesn't allow the callback to fire for one specific entity; the plugin author still has to keep track which entities they want to act on.

I think the closest approach to what dysphie wants is using DHook to listen in on the entity's UpdateOnRemove virtual function, which does call into the entity list's NotifyRemoveEntity and subsequently OnEntityDeleted, which is what SDKHooks (and in turn, DHooks) uses.

nosoop avatar Aug 25 '22 15:08 nosoop

I too wish that this was implemented.

DarthMan avatar Apr 12 '24 13:04 DarthMan