Raised This Month: $51 Target: $400
 12% 

Trouble with AddPlayerItem


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
bibu
Veteran Member
Join Date: Sep 2010
Old 11-23-2013 , 05:48   Trouble with AddPlayerItem
Reply With Quote #1

I am having some troubles with Ham's AddPlayerItem. In the following code, I test it on dust as T and using only the first chat print, this gets called 3 times.

- knife
- glock
- c4

But after trying to filter some weapons out with cs_get_weapon_id, it all acts differently. The default function gets like printed more than 10 times.

See this output:

PHP Code:
knifeUSP or C4 added.
some other weapon blocked.
knifeUSP or C4 added.
some other weapon blocked.
some other weapon blocked.
some other weapon blocked.
some other weapon blocked.
some other weapon blocked.
some other weapon blocked.
some other weapon blocked
The first 3 prints are right I assume, since the glock also gets blocked, but what on earth are those other weapons which get blocked?

Test yourself on de_dust as T:

PHP Code:
#include <amxmodx>
#include <cstrike>
#include <hamsandwich>

public plugin_init()
    
RegisterHam(Ham_AddPlayerItem"player""AddPlayerItem"

public 
AddPlayerItem(idiWeapon)
{
    
// Using only the print below, it all works fine. And gets called three times on t spawn, knife + glock + c4
    //client_print(id, print_chat, "weapon added.")
        
    
switch(cs_get_weapon_id(iWeapon))
    {
        
// Allow getting knife, c4, and usp into the inventory.
        
case CSW_KNIFECSW_USPCSW_C4:
        {
            
client_print(idprint_chat"knife, USP or C4 added.")
            return 
HAM_IGNORED
        
}
        
// All other weapons can't be added.
        
default:
        {
            
client_print(idprint_chat"some other weapon blocked.")
            
SetHamReturnInteger(false//dont know if this is needed?
            
return HAM_SUPERCEDE
        

    }
    return 
HAM_IGNORED

__________________
Selling tons of my own private works.
Accepting paid work for clans and communities.
Don't hesitate to contact me.
bibu is offline
ConnorMcLeod
Veteran Member
Join Date: Jul 2006
Location: France (95)
Old 11-23-2013 , 12:32   Re: Trouble with AddPlayerItem
Reply With Quote #2

Why don't you debug it like this ?

PHP Code:
public AddPlayerItem(idiWeapon)
{
    
// Using only the print below, it all works fine. And gets called three times on t spawn, knife + glock + c4
    //client_print(id, print_chat, "weapon added.")

    
new iId cs_get_weapon_id(iWeapon);
    switch( 
iId )
    {
        
// Allow getting knife, c4, and usp into the inventory.
        
case CSW_KNIFECSW_USPCSW_C4// you don't check for glock18 ?
        
{
            
client_print(idprint_chat"knife, USP or C4 added.")
            return 
HAM_IGNORED
        
}
        
// All other weapons can't be added.
        
default:
        {
            new class[
32];
            
pev(iWeaponpev_classname, class, charsmax(class));
            
client_print(idprint_chat"some other weapon blocked. iId=%d classname=%s"iId, class)
            
SetHamReturnInteger(false//dont know if this is needed?
            
return HAM_SUPERCEDE
        
}
    }
    return 
HAM_IGNORED

__________________
- tired and retired -

- my plugins -
ConnorMcLeod is offline
bibu
Veteran Member
Join Date: Sep 2010
Old 11-23-2013 , 13:08   Re: Trouble with AddPlayerItem
Reply With Quote #3

Quote:
knife, USP or C4 added.
some other weapon blocked. iId=17 classname=weapon_glock18
knife, USP or C4 added.
some other weapon blocked. iId=17 classname=weapon_glock18
some other weapon blocked. iId=17 classname=weapon_glock18
some other weapon blocked. iId=17 classname=weapon_glock18
some other weapon blocked. iId=17 classname=weapon_glock18
some other weapon blocked. iId=17 classname=weapon_glock18
some other weapon blocked. iId=17 classname=weapon_glock18
some other weapon blocked. iId=17 classname=weapon_glock18
// and much more...
Somehow then, I get glock like more than 20 times, how is that possible?
__________________
Selling tons of my own private works.
Accepting paid work for clans and communities.
Don't hesitate to contact me.

Last edited by bibu; 11-23-2013 at 13:08.
bibu is offline
ConnorMcLeod
Veteran Member
Join Date: Jul 2006
Location: France (95)
Old 11-23-2013 , 16:45   Re: Trouble with AddPlayerItem
Reply With Quote #4

No idea, nobody knows what you are running on your server.
It doesn't seem to be default game behavior.
__________________
- tired and retired -

- my plugins -
ConnorMcLeod is offline
bibu
Veteran Member
Join Date: Sep 2010
Old 11-23-2013 , 17:15   Re: Trouble with AddPlayerItem
Reply With Quote #5

Quote:
Originally Posted by ConnorMcLeod View Post
No idea, nobody knows what you are running on your server.
It doesn't seem to be default game behavior.
Default plugins no other 3rd party plugins lol. Did you test on yourself?
__________________
Selling tons of my own private works.
Accepting paid work for clans and communities.
Don't hesitate to contact me.
bibu is offline
ConnorMcLeod
Veteran Member
Join Date: Jul 2006
Location: France (95)
Old 11-23-2013 , 20:26   Re: Trouble with AddPlayerItem
Reply With Quote #6

Oh, i had forgotten that, you need to remove weapon, else it gonna be touch few times before it is removed.

PHP Code:
            set_pev(iWeaponpev_flagsFL_KILLME);
            
SetHamReturnInteger(false);
            return 
HAM_SUPERCEDE
You can check iWeapon value, it should be the same on all calls.
__________________
- tired and retired -

- my plugins -
ConnorMcLeod is offline
bibu
Veteran Member
Join Date: Sep 2010
Old 11-24-2013 , 07:35   Re: Trouble with AddPlayerItem
Reply With Quote #7

Quote:
Originally Posted by ConnorMcLeod View Post
Oh, i had forgotten that, you need to remove weapon, else it gonna be touch few times before it is removed.
Wow, this helped, thank you. Could you answer me why such function require setting return to false and Ham_Touch to block weapon pickup not. On that hook superceding is just fine.
__________________
Selling tons of my own private works.
Accepting paid work for clans and communities.
Don't hesitate to contact me.
bibu is offline
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 11-24-2013 , 08:22   Re: Trouble with AddPlayerItem
Reply With Quote #8

When you hook a function, superceding means you don't run the original function code but function is still hooked and you need to return a value. By default, Ham sets to 0. Therefore in this case, SetHamReturnInteger is not necessary, though adding it won't hurt (readability, consistency reasons).

A touch function is called several times by frame.
When you pickup a weapon, you are going to hit most likely such function :
PHP Code:
void CBasePlayerItem::DefaultTouchCBaseEntity *pOther )
{
        
// if it's not a player, ignore
        
if ( !pOther->IsPlayer() )
                return;

        
CBasePlayer *pPlayer = (CBasePlayer *)pOther;

        
// can I have this?
        
if ( !g_pGameRules->CanHavePlayerItempPlayerthis ) )
        {
                if ( 
gEvilImpulse101 )
                {
                        
UTIL_Removethis );
                }
                return;
        }

        if (
pOther->AddPlayerItemthis ))
        {
                
AttachToPlayerpPlayer );
                
EMIT_SOUND(ENT(pPlayer->pev), CHAN_ITEM"items/gunpickup2.wav"1ATTN_NORM);
        }

        
SUB_UseTargetspOtherUSE_TOGGLE); // UNDONE: when should this happen?


It means that if you want to supercede AddPlayerItem and you don't return 0, the code inside the check will be called. It's not what you want.
Also, since a touch function called several times in a row, and the weapon entity still existing, your callback is going to be called few times.
That's why you need, in this case, to tell the game this entity needs to be removed.
__________________

Last edited by Arkshine; 11-24-2013 at 08:25.
Arkshine 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 07:26.


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