AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Problem with Ham_Item_Deploy and _cl_autowepswitch 1 (https://forums.alliedmods.net/showthread.php?t=251129)

Aydeev 11-06-2014 10:44

Problem with Ham_Item_Deploy and _cl_autowepswitch 1
 
Hello,

I've been using the folowing code to get a player's id when a weapon deploy is registered

PHP Code:

...
RegisterHam(Ham_Item_DeployszWeaponName"Ham_Item_Deploy_hook"0)
...

public 
Ham_Item_Deploy_hook(ent)
{

new 
userid pev(entpev_owner)

// working with the user id
//if (...


When testing with bots i noticed they were causing "index out of bounds" errors on the if statement and after some testing i figured out why:

Apparently it takes some time for pev_owner to update.

If a player using _cl_autowepswitch 1 is picking up a weapon from the ground pev_owner will still be set to the entity id when the function is called. I guess a weapon on the ground is it's own owner and therefore pev_owner = entity id.

So my function will give me invalid user ids.

I've been trying to use set_task(1.0,...) to force a little pause and give it time to update but you can still get index out of bounds if you spam dropping weapons while facing a wall.

Is there any better way to get the player id?

Fr33m@n 11-06-2014 11:17

Re: Problem with Ham_Item_Deploy and _cl_autowepswitch 1
 
PHP Code:

//At the start of plugin
const m_pPlayer 41
const XO_WEAPON 4

...
// Unless you need to modify the output of Ham_Item_Deploy_hook aka returning a different value or blocking it's call (HAM_SUPERCEDE) you should use post
RegisterHam(Ham_Item_DeployszWeaponName"Ham_Item_Deploy_hook"1)
...

public 
Ham_Item_Deploy_hook(ent)
{
    new 
userid get_pdata_cbase(entm_pPlayerXO_WEAPON)

    
//...



Aydeev 11-06-2014 12:16

Re: Problem with Ham_Item_Deploy and _cl_autowepswitch 1
 
I don't understand what's going on there but it works. Thank you :)

HamletEagle 11-06-2014 15:15

Re: Problem with Ham_Item_Deploy and _cl_autowepswitch 1
 
You need to know that pev_owner is filled with the owner id when AttachToPlayer is called, by using the offset it's faster cause it's filled when AddToPlayer get called, which happen earlier than AttachToPlayer

Aydeev 11-06-2014 20:21

Re: Problem with Ham_Item_Deploy and _cl_autowepswitch 1
 
Quote:

Originally Posted by HamletEagle (Post 2221003)
You need to know that pev_owner is filled with the owner id when AttachToPlayer is called, by using the offset it's faster cause it's filled when AddToPlayer get called, which happen earlier than AttachToPlayer

My mistake was only looking at CBasePlayerWeapon, if I had realized CBasePlayerItem was worth checking out too I might have found m_pPlayer myself :)

Thanks

Aydeev 11-15-2014 12:07

Re: Problem with Ham_Item_Deploy and _cl_autowepswitch 1
 
PHP Code:

const m_pPlayer 41
const XO_WEAPON 4

...
//inside the hook function
new userid get_pdata_cbase(entm_pPlayerXO_WEAPON

This works on my local windows test server but not on the main unix server. is there anything wrong with the offset? According to https://wiki.alliedmods.net/CBasePlayerItem_%28CS%29 4 is right

HamletEagle 11-15-2014 12:32

Re: Problem with Ham_Item_Deploy and _cl_autowepswitch 1
 
Can you show as a full code because the snippet you posted nothing is wrong.

Aydeev 11-15-2014 12:41

Re: Problem with Ham_Item_Deploy and _cl_autowepswitch 1
 
Plugin prevents players from shooting with primary weapons if blockshooting flag is set. Works on windows server, but on unix servers players can still shoot. I currently do not have access to the unix servers' logs so I can't check if it throws any error messages

PHP Code:

const m_pPlayer 41
const XO_WEAPON 4
new blockshooting[33]

public 
plugin_init() 

new 
szWeaponName[32]
    new 
NOSHOT_BITSUM = (1<<CSW_KNIFE) | (1<<CSW_HEGRENADE) | (1<<CSW_FLASHBANG) | (1<<CSW_SMOKEGRENADE) | (1<<CSW_ELITE) | (1<<CSW_FIVESEVEN) | (1<<CSW_USP) | (1<<CSW_GLOCK18) | (1<<CSW_DEAGLE) | (1<<CSW_C4)
    for(new 
iId CSW_SCOUTiId <= CSW_P90iId++)
    {
        if( ~
NOSHOT_BITSUM 1<<iId && get_weaponname(iIdszWeaponNamecharsmax(szWeaponName) ))
        {
            
RegisterHam(Ham_Item_DeployszWeaponName"block_shooting"0)
        }        
    }
}

public 
block_shooting(ent)
{
    new 
userid get_pdata_cbase(entm_pPlayerXO_WEAPON)
    
    if(
blockshooting[userid])
    {    
        
set_pdata_floatent469999.0);
        return 
HAM_HANDLED    
    
}
      
    return 
HAM_HANDLED



NiHiLaNTh 11-15-2014 16:40

Re: Problem with Ham_Item_Deploy and _cl_autowepswitch 1
 
Quote:

set_pdata_float( ent, 46, 9999.0, 5 );

it should be 4 not 5, because 5 is diff for player entities. For weapon ents its +4.

Aydeev 11-15-2014 20:26

Re: Problem with Ham_Item_Deploy and _cl_autowepswitch 1
 
What a stupid mistake to make... :cry: I was only looking at the get_pdata_cbase offset and completely forgot the other one

Thanks for your help


All times are GMT -4. The time now is 17:44.

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