AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Solved no chance for me to understand bits (https://forums.alliedmods.net/showthread.php?t=340386)

kww 11-15-2022 09:22

no chance for me to understand bits
 
Hi. Why does this method not work? When I pass any CSW_* constant, it returns me *not* random numbers but they're wrong.
Did I do something wrong?
Testing code:
PHP Code:

#include amxmodx
#include cstrike_const

const CSW_PRIMARY   = (CSW_ALL_SHOTGUNS CSW_ALL_SMGS CSW_ALL_RIFLES CSW_ALL_SNIPERRIFLES CSW_ALL_MACHINEGUNS);
const 
CSW_SECONDARY = (CSW_ALL_PISTOLS);
const 
CSW_GRENADES  = (CSW_ALL_GRENADES);

public 
plugin_init()
{
    new 
slotcachedszWeaponName[32];

    for(new 
wpn CSW_P228wpn <= CSW_P90wpn++)
    {
        
cached wpn;

        if(
get_weaponname(cachedszWeaponNamecharsmax(szWeaponName)))
        {
            
slot get_weapon_slot(cached);
            
console_print(0"%i :: %s :: Slot: %i"cachedszWeaponNameslot);
        }
    }
}

/** I'm asking about this method */
public get_weapon_slot(wpn)
{
    if(
wpn CSW_PRIMARY)   return CS_WEAPONSLOT_PRIMARY;
    if(
wpn CSW_SECONDARY) return CS_WEAPONSLOT_SECONDARY;
    if(
wpn CSW_KNIFE)     return CS_WEAPONSLOT_KNIFE;
    if(
wpn CSW_GRENADES)  return CS_WEAPONSLOT_GRENADE;
    return 
0;


Output:
https://i.ibb.co/MsJTfcZ/image.png

Natsheh 11-15-2022 10:25

Re: no chance for me to understand bits
 
your solution is to shift the wpn depending on the weapon ID

So your function should be as following.

PHP Code:

/** I'm asking about this method */
get_weapon_slot(wpn)
{
    
wpn = (1<<wpn);
    if(
wpn CSW_PRIMARY)   return CS_WEAPONSLOT_PRIMARY;
    if(
wpn CSW_SECONDARY) return CS_WEAPONSLOT_SECONDARY;
    if(
wpn CSW_KNIFE)     return CS_WEAPONSLOT_KNIFE;
    if(
wpn CSW_GRENADES)  return CS_WEAPONSLOT_GRENADE;
    return 
0;



kww 11-15-2022 10:39

Re: no chance for me to understand bits
 
Quote:

Originally Posted by Natsheh (Post 2792860)
your solution is to shift the wpn depending on the weapon ID

So your function should be as following.

PHP Code:

/** I'm asking about this method */
get_weapon_slot(wpn)
{
    
wpn = (1<<wpn);
    if(
wpn CSW_PRIMARY)   return CS_WEAPONSLOT_PRIMARY;
    if(
wpn CSW_SECONDARY) return CS_WEAPONSLOT_SECONDARY;
    if(
wpn CSW_KNIFE)     return CS_WEAPONSLOT_KNIFE;
    if(
wpn CSW_GRENADES)  return CS_WEAPONSLOT_GRENADE;
    return 
0;



bruh... That's it? I forgot it's not shifted. Thank you


All times are GMT -4. The time now is 15:38.

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