Raised This Month: $ Target: $400
 0% 

Hook Entity Removal/Deletion/Killing


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Depresie
Veteran Member
Join Date: Nov 2013
Old 06-30-2017 , 13:57   Hook Entity Removal/Deletion/Killing
Reply With Quote #1

Is there any method to hook the moment when an entity is deleted from the game/map ?
__________________

Last edited by Depresie; 06-30-2017 at 14:17.
Depresie is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 06-30-2017 , 14:22   Re: Hook Entity Removal/Deletion/Killing
Reply With Quote #2

The forward passes the entity index.
__________________

Last edited by HamletEagle; 06-30-2017 at 14:22.
HamletEagle is offline
Depresie
Veteran Member
Join Date: Nov 2013
Old 06-30-2017 , 14:23   Re: Hook Entity Removal/Deletion/Killing
Reply With Quote #3

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 ?
__________________

Last edited by Depresie; 06-30-2017 at 14:25.
Depresie is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 06-30-2017 , 16:38   Re: Hook Entity Removal/Deletion/Killing
Reply With Quote #4

Quote:
Originally Posted by Depresie View Post
1. is there any empty field on entities to store strings ? like pev_iuser1 ??
You can use the Custom Entity Data module.
klippy is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 07-01-2017 , 08:06   Re: Hook Entity Removal/Deletion/Killing
Reply With Quote #5

Quote:
Originally Posted by KliPPy View Post
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 View Post
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.
__________________
HamletEagle is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 07-02-2017 , 07:54   Re: Hook Entity Removal/Deletion/Killing
Reply With Quote #6

Quote:
Originally Posted by HamletEagle View Post
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.
klippy is offline
PRoSToTeM@
Veteran Member
Join Date: Jan 2010
Location: Russia, Ivanovo
Old 07-02-2017 , 19:20   Re: Hook Entity Removal/Deletion/Killing
Reply With Quote #7

Quote:
Originally Posted by HamletEagle View Post
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?
It also handles entity remove.
Quote:
Originally Posted by HamletEagle View Post
Because if there is no practical advantage over a trie I would not create unneded dependencies.
1. Readability - get/set functions, but you can also create them in your local implementation.
2. Error prone. But looks like that entity data implementation isn't complicated. (but, for example, player tasks in amxx are error prone, because even skilled amxx scripters sometimes forget to remove them on disconnect and other events)
__________________

Last edited by PRoSToTeM@; 07-02-2017 at 19:30.
PRoSToTeM@ is offline
Send a message via ICQ to PRoSToTeM@ Send a message via Skype™ to PRoSToTeM@
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 06-30-2017 , 15:47   Re: Hook Entity Removal/Deletion/Killing
Reply With Quote #8

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

Last edited by HamletEagle; 06-30-2017 at 15:48.
HamletEagle is offline
NiHiLaNTh
Way Past Expiration
Join Date: May 2009
Location: Latvia
Old 06-30-2017 , 15:47   Re: Hook Entity Removal/Deletion/Killing
Reply With Quote #9

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

NiHiLaNTh is offline
Send a message via Skype™ to NiHiLaNTh
Depresie
Veteran Member
Join Date: Nov 2013
Old 06-30-2017 , 16:03   Re: Hook Entity Removal/Deletion/Killing
Reply With Quote #10

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 ?
__________________
Depresie is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


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


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