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

Check if Player already owns a weapon


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
wmchris
Senior Member
Join Date: Sep 2008
Old 04-25-2010 , 21:46   Check if Player already owns a weapon
Reply With Quote #1

Hey,
i'm trying to give a player a new weapon using sourcepawn for HL2DM

How can i check if the player already owns the weapon, so i could give him for example instead of "weapon_pistol" an "item_ammo_pistol_large"?
wmchris is offline
exvel
SourceMod Donor
Join Date: Jun 2006
Location: Russia
Old 04-26-2010 , 00:05   Re: Check if Player already owns a weapon
Reply With Quote #2

GetClientWeapon
__________________
For admins: My plugins

For developers: Colors library
exvel is offline
Send a message via ICQ to exvel
miniman
Senior Member
Join Date: Aug 2009
Location: Israel
Old 04-26-2010 , 01:22   Re: Check if Player already owns a weapon
Reply With Quote #3

Quote:
Originally Posted by exvel View Post
It doesn't work well on HL2DM because you can have some weapons on the same slot , it can give you only the first slot the another solution is scan m_hMyWeapons , btw instead of giving the ammo box you can just set the ammo with m_iAmmo also you have the offest GiveAmmo.
miniman is offline
wmchris
Senior Member
Join Date: Sep 2008
Old 04-26-2010 , 07:18   Re: Check if Player already owns a weapon
Reply With Quote #4

i already tried searching with m_hMyWeapon, but dunno why - it doesn't work (and in a thread here its prooved not to work even with flawless code)
wmchris is offline
bl4nk
SourceMod Developer
Join Date: Jul 2007
Old 04-26-2010 , 17:23   Re: Check if Player already owns a weapon
Reply With Quote #5

Quote:
Originally Posted by wmchris View Post
i already tried searching with m_hMyWeapon, but dunno why - it doesn't work (and in a thread here its prooved not to work even with flawless code)
Could you post the thread? It's also "m_hMyWeapons", which could have been the problem.
bl4nk is offline
berni
SourceMod Plugin Approver
Join Date: May 2007
Location: Austria
Old 04-26-2010 , 20:15   Re: Check if Player already owns a weapon
Reply With Quote #6

tach wmchris,

I found this in my awesome smlib:

PHP Code:
stock HasClientWeapon(client,String:weaponClassname[]){
    
    if(!
Client_IsPlayer(client) || !IsClientInGame(client) || !IsPlayerAlive(client)){
        return -
1;
    }
    
    new 
offset FindSendPropOffs("CHL2MP_Player""m_hMyWeapons");
    
    if(
offset 0){
        return -
1;
    }
    
    new 
entity = -1;
    new 
String:classname[32];
    
    for(new 
offset_add=0;offset_add<256;offset_add+=4) {
        
        
entity GetWeaponClassnameByOffset(client,offset_add,classname,sizeof(classname));
        
        if((
entity 1) || !IsValidEntity(entity)){
            return -
1;
        }
        
        if(
StrEqual(weaponClassname,classname,false)){
            return 
entity;
        }
    }
    return -
1;
    

Can't gurantee it works though, as i haven't written it myself, if it does not give me a ping and I will fix/rewrite it. feel free to msg me anytime ;)


This removes all weapons from a player btw.:

PHP Code:
stock RemoveAllWeapons(client,String:playernetclass[]) {
    
    if(!
IsClientInGame(client) || !IsPlayerAlive(client) || !Client_IsPlayer(client)){
        return -
1;
    }
    
    new 
offset_PlayerWeapons FindSendPropOffs(playernetclass"m_hMyWeapons");
    
    if(
offset_PlayerWeapons == -1){
        
LogError("[SMLIB] Error: Can't get m_hMyWeapons from client '%N' clientindex: %d offset: %d. Wrong playernetclass?",client,offset_PlayerWeapons);
        return -
1;
    }
    
    new 
entity;
    new 
removedweapons 0;
    new 
bool:success;
    
    for (new 
offset=0;offset<128;offset+=4){
        
        
entity GetEntDataEnt2(clientoffset_PlayerWeapons+offset);
        
        if (
IsValidEdict(entity) && IsValidEntity(entity)){
            
            
success RemovePlayerItem(client,entity);
            if(!
success){continue;}
            
RemoveEdict(entity);
            
removedweapons++;
        }
    }
    
    return 
removedweapons;

__________________
Why reinvent the wheel ? Download smlib with over 350 useful functions.

When people ask me "Plz" just because it's shorter than "Please" I feel perfectly justified to answer "No" because it's shorter than "Yes"
powered by Core i7 3770k | 32GB DDR3 1886Mhz | 2x Vertex4 SSD Raid0

Last edited by berni; 04-26-2010 at 20:18.
berni is offline
wmchris
Senior Member
Join Date: Sep 2008
Old 04-27-2010 , 01:08   Re: Check if Player already owns a weapon
Reply With Quote #7

it was indeed the error with the missing s (ashamed)

bernie your implemention looks very good. i think i'll use your code instead of mine. thanks
wmchris 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 22:35.


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