AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Solved cs_get_weapon_ammo returning "Non-player entity" (https://forums.alliedmods.net/showthread.php?t=333807)

Shadows Adi 08-07-2021 01:31

cs_get_weapon_ammo returning "Non-player entity"
 
Hello,
Today I tried out to retrieve player's weapon ammo inside FM_PlaybackEvent but every time I ran into this error:
PHP Code:

[CSTRIKENon-player entity 1 out of range 

Code:
PHP Code:

public fw_PlaybackEvent(flagsiPlayereventidFloat:delayFloat:origin[3], Float:angles[3], Float:fparam1Float:fparam2iParam1iParam2bParam1bParam2)
{
    if(!
is_user_connected(iPlayer) || !pev_valid(iPlayer))
        return;

    
server_print("Entities: %d/%d"entity_count(), global_get(glb_maxEntities))
    static 
iClip;

    
iClip cs_get_weapon_ammo(iPlayer);


http://www.amxmodx.org/api/cstrike/cs_get_weapon_ammo

PHP Code:

index    
Weapon entity index 

In API docs it's specified that the index should be a weapon entity, but after looking in the core code I saw that is not right; Index param should be player's index:
PHP Code:

// native cs_get_weapon_ammo(index);
static cell AMX_NATIVE_CALL cs_get_weapon_ammo(AMX *amxcell *params)
{
    
GET_OFFSET("CBasePlayerWeapon"m_iClip);

    
int index params[1];

    
CHECK_NONPLAYER(index);
    
edict_t *pWeapon TypeConversion.id_to_edict(index);

    return 
get_pdata<int>(pWeaponm_iClip);
}

#define CHECK_NONPLAYER(x) \
    
if (|| <= gpGlobals->maxClients || gpGlobals->maxEntities) { \
        
MF_LogError(amxAMX_ERR_NATIVE"Non-player entity %d out of range"x); \
        return 
0; \
    } else { \
        if (
FNullEnt(TypeConversion.id_to_edict(x))) { \
            
MF_LogError(amxAMX_ERR_NATIVE"Invalid non-player entity %d"x); \
            return 
0; \
        } \
    } 

It said that "Non-player entity 1 out of range" which is not right at all because player is connected to server and it can't exceed MaxEntities according to my debugging:
PHP Code:

server_print("Entities: %d/%d"entity_count(), global_get(glb_maxEntities))

OutputEntities126/1365 


DJEarthQuake 08-07-2021 02:35

Re: cs_get_weapon_ammo returning "Non-player entity"
 
Code:
stock weapon_details(id) {     wpnid = get_user_weapon(id, magazine, ammo);     return wpnid, magazine, ammo; }

HamletEagle 08-07-2021 03:14

Re: cs_get_weapon_ammo returning "Non-player entity"
 
No, it shouldn't. Pass the weapon ENTITY index. CHECK_NONPLAYER checks to make sure the entity you passed is not a player. The error message is correct, you passed id = 1 whicb is a player id and the native does not want that.

Based on "eventid" you know for which weapon the forward was triggered(you can deduce the CSW_* index). With the csw index you can retrieve the weapon entity index.

Also, what is "iPlayer"? In the forward header you have "id".

Shadows Adi 08-07-2021 03:26

Re: cs_get_weapon_ammo returning "Non-player entity"
 
Thank you!
I will try what Hamlet said.

And about this
Quote:

Originally Posted by HamletEagle (Post 2754673)
Also, what is "iPlayer"? In the forward header you have "id".

Changed the code:D

Solved passing the Weapon Entity index retrieved from player's pdata:
PHP Code:

    static wID 
    wID 
get_pdata_cbase(iPlayer373XO_PLAYER)

    
iClip cs_get_weapon_ammo(wID); 


DJEarthQuake 08-07-2021 09:42

Re: cs_get_weapon_ammo returning "Non-player entity"
 
I wasn't paying careful enough attention. Thought you needed stronger filter. That just made matters worse! Added more relevant code. A stock I use to get ammo from player's index.


All times are GMT -4. The time now is 02:32.

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