How do you check if a player has ANY weapons?
#1

Hi there,

I am wondering how would you check if a player has a weapon of any kind?

I tried doing this

pawn Код:
Check_Weapons(playerid)
{
    new playerWeaponData[2][13];

    for(new i = 0; i < 13; i++)
    {
        GetPlayerWeaponData(playerid, i, playerWeaponData[0][i], playerWeaponData[1][i]);
       
        if(playerWeaponData[0][i] > 1)
        {
            SendClientMessage(playerid,COLOR_BLUE,"has weapons");
            return 1;
        }
    }

    return 0;
}
This code above ^ didn't work.

Thanks for any help provided <3
Reply
#2

pawn Код:
// [ DEVELOPMENT GAMEMODE ]

// INCLUDES:

#include <a_samp>
#include <zcmd>

// DEFINES:

// FUNCTIONS:

#define function%0(%1) forward%0(%1); public%0(%1)

// MAIN:

main()
{
    print("Development Mode: weapon_check.amx");
}

// CALLBACKS:

public OnGameModeInit()
{
    return 1;
}

public OnGameModeExit()
{
    return 1;
}

// COMMANDS:

CMD:weaponcheck(playerid, params[])
{
    if(DoesPlayerHaveWeapons(playerid)) SendClientMessage(playerid, -1, "You have weapons.");
    else SendClientMessage(playerid, -1, "You do not have weapons.");
    return 1;
}

CMD:giveweapon(playerid, params[])
{
    GivePlayerWeapon(playerid, WEAPON_M4, 20);
    return 1;
}

CMD:removeweapons(playerid, params[])
{
    ResetPlayerWeapons(playerid);
    return 1;
}

// FUNCTIONS:

function bool:DoesPlayerHaveWeapons(playerid)
{
    new weapon[13], ammo[13], bool:found = false;
    for(new i = 0; i < 13; i ++)
    {
        GetPlayerWeaponData(playerid, i, weapon[i], ammo[i]);

        if(weapon[i] != 0 && ammo[i] >= 1)
        {
            found = true;
            break;
        }
    }
    return found;
}
Reply
#3

pawn Код:
new playerWeaponData[2][13]; --> new playerWeaponData[13][2];
Reply
#4

You'd write this function in a way likу this:
PHP код:
Check_Weapons(playerid)
{
    new 
weaponidammo;

    for(new 
013i++)
    {
        
GetPlayerWeaponData(playeridiweaponidammo);
        
        if(
weaponid 0// here was > 1 but brass knuckles are weapon too
        
{
            return 
1// No message here
        
}
    }

    return 
0;

And then, where its needed, you can output message if player has any weapon:
PHP код:
if(Check_Weapons(playerid) == 1)
{
    
SendClientMessage(playerid, -1"has weapons");
    return 
1;

Also, I cannot see any problem with your function.
Reply
#5

Thank you @SickAttack and @dominik523

@Sickattack's solution worked
Reply
#6

pawn Код:
forward bool:DoesPlayerHaveWeapons(playerid);
public bool:DoesPlayerHaveWeapons(playerid)
{
    new weap, am;
    for(new i = 0; i < 13; i ++)
    {
        GetPlayerWeaponData(playerid, i, weap, am);
        if(weap && am >= 1) return true;
    }
    return false;
}
A simplified version.
Reply
#7

Quote:
Originally Posted by Threshold
Посмотреть сообщение
pawn Код:
forward bool:DoesPlayerHaveWeapons(playerid);
public bool:DoesPlayerHaveWeapons(playerid)
{
    new weap, am;
    for(new i = 0; i < 13; i ++)
    {
        GetPlayerWeaponData(playerid, i, weap, am);
        if(weap && am >= 1) return true;
    }
    return false;
}
A simplified version.
pawn Код:
bool:DoesPlayerHaveWeapons(playerid)
{
    new weap, am;
    for(new i = 0; i < 13; i ++)
    {
        GetPlayerWeaponData(playerid, i, weap, am);
        if(weap && am >= 1) return true;
    }
    return false;
}
a better version.
Reply
#8

Quote:
Originally Posted by HBG
Посмотреть сообщение
pawn Код:
bool:DoesPlayerHaveWeapons(playerid)
{
    new weap, am;
    for(new i = 0; i < 13; i ++)
    {
        GetPlayerWeaponData(playerid, i, weap, am);
        if(weap && am >= 1) return true;
    }
    return false;
}
a better version.
How is that better? You will get a warning (in a case), such as: C:\Users\... : warning 208: function with tag result used before definition, forcing reparse.
Reply
#9

Quote:
Originally Posted by SickAttack
Посмотреть сообщение
How is that better? You will get a warning (in a case), such as: C:\Users\... : warning 208: function with tag result used before definition, forcing reparse.
Throws no warnings for me at all, and I am sorry but is this 2006? I thought it was a known fact that using callbacks for your custom functions was a bad practice that only makes your script use more disk space and memory. If youre getting a warning it is because the function isn't being used in your script, either use it in your script somewhere or put stock infront of it. You shouldn't be using callbacks none the less.
Reply
#10

Quote:
Originally Posted by HBG
Посмотреть сообщение
Throws no warnings for me at all, and I am sorry but is this 2006? I thought it was a known fact that using callbacks for your custom functions was a bad practice that only makes your script use more disk space and memory. If youre getting a warning it is because the function isn't being used in your script, either use it in your script somewhere or put stock infront of it. You shouldn't be using callbacks none the less.
Add it to the end of the file, where all the functions are most likely always placed.

Public functions use more memory and should be used in special cases, there is nothing really wrong in using it for functions that require a tag.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)