Issue with an Array related to separating valid weapons from invalid ones
#1

So I have created an array that contains the ID's of weapons that are valid in terms that I want them to be valid in my gamemode and those that I am not planning to use invalid. Now the problem is that the function that removes the weapon using that array does not function properly and causes my command to become unknown.



pawn Код:
new e_ValidWeapon[] =
{
    0,
    3,
    4,
    5,
    8,
    9,
    15,
    22,
    23,
    24,
    25,
    27,
    28,
    29,
    30,
    31,
    32,
    33,
    34,
    41,
    42,
    43
};
pawn Код:
RemoveWeaponFromPlayer(playerid, weaponid)
{
    new Ammo;
    SetPlayerArmedWeapon(playerid, weaponid);
    Ammo = GetPlayerAmmo(playerid);
    if(weaponid != e_ValidWeapon[weaponid]) return false;
    else if(weaponid == e_ValidWeapon[weaponid])
    {
        print("Executed.");
        GivePlayerWeapon(playerid, weaponid, -Ammo);
        return true;
    }
    return false;
}
pawn Код:
CMD:offduty(playerid, params[])
{
    new string[250];
    if(GetPlayerInterior(playerid) == 6 && pInfo[playerid][USER_FACTION] == SCSD)
    {
        if(pInfo[playerid][USER_FACTIONRANK] == 1)
        {
            SetPlayerArmour(playerid, 0.0);
            SetPlayerHealth(playerid, 100.0);
            RemoveWeaponFromPlayer(playerid, 3);
            RemoveWeaponFromPlayer(playerid, 24);
            RemoveWeaponFromPlayer(playerid, 41);
            SetPlayerDuty(playerid, false);
            format(string, sizeof(string), "** Cadet %s is now off-duty! **", RemoveUnderscore(playerid));
            SendMessageToFaction(SCSD, string);
            return 1;
        }
        else if(pInfo[playerid][USER_FACTIONRANK] == 2)
        {
            SetPlayerArmour(playerid, 0.0);
            SetPlayerHealth(playerid, 100.0);
            RemoveWeaponFromPlayer(playerid, 3);
            RemoveWeaponFromPlayer(playerid, 24);
            RemoveWeaponFromPlayer(playerid, 41);
            SetPlayerDuty(playerid, false);
            format(string, sizeof(string), "** Deputy I %s is now off-duty! **", RemoveUnderscore(playerid));
            SendMessageToFaction(SCSD, string);
            return 1;
        }
        else if(pInfo[playerid][USER_FACTIONRANK] == 3)
        {
            SetPlayerArmour(playerid, 0.0);
            SetPlayerHealth(playerid, 100.0);
            RemoveWeaponFromPlayer(playerid, 3);
            RemoveWeaponFromPlayer(playerid, 24);
            RemoveWeaponFromPlayer(playerid, 41);
            SetPlayerDuty(playerid, false);
            format(string, sizeof(string), "** Deputy II %s is now off-duty! **", RemoveUnderscore(playerid));
            SendMessageToFaction(SCSD, string);
            return 1;
        }
        else if(pInfo[playerid][USER_FACTIONRANK] == 4)
        {
            SetPlayerArmour(playerid, 0.0);
            SetPlayerHealth(playerid, 100.0);
            RemoveWeaponFromPlayer(playerid, 3);
            RemoveWeaponFromPlayer(playerid, 24);
            RemoveWeaponFromPlayer(playerid, 41);
            SetPlayerDuty(playerid, false);
            format(string, sizeof(string), "** Corporal %s is now off-duty! **", RemoveUnderscore(playerid));
            SendMessageToFaction(SCSD, string);
            return 1;
        }
        else if(pInfo[playerid][USER_FACTIONRANK] == 5)
        {
            SetPlayerArmour(playerid, 0.0);
            SetPlayerHealth(playerid, 100.0);
            RemoveWeaponFromPlayer(playerid, 3);
            RemoveWeaponFromPlayer(playerid, 24);
            RemoveWeaponFromPlayer(playerid, 41);
            SetPlayerDuty(playerid, false);
            format(string, sizeof(string), "** Sergeant %s is now off-duty! **", RemoveUnderscore(playerid));
            SendMessageToFaction(SCSD, string);
            return 1;
        }
        else if(pInfo[playerid][USER_FACTIONRANK] == 6)
        {
            SetPlayerArmour(playerid, 0.0);
            SetPlayerHealth(playerid, 100.0);
            RemoveWeaponFromPlayer(playerid, 3);
            RemoveWeaponFromPlayer(playerid, 24);
            RemoveWeaponFromPlayer(playerid, 41);
            SetPlayerDuty(playerid, false);
            format(string, sizeof(string), "** Lieutenant %s is now off-duty! **", RemoveUnderscore(playerid));
            SendMessageToFaction(SCSD, string);
            return 1;
        }
        else if(pInfo[playerid][USER_FACTIONRANK] == 7)
        {
            SetPlayerArmour(playerid, 0.0);
            SetPlayerHealth(playerid, 100.0);
            RemoveWeaponFromPlayer(playerid, 3);
            RemoveWeaponFromPlayer(playerid, 24);
            RemoveWeaponFromPlayer(playerid, 41);
            SetPlayerDuty(playerid, false);
            format(string, sizeof(string), "** Captain %s is now off-duty! **", RemoveUnderscore(playerid));
            SendMessageToFaction(SCSD, string);
            return 1;
        }
        else if(pInfo[playerid][USER_FACTIONRANK] == 8)
        {
            SetPlayerArmour(playerid, 0.0);
            SetPlayerHealth(playerid, 100.0);
            RemoveWeaponFromPlayer(playerid, 3);
            RemoveWeaponFromPlayer(playerid, 24);
            RemoveWeaponFromPlayer(playerid, 41);
            SetPlayerDuty(playerid, false);
            format(string, sizeof(string), "** Major %s is now off-duty! **", RemoveUnderscore(playerid));
            SendMessageToFaction(SCSD, string);
            return 1;
        }
        else if(pInfo[playerid][USER_FACTIONRANK] == 9)
        {
            SetPlayerArmour(playerid, 0.0);
            SetPlayerHealth(playerid, 100.0);
            RemoveWeaponFromPlayer(playerid, 3);
            RemoveWeaponFromPlayer(playerid, 24);
            RemoveWeaponFromPlayer(playerid, 41);
            SetPlayerDuty(playerid, false);
            format(string, sizeof(string), "** Undersheriff %s is now off-duty! **", RemoveUnderscore(playerid));
            SendMessageToFaction(SCSD, string);
            return 1;
        }
        else if(pInfo[playerid][USER_FACTIONRANK] == 10)
        {
            SetPlayerArmour(playerid, 0.0);
            SetPlayerHealth(playerid, 100.0);
            RemoveWeaponFromPlayer(playerid, 3);
            RemoveWeaponFromPlayer(playerid, 24);
            RemoveWeaponFromPlayer(playerid, 41);
            SetPlayerDuty(playerid, false);
            format(string, sizeof(string), "** Sheriff %s is now off-duty! **", RemoveUnderscore(playerid));
            SendMessageToFaction(SCSD, string);
            return 1;
        }
        return 1;
    }
    return 1;
}
The issue is that the command does not execute properly as it should have, when it reaches RemoveWeaponFromPlayer it becomes unknown and the function itself does not exectue.
Reply
#2

Hi!

You have to write it like this:
PHP код:
RemoveWeaponFromPlayer(playerid,weaponid)
{
    new 
Ammo;
    
SetPlayerArmedWeapon(playerid,weaponid);
    
Ammo GetPlayerAmmo(playerid);
    for(new 
i;i<sizeof(e_ValidWeapon);i++)
    {
        if(
weaponid != e_ValidWeapon[i])continue;
        print(
"Executed.");
        
GivePlayerWeapon(playerid,weaponid,-Ammo);
        break;
    }
    return 
1;

When you take your version, it would look like this:
PHP код:
weaponid 43 
PHP код:
if(weaponid != e_ValidWeapon[weaponid]) return false
PHP код:
if(43 != e_ValidWeapon[43])return false
PHP код:
new e_ValidWeapon[] =
{
    
0//1
    
3//2
    
4//3
    
5//4
    
8//5
    
9//6
    
15//7
    
22//8
    
23//9
    
24//10
    
25//11
    
27//12
    
28//13
    
29//14
    
30//15
    
31//16
    
32//17
    
33//18
    
34//19
    
41//20
    
42//21
    
43 //...
            // 43
}; 
The field 43 is nothing. I hope you understand how I mean it.
Reply
#3

Thanks for helping me out. +REP
Reply
#4

I would preferably avoid the use of a loop by accessing the index directly. This can be achieved by placing all of the IDs in the array, but assigning -1 or 0 to the ones you want to classify as "invalid".
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)