Raised This Month: $ Target: $400
 0% 

Solved Get weapon's entity id, from weapon ID


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
GasmoN
Member
Join Date: Jul 2014
Old 12-08-2020 , 17:29   Get weapon's entity id, from weapon ID
Reply With Quote #1

At the end of every round, I want to loop through all players and their weapons. And remove weapons flagged with certial flag. For that, I need entity ID from all their weapons, so how can I retreive it?

PHP Code:
    new i;
    new 
weapons[32], iWeapons
    get_user_weapons
(idweaponsiWeapons)
    
    for (
0<= iWeaponsi++)
    {      
             
// How to get Entity ID here for all weapons that player have in inventory?
            // Something like new iEnt = get_weapon_ent_id(iWeapons[i])
    


Last edited by GasmoN; 12-09-2020 at 11:17. Reason: Solved
GasmoN is offline
Natsheh
Veteran Member
Join Date: Sep 2012
Old 12-08-2020 , 18:37   Re: Get weapon's entity id, from weapon ID
Reply With Quote #2

Engine
Code:
get_player_weapon_entity(id, CSW_ID) {     static ent, szClassname[24]; ent = -1;     get_weaponname(CSW_ID, szClassname, charsmax(szClassname));     while ( (ent=find_ent_by_class(ent, szClassname)) > 0 && entity_get_edict(ent, EV_ENT_owner) != id ) { }         return ent; }


Or most common sense.

Code:
ent = find_ent_by_owner(-1, "weapon_class", owner);

or
Hamsandwich

PHP Code:
new g_user_weapon_ent_pointer[32][32];

public 
plugin_init()
{
    
    for ( new 
i=CSW_P228<= CSW_P90i++)
    {
        if(
get_weaponname(iszWpncharsmax(szWpn)))
        {
            
RegisterHam(Ham_Item_AddToPlayerszWpn"fw_weapon_addtoplayer_post"true);
            
RegisterHam(Ham_Item_KillszWpn"fw_weapon_kill_post"true);
        }
    }
}


public 
fw_weapon_addtoplayer_post(entplayer)
{
    static 
szClassname[32];
    
entity_get_string(entEV_SZ_classnameszClassnamecharsmax(szClassname));
    
g_user_weapon_ent_pointer[player 1][ get_weaponid(szClassname)-] = ent;
}

public 
fw_weapon_kill_post(ent)
{
    new 
player entity_get_edict(entEV_ENT_owner);
    
    static 
szClassname[32];
    
entity_get_string(entEV_SZ_classnameszClassnamecharsmax(szClassname));
    
    
g_user_weapon_ent_pointer[player 1][ get_weaponid(szClassname)-] = 0;

__________________
@Jailbreak Main Mod v2.7.0 100%
@User Tag Prefix 100% done !
@Mystery Box 100% done !
@VIP System 100% done !


Last edited by Natsheh; 12-09-2020 at 12:55. Reason: Code updated
Natsheh is offline
Send a message via MSN to Natsheh Send a message via Skype™ to Natsheh
GasmoN
Member
Join Date: Jul 2014
Old 12-09-2020 , 11:16   Re: Get weapon's entity id, from weapon ID
Reply With Quote #3

Thank you, this is how I fixed this

At the end of every round, I added pf_check_weapon_life()
If anyone know any better way, I would like to hear it.
PHP Code:
public zp_fw_gamemodes_end() // Round just ended.
{
    
pf_check_weapon_life();
}

stock pf_check_weapon_life()
{
    new 
i;
    
    for(
1<= g_maxplayersi++)
    {
        if(
is_user_alive(i))
        {
            
pf_check_user_inventory(i);
        }
    }
}

stock pf_check_user_inventory(id
{
    new 
weapons[32], num
    get_user_weapons
(idweaponsnum)
    
    for (new 
0numi++) 
    {
        if (
weapons[i] > 0
        {
            new 
iEnt pf_find_weapon_ent_id(idweapons[i]);
            new 
iEntLife entity_get_int(iEntENT_INDENT_LIFE);
            
            switch(
iEntLife)
            {
                case 
ENT_KEY_LIFE_ONEROUND:
                {
                    
fm_strip_user_gun(idweapons[i]);
                }
            }            
        }
    }
}

stock pf_find_weapon_ent_id(idCSW_ID)
{
    static 
entszClassname[24]; ent = -1;
    
get_weaponname(CSW_IDszClassnamecharsmax(szClassname));
    
    while ((
ent find_ent_by_class(entszClassname)) > && entity_get_edict(entEV_ENT_owner) != id) { }
    return (
ent && entity_get_edict(entEV_ENT_owner) == id) ? ent:0;


Last edited by GasmoN; 12-09-2020 at 11:17.
GasmoN is offline
Natsheh
Veteran Member
Join Date: Sep 2012
Old 12-09-2020 , 12:52   Re: Get weapon's entity id, from weapon ID
Reply With Quote #4

PHP Code:
stock pf_find_weapon_ent_id(idCSW_ID)
{
    static 
entszClassname[24]; ent = -1;
    
get_weaponname(CSW_IDszClassnamecharsmax(szClassname));
    
    while ((
ent find_ent_by_class(entszClassname)) > && entity_get_edict(entEV_ENT_owner) != id) { }
    return 
ent;

it will be more common sense and faster to use..

Code:
stock pf_find_weapon_ent_id(id, CSW_ID) {     static szClassname[24];     get_weaponname(CSW_ID, szClassname, charsmax(szClassname));     return find_ent_by_owner(-1, szClassname, id); }

since i forgot about the find_ent_by_owner native.
__________________
@Jailbreak Main Mod v2.7.0 100%
@User Tag Prefix 100% done !
@Mystery Box 100% done !
@VIP System 100% done !


Last edited by Natsheh; 12-11-2020 at 15:43.
Natsheh is offline
Send a message via MSN to Natsheh Send a message via Skype™ to Natsheh
GasmoN
Member
Join Date: Jul 2014
Old 12-11-2020 , 15:12   Re: Get weapon's entity id, from weapon ID
Reply With Quote #5

Quote:
Originally Posted by GasmoN View Post
Thank you, this is how I fixed this

At the end of every round, I added pf_check_weapon_life()
If anyone know any better way, I would like to hear it.
PHP Code:
public zp_fw_gamemodes_end() // Round just ended.
{
    
pf_check_weapon_life();
}

stock pf_check_weapon_life()
{
    new 
i;
    
    for(
1<= g_maxplayersi++)
    {
        if(
is_user_alive(i))
        {
            
pf_check_user_inventory(i);
        }
    }
}

stock pf_check_user_inventory(id
{
    new 
weapons[32], num
    get_user_weapons
(idweaponsnum)
    
    for (new 
0numi++) 
    {
        if (
weapons[i] > 0
        {
            new 
iEnt pf_find_weapon_ent_id(idweapons[i]);
            new 
iEntLife entity_get_int(iEntENT_INDENT_LIFE);
            
            switch(
iEntLife)
            {
                case 
ENT_KEY_LIFE_ONEROUND:
                {
                    
fm_strip_user_gun(idweapons[i]);
                }
            }            
        }
    }
}

stock pf_find_weapon_ent_id(idCSW_ID)
{
    static 
entszClassname[24]; ent = -1;
    
get_weaponname(CSW_IDszClassnamecharsmax(szClassname));
    
    while ((
ent find_ent_by_class(entszClassname)) > && entity_get_edict(entEV_ENT_owner) != id) { }
    return (
ent && entity_get_edict(entEV_ENT_owner) == id) ? ent:0;

Okay, so just a quick update. This started crashing server if there is more than 2 players. So after a while, I found another solution (I am writing this just in case someone need something like this).
The stock below is tested with 32/32 players while all of them had weapon that will be removed at the end of the round.

PHP Code:
stock pf_check_weapon_life()
{
    new 
iEntCount entity_count();
    new 
iEntOwner;
    new 
iEntWeaponID;
    new 
iEnt;
    
    for (
iEnt 0iEnt iEntCountiEnt++)
    {
        if(!
is_valid_ent(iEnt))
        continue;
        
        
iEntOwner entity_get_edict(iEntEV_ENT_owner)
        
        if(
is_user_alive(iEntOwner))
        {
            switch(
entity_get_int(iEntENT_INDENT_LIFE))
            {
                case 
ENT_KEY_LIFE_ONEROUND:
                {
                    
iEntWeaponID cs_get_weapon_id(iEnt);
                    
                    if(
get_pdata_cbase(iEntOwnerm_pActiveItem) == iEnt)
                    
ExecuteHamB(Ham_Weapon_RetireWeaponiEnt);
                    
                    if(
ExecuteHamB(Ham_RemovePlayerItemiEntOwneriEnt))
                    {
                        
user_has_weapon(iEntOwneriEntWeaponID0);
                        
ExecuteHamB(Ham_Item_KilliEnt);
                    }                    
                }
            }
        }
    }

GasmoN is offline
Natsheh
Veteran Member
Join Date: Sep 2012
Old 12-11-2020 , 15:37   Re: Get weapon's entity id, from weapon ID
Reply With Quote #6

previous method was much better you just now ruined it.

if it crashes thats means there's something going wrong in the code, debug and test when the crash acquires.


if you want to loop all players alive use this method its much faster

Code:
new players[32], pnum; get_players(players, pnum, "ah"); // flag a to loop alive clients only, flag h is to skip HLTV for(new i ; i < pnum; i++ ) {    new player_index = players[ i ]; }

also you need to show what's the body of fm_strip_user_gun(id, weapons[i]); and how it works...
__________________
@Jailbreak Main Mod v2.7.0 100%
@User Tag Prefix 100% done !
@Mystery Box 100% done !
@VIP System 100% done !


Last edited by Natsheh; 12-11-2020 at 15:42.
Natsheh is offline
Send a message via MSN to Natsheh Send a message via Skype™ to Natsheh
GasmoN
Member
Join Date: Jul 2014
Old 12-11-2020 , 16:29   Re: Get weapon's entity id, from weapon ID
Reply With Quote #7

Before I was using one loop, and 2 other loops inside that one loop.
Now, I only loop entities with maximum number of created entities and check some conditions.
Why is that ruined?

fm_strip_user_gun already comes with fakemeta_util, I didn't looked up the stock itself how it's done. https://www.amxmodx.org/api/fakemeta...strip_user_gun
GasmoN is offline
Natsheh
Veteran Member
Join Date: Sep 2012
Old 12-11-2020 , 17:43   Re: Get weapon's entity id, from weapon ID
Reply With Quote #8

Quote:
Originally Posted by GasmoN View Post
Before I was using one loop, and 2 other loops inside that one loop.
Now, I only loop entities with maximum number of created entities and check some conditions.
Why is that ruined?

fm_strip_user_gun already comes with fakemeta_util, I didn't looked up the stock itself how it's done. https://www.amxmodx.org/api/fakemeta...strip_user_gun
there's nothing wrong by using two loops inside of each other and also i told you to update the code and use the other way, which its this way, the looping is done in the module which is much faster.

Quote:
Originally Posted by Natsheh
PHP Code:
stock pf_find_weapon_ent_id(idCSW_ID)
{
    static 
szClassname[24];
    
get_weaponname(CSW_IDszClassnamecharsmax(szClassname));
    return 
find_ent_by_owner(-1szClassnameid);

and how's it better by looping all the existed entities ?
__________________
@Jailbreak Main Mod v2.7.0 100%
@User Tag Prefix 100% done !
@Mystery Box 100% done !
@VIP System 100% done !


Last edited by Natsheh; 12-11-2020 at 17:50.
Natsheh is offline
Send a message via MSN to Natsheh Send a message via Skype™ to Natsheh
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:00.


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