Raised This Month: $186 Target: $400
 46% 

Solved [ H3LP ] Check if player has any weapons


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
DarthMan
Veteran Member
Join Date: Aug 2011
Old 05-17-2018 , 07:42   [ H3LP ] Check if player has any weapons
Reply With Quote #1

Hello. I'm trying to loop through all weapon slots to check if player has any weapon but if ti has no weapon, the plugin crashes with message Run time error 4: index out of bounds. Any ideas?

Code:
bool: HasAnyWeapon(iID) {     new bool: bHasWeapons, iWeapon;         if(is_user_alive(iID))     {         for(new i = 0; i < MAX_WEAPON_SLOTS + 1; i++)         {             iWeapon = get_pdata_cbase(iID, m_rgpPlayerItems[i], XO_PLAYER);                         if(!pev_valid(iWeapon))                 continue;                         if(!bHasWeapons)             {                 bHasWeapons = true;                 break;             }         }     }         return bHasWeapons; }

Last edited by DarthMan; 05-17-2018 at 12:54. Reason: Solved
DarthMan is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 05-17-2018 , 07:54   Re: [ H3LP ] Check if player has any weapons
Reply With Quote #2

Is the specific slot important? If no, just check if pev_weapons is not zero. You can add things to an exclude bitfield, such as knife or c4 that you do not care about.
__________________
Bugsy is offline
DarthMan
Veteran Member
Join Date: Aug 2011
Old 05-17-2018 , 08:01   Re: [ H3LP ] Check if player has any weapons
Reply With Quote #3

Quote:
Originally Posted by Bugsy View Post
Is the specific slot important? If no, just check if pev_weapons is not zero. You can add things to an exclude bitfield, such as knife or c4 that you do not care about.
Well, it somehow is because what if the player had no weapons, he picked a weapon but didn't change to it which means that his weapon still equals to 0? On CS it looks like when u pick a weapon it automatically switches to it but that's for example not the case on HL1 let's say if the auto switch game cvar is disabled.

Last edited by DarthMan; 05-17-2018 at 08:02.
DarthMan is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 05-17-2018 , 10:24   Re: [ H3LP ] Check if player has any weapons
Reply With Quote #4

Show MAX_WEAPON_SLOTS's value.
__________________
HamletEagle is offline
DarthMan
Veteran Member
Join Date: Aug 2011
Old 05-17-2018 , 12:21   Re: [ H3LP ] Check if player has any weapons
Reply With Quote #5

Quote:
Originally Posted by HamletEagle View Post
Show MAX_WEAPON_SLOTS's value.
It's 6.
Also, new const m_rgpPlayerItems[MAX_WEAPON_SLOTS] = { 343, 344, ... }; // for Half-Life : Opposing Force

Last edited by DarthMan; 05-17-2018 at 12:21.
DarthMan is offline
KliPPy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 05-17-2018 , 12:29   Re: [ H3LP ] Check if player has any weapons
Reply With Quote #6

Code:
i < MAX_WEAPON_SLOTS + 1
I believe you don't need that + 1.
__________________
KliPPy is offline
DarthMan
Veteran Member
Join Date: Aug 2011
Old 05-17-2018 , 12:54   Re: [ H3LP ] Check if player has any weapons
Reply With Quote #7

Quote:
Originally Posted by KliPPy View Post
Code:
i < MAX_WEAPON_SLOTS + 1
I believe you don't need that + 1.
That was it, many thanks ! I tought that I needed + 1 beucase there are slots from 1 to 7 so that the loop will end at 6. But I guess since slots are actually starting from 0 and slot1 is like 0 the - 1 wasn't needed. Was a bit confused due to how the binds were in the game.
DarthMan is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 05-17-2018 , 18:17   Re: [ H3LP ] Check if player has any weapons
Reply With Quote #8

pev_weapons does not contain the currently held weapon. It contains a bit-sum of the players inventory of weapons. Here's an example for getting inventory out of pev_weapons, ignoring knife and c4.

PHP Code:
#include <amxmodx>
#include <fakemeta>

const Ignore_Weapons = ( ( << CSW_KNIFE ) | ( << CSW_C4 ) );

public 
plugin_init() 
{
    
register_clcmd"say a" "ShowPevWeapons" );
}

public 
ShowPevWeaponsid )
{
    new 
szWeapon19 ] , iWeapons = ( pevid pev_weapons ) & ~Ignore_Weapons );
    
    for ( new 
CSW_P228 <= CSW_P90 i++ )
    {
        if ( 
iWeapons & ( << ) )
        {
            
get_weaponnameszWeapon charsmaxszWeapon ) );
            
client_printid print_chat szWeapon );
        }
    }

__________________

Last edited by Bugsy; 05-17-2018 at 18:17.
Bugsy is offline
DarthMan
Veteran Member
Join Date: Aug 2011
Old 05-17-2018 , 18:53   Re: [ H3LP ] Check if player has any weapons
Reply With Quote #9

Quote:
Originally Posted by Bugsy View Post
pev_weapons does not contain the currently held weapon. It contains a bit-sum of the players inventory of weapons. Here's an example for getting inventory out of pev_weapons, ignoring knife and c4.

PHP Code:
#include <amxmodx>
#include <fakemeta>

const Ignore_Weapons = ( ( << CSW_KNIFE ) | ( << CSW_C4 ) );

public 
plugin_init() 
{
    
register_clcmd"say a" "ShowPevWeapons" );
}

public 
ShowPevWeaponsid )
{
    new 
szWeapon19 ] , iWeapons = ( pevid pev_weapons ) & ~Ignore_Weapons );
    
    for ( new 
CSW_P228 <= CSW_P90 i++ )
    {
        if ( 
iWeapons & ( << ) )
        {
            
get_weaponnameszWeapon charsmaxszWeapon ) );
            
client_printid print_chat szWeapon );
        }
    }

Thanks, I think this could be useful for my CS server to ignore stripping the knife :-)
DarthMan is offline
Reply


Thread Tools
Display Modes

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 17:34.


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