Comparing a string to a string in an array?
#1

I want to compare these two strings, if someone wants store a 'Desert Eagle' and WeaponNames[24] held the string "Desert Eagle", Could I compare these accurately?

pawn Code:
WeaponNames[model];
pawn Code:
new WeaponNames[47][] = // As below
{
    "Fists","Brass Knuckles","Golf Club","Nightstick","Knife","Baseball Bat","Shovel","Pool Cue","Katana","Chainsaw","Purple Dildo","Small White Vibrator","Large White Vibrator","Silver Vibrator",
    "Flowers","Cane","Grenade","Tear Gas Grenade","Molotov Cocktail","Jetpack"," "," ","9mm Pistol","Silenced Pistol","Desert Eagle","12-gauge Shotgun","Sawn-off Shotgun","SPAS-12",
    "Micro Uzi","MP5","AK-47","M4A1 Carbine","TEC-9","Country Rifle","Sniper Rifle","Rocket Launcher","Heatseeker","Flamethrower","Minigun","Satchel Charge","Detonator","Spray Can","Fire Extinguisher",
    "Camera","Night Vision Goggles", "Thermal Goggles","Parachute"
};
Reply
#2

What do you mean?

You can compare this string like this:
PHP Code:
if(!strcmp(WeaponNames[modelid], [second string here], true)) 
Reply
#3

So if I looped through all of the ID's I could check which one compares correctly?

Would that work? Like this.

pawn Code:
for(new w = 0; w < 45; w++)
                {
                    if(!strcmp(WeaponNames[w], USER INPUT, true))
                    {
                        if(Playerhasthisweaponid)
                        {
                            //do the rest here
                        }
                    }
                }
Reply
#4

Quote:
Originally Posted by Dokins
View Post
So if I looped through all of the ID's I could check which one compares correctly?

Would that work? Like this.

pawn Code:
for(new w = 0; w < 45; w++)
                {
                    if(!strcmp(WeaponNames[w], USER INPUT, true))
                    {
                        if(Playerhasthisweaponid)
                        {
                            //do the rest here
                        }
                    }
                }
Yeah, this will work.
Reply
#5

I did this and it did not work.

pawn Code:
CMD:weptest(playerid, params[])
{
    new string[50];
    if(sscanf(params, "s[50]", string)) return SendClientMessage(playerid, COLOUR_GREY, "Usage: /weptest [weapon]");
    for(new w = 0; w < 12; w++)
    {
        new model = WepModel[PlayerWeapons[w][playerid]];
        if(!strcmp(WeaponNames[model], string, true))
        {
            format(string, sizeof(string), "Weapon: %s | Ammo: %d | Weapon ID: %d", WeaponNames[model], WepAmmo[w], w);
            SendClientMessage(playerid, COLOUR_WHITE, string);
        }
    }

    return 1;
}
Reply
#6

Try this:
PHP Code:
CMD:weptest(playeridparams[])
{
    new 
string[50];
     if(
sscanf(params"s[50]"string)) return SendClientMessage(playeridCOLOUR_GREY"Usage: /weptest [weapon]");
    for(new 
047w++)
    {
        if(!
strcmp(WeaponNames[w], stringtrue))
        {
            
format(stringsizeof(string), "Weapon: %s | Ammo: %d | Weapon ID: %d"WeaponNames[w], WepAmmo[w], w);
            
SendClientMessage(playeridCOLOUR_WHITEstring);
        }
    }
    return 
1;

Reply
#7

Weapon names is the weapon model, I save weapons with their ID. But thanks! I appreciate the help!
Reply
#8

But, the code I gave will work. It compares the string of WeaponNames with the string entered by the player.
Reply
#9

Not quite sure why you're using 'PlayerWeapons' in the first place, this is all you really need:

pawn Code:
CMD:weptest(playerid, params[])
{
    new weapname[25];
    if(sscanf(params, "s[25]", weapname)) return SendClientMessage(playerid, COLOUR_GREY, "Usage: /weptest [weapon]");
    for(new w = 0; w < sizeof(WeaponNames); w++)
    {
        if(!strcmp(WeaponNames[w], weapname, true))
        {
            new string[65];
            format(string, sizeof(string), "Weapon: %s | Ammo: %d | Weapon ID: %d", WeaponNames[w], WepAmmo[w], w);
            SendClientMessage(playerid, COLOUR_WHITE, string);
            break;
        }
    }
    return 1;
}
EDIT: What's the difference between model and ID? Does ID refer to the weapon slot in which they are held?
Reply
#10

Quote:
Originally Posted by Threshold
View Post
Not quite sure why you're using 'PlayerWeapons' in the first place, this is all you really need:

pawn Code:
CMD:weptest(playerid, params[])
{
    new weapname[25];
    if(sscanf(params, "s[25]", weapname)) return SendClientMessage(playerid, COLOUR_GREY, "Usage: /weptest [weapon]");
    for(new w = 0; w < sizeof(WeaponNames); w++)
    {
        if(!strcmp(WeaponNames[w], weapname, true))
        {
            new string[65];
            format(string, sizeof(string), "Weapon: %s | Ammo: %d | Weapon ID: %d", WeaponNames[w], WepAmmo[w], w);
            SendClientMessage(playerid, COLOUR_WHITE, string);
            break;
        }
    }
    return 1;
}
EDIT: What's the difference between model and ID? Does ID refer to the weapon slot in which they are held?
Try this:
PHP Code:
CMD:weptest(playeridparams[])
{
    new 
string[50];
     if(
sscanf(params"s[50]"string)) return SendClientMessage(playeridCOLOUR_GREY"Usage: /weptest [weapon]");
    for(new 
047w++)
    {
        if(!
strcmp(WeaponNames[w], stringtrue))
        {
            
format(stringsizeof(string), "Weapon: %s | Ammo: %d | Weapon ID: %d"WeaponNames[w], WepAmmo[w], w);
            
SendClientMessage(playeridCOLOUR_WHITEstring);
        }
    }
    return 
1;

Both are same -_-
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)