AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Hook Entity Removal/Deletion/Killing (https://forums.alliedmods.net/showthread.php?t=299042)

Depresie 06-30-2017 13:57

Hook Entity Removal/Deletion/Killing
 
Is there any method to hook the moment when an entity is deleted from the game/map ?

HamletEagle 06-30-2017 14:22

Re: Hook Entity Removal/Deletion/Killing
 
The forward passes the entity index.

Depresie 06-30-2017 14:23

Re: Hook Entity Removal/Deletion/Killing
 
Updated, also

1. is there any empty field on entities to store strings ? like pev_iuser1 ?
2. is there any other way to hook all weapons on Ham_ItemDeploy/Ham_ItemKill than hooking each one of them one by one or with a loop ?

HamletEagle 06-30-2017 15:47

Re: Hook Entity Removal/Deletion/Killing
 
Quote:

1. is there any empty field on entities to store strings ? like pev_iuser1 ?
I'm not sure and I don't feel like browsing all the pev_* values to find out. You can do that on your own. Anyway, there's a trick you can do by using EngFunc_AllocString and glb_pStringBase/EngFunc_SzFromIndex.

PHP Code:

stock set_string_int(const ent, const field, const string[])
{
    if(
pev_valid(ent))
    {
        if(
string[0] != EOS)
        {
            new 
offset engfunc(EngFunc_AllocStringstring)
            
set_pev(entfieldoffset)
        }
    }
}

stock get_string_int(const ent, const field, const string[], const size)
{
    if(
pev_valid(ent))
    {
        if(
size != 0)
        {
            new 
offset pev(entfield)
            
global_get(glb_pStringBaseoffsetstringsize
        }
    }


Note that allocating a string is an expensive operation and the same string will be allocated multiple times if you call that native for more than once for the same string. What I would recommend you to do, if you use the above method is to allocate only once and remember in a variable or somewhere the allocated value, because it won't change. You can even use a trie, the string is the key and the value is the "offset" obtained.

NiHiLaNTh 06-30-2017 15:47

Re: Hook Entity Removal/Deletion/Killing
 
1. It really depends on the entity, but I am pretty sure there are some unused string fields, like pev_noise1,2,3 (I know that doors definetely use those fields), pev_message.

Depresie 06-30-2017 16:03

Re: Hook Entity Removal/Deletion/Killing
 
I was thinking about pev_noise but im not sure if they are unused.. i guess i will have to find out..

Hams, i remember somewhere you were explaining us how to retrieve the weapon entity index from within the weaponbox in FM_SetModel forward, but i don't remember where..

Could you please tell me again how i can retrieve it ?

klippy 06-30-2017 16:38

Re: Hook Entity Removal/Deletion/Killing
 
Quote:

Originally Posted by Depresie (Post 2532714)
1. is there any empty field on entities to store strings ? like pev_iuser1 ??

You can use the Custom Entity Data module.

HamletEagle 07-01-2017 08:06

Re: Hook Entity Removal/Deletion/Killing
 
Quote:

Originally Posted by KliPPy (Post 2532744)
You can use the Custom Entity Data module.

I did not look at the code, so could you explain how it's different than using a trie and saving data with entity id as key?

Quote:

Originally Posted by Depresie (Post 2532736)
Hams, i remember somewhere you were explaining us how to retrieve the weapon entity index from within the weaponbox in FM_SetModel forward, but i don't remember where..
?

Retrieving in FM_SetModel is the same as retrieving anywhere else.

PHP Code:

#include <amxmodx>
#include <fakemeta>
#include <hamsandwich>

#define MAX_ITEM_TYPES 6

new const m_rgpPlayerItems_CWeaponBox[MAX_ITEM_TYPES] = {3435, ...}

const 
m_pNext           42 
const XoCWeaponBox      4
const XoCBasePlayerItem 4

public plugin_init()
{
    
register_forward(FM_SetModel"pfnSetModel"false)
}

public 
pfnSetModel(const entity, const model[])
{
    if(
pev_valid(entity))
    {
        new 
className[32]
        
pev(entitypev_classnameclassNamecharsmax(className))
        if(
equal(className"weaponbox"))
        {
            new 
weaponEntity
            
for(new 1sizeof m_rgpPlayerItems_CWeaponBoxi++) 
            { 
                
weaponEntity get_pdata_cbase(entitym_rgpPlayerItems_CWeaponBox[i], XoCWeaponBox)
                while(
pev_valid(weaponEntity)) 
                { 
                    
//do your stuff
                    
weaponEntity get_pdata_cbase(weaponEntitym_pNextXoCBasePlayerItem)
                } 
            } 
        }
    }


While a weaponbox can contain multiple weapons and ammo types, in CS they usually contain only one. You could have weaponboxes with multiple weapons if a coder/mapper decides to do that, but by default there is only one.
This means that the while() loop is not really needed and the simplified code will be like:

PHP Code:

#include <amxmodx>
#include <fakemeta>
#include <hamsandwich>

#define MAX_ITEM_TYPES 6

new const m_rgpPlayerItems_CWeaponBox[MAX_ITEM_TYPES] = {3435, ...}
const 
XoCWeaponBox      4

public plugin_init()
{
    
register_forward(FM_SetModel"pfnSetModel"false)
}

public 
pfnSetModel(const entity, const model[])
{
    if(
pev_valid(entity))
    {
        new 
className[32]
        
pev(entitypev_classnameclassNamecharsmax(className))
        if(
equal(className"weaponbox"))
        {
            new 
weaponEntity
            
for(new 1sizeof m_rgpPlayerItems_CWeaponBoxi++) 
            { 
                
weaponEntity get_pdata_cbase(entitym_rgpPlayerItems_CWeaponBox[i], XoCWeaponBox)
                if(
pev_valid(weaponEntity)) 
                { 
                    
//do your stuff
                    
break
                } 
            } 
        }
    }


I would still advise you to use the first code, the difference in performance is almost non-existent.

Depresie 07-01-2017 08:41

Re: Hook Entity Removal/Deletion/Killing
 
Thanks Hams

klippy 07-02-2017 07:54

Re: Hook Entity Removal/Deletion/Killing
 
Quote:

Originally Posted by HamletEagle (Post 2532809)
I did not look at the code, so could you explain how it's different than using a trie and saving data with entity id as key?

You don't have to worry about other plugins using the same entvar for storing data. Other than that, probably that it's just straight forward and more intuitive with proper native names.


All times are GMT -4. The time now is 23:11.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.