alliedmodders / sourcemod

SourceMod - Source Engine Scripting and Administration
http://www.sourcemod.net/
985 stars 423 forks source link

[Feature Request] SDKHook for entity removals #1827

Open dysphie opened 2 years ago

dysphie commented 2 years ago

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

ambaca commented 2 years ago

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.~~

nosoop commented 2 years ago

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.

DarthMan commented 7 months ago

I too wish that this was implemented.