SA-MP Forums Archive
Command help - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Command help (/showthread.php?tid=663395)



Command help - SymonClash - 30.01.2019

I'm trying to do a /fight command.

Explanation:

Admin can do /fight [weapon id] to give the choose weapon ID to all players and showing them a GameText, but in GameText i wanna show weapon name, like, if admins do /fight 24 it will show: "Deagle fight!", but getting some errors:

Quote:

(2677) : error 012: invalid function call, not a valid address
(2677) : warning 215: expression has no effect
(2677) : error 001: expected token: ";", but found ")"
(2677) : error 029: invalid expression, assumed zero
(2677) : fatal error 107: too many error messages on one line

Error line is referred to GameText.

pawn Код:
CMD:fight(playerid, params[])
{
    new wep_id, string[100];
   
    if(sscanf(params, "d", wep_id)) return SCM(playerid, COLOR_GREEN, "* [USAGE]: /fight [weapon id]");
   
    foreach(new i : Player)
    {
        GivePlayerWeapon(i, wep_id, 9999);
        format(string, sizeof(string), "~g~%s ~w~fight!", GunName(wep_id));
        GameTextForPlayer(i, string, 3000, 4);
    }
    return 1;
}
GunName:

pawn Код:
new GunName[47][20] =
{
    "Fist","Brass Knuckles","Golf Club","Nightstick","Knife","Basebal Bat","Shovel","Pool Cue","Katana","Chainsaw","Double-ended Dildo","Dildo","Vibrator",
    "Silver Vibrator","Flowers","Cane","Grenade","Tear Gas","Molotv Cocktail","?","?","?","9mm","Silenced 9mm","Desert Eagle","Shotgun","Sawnoff",
    "SPAS-12","Micro-SMG","MP5","Ak-47","M4","Tec9","Country Rifle","Sniper Rifle","RPG","HS-RPG","Flame-Thrower","Minigun","Satchel Charge","Detonator",
    "Spray Can","Fire Extinguisher","Camera","Night Goggles","Thermal Goggles","Parachute"
};



Re: Command help - Calisthenics - 30.01.2019

`GunName` is array, not function.

Код:
format(string, sizeof(string), "~g~%s ~w~fight!", GunName[wep_id]);
Place sanity checks before the loop to validate the weaponid otherwise run time error 4. In case you do not know, there is already a function to get the name of weapon: https://sampwiki.blast.hk/wiki/GetWeaponName


Re: Command help - SymonClash - 30.01.2019

Ok did in this way:

pawn Код:
CMD:fight(playerid, params[])
{
    new wep_id, string[100];
   
    new gunname[32];
    GetWeaponName(wep_id, gunname, sizeof(gunname));

    if(sscanf(params, "d", wep_id)) return SCM(playerid, COLOR_GREEN, "* [USAGE]: /fight [weapon id]");

    foreach(new i : Player)
    {
        GivePlayerWeapon(i, wep_id, 9999);
        format(string, sizeof(string), "~g~%s ~w~fight!", gunname);
        GameTextForPlayer(i, string, 3000, 4);
    }
    return 1;
}
But the GameText shows only "FIGHT!" and not the weapon name before. Also can you explain more about sanity checks?


Re: Command help - Calisthenics - 30.01.2019

Call `GetWeaponName` after sscanf. Sanity checks about the weaponid that it is in range between 0 and 46. Weapon ids 19, 20, 21 are invalid.

Reference: https://sampwiki.blast.hk/wiki/Weapons


Re: Command help - SymonClash - 30.01.2019

Thanks, did in this way and it's working now.

pawn Код:
CMD:fight(playerid, params[])
{
    new wep_id, string[100], gunname[32];

    if(sscanf(params, "d", wep_id)) return SCM(playerid, COLOR_GREEN, "* [USAGE]: /fight [weapon id]");
   
    GetWeaponName(wep_id, gunname, sizeof(gunname));
   
    switch(wep_id)
    {
        case 0, 1, 14, 17, 19, 20, 21, 40, 43, 44, 45, 46: return SCM(playerid, COLOR_RED, "* Invalid weapon ID.");
    }

    foreach(new i : Player)
    {
        GivePlayerWeapon(i, wep_id, 9999);
        format(string, sizeof(string), "~g~%s ~w~fight!", gunname);
        GameTextForPlayer(i, string, 3000, 4);
    }
    return 1;
}
As you can see, i restriced some wep ids to be given, but i can still do /fight 50 or even /fight 500 ...

How to add your sanity checks?


Re: Command help - Calisthenics - 30.01.2019

You can check what weapons are not restricted and use `default` keyword to handle the error.
pawn Код:
switch (wep_id)
{
    case 2 .. 13, 15, 16, 18, 22 .. 39, 41, 42:
    {
        // foreach loop
    }
    default:
    {
        SCM(playerid, COLOR_RED, "* Invalid weapon ID.");
    }
}
Another thing to consider is format the text before the loop and not re-format it as many times as the online players. It is the same with variables declaration. If an invalid weapon was given, it creates a string for no reason.


Re: Command help - SymonClash - 30.01.2019

So, something like this?

pawn Код:
CMD:fight(playerid, params[])
{
    new wep_id, string[100], gunname[32];

    if(sscanf(params, "d", wep_id)) return SCM(playerid, COLOR_GREEN, "* [USAGE]: /fight [weapon id]");
   
    GetWeaponName(wep_id, gunname, sizeof(gunname));
   
    switch (wep_id)
    {
        case 2 .. 13, 15, 16, 18, 22 .. 39, 41, 42:
        {
       
        format(string, sizeof(string), "~g~%s ~w~fight!", gunname);
       
        foreach(new i : Player)
        {
            GivePlayerWeapon(i, wep_id, 9999);
            GameTextForPlayer(i, string, 3000, 4);
        }
       
        }
        default:
        {
            SCM(playerid, COLOR_RED, "* Invalid weapon ID.");
        }
    }
    return 1;
}



Re: Command help - Calisthenics - 30.01.2019

Yes, it formats only once now. But you still declare variables or get the name of weapon before checking if the weapon is valid. Notice how I placed them inside the allowed weapons:

pawn Код:
CMD:fight(playerid, params[])
{
    new wep_id;

    if (sscanf(params, "d", wep_id)) return SCM(playerid, COLOR_GREEN, "* [USAGE]: /fight [weapon id]");

    switch (wep_id)
    {
        case 2..13, 15, 16, 18, 22..39, 41, 42:
        {
            new string[32], gunname[19];

            GetWeaponName(wep_id, gunname, sizeof(gunname));
            format(string, sizeof(string), "~g~%s ~w~fight!", gunname);

            foreach(new i: Player)
            {
                GivePlayerWeapon(i, wep_id, 9999);
                GameTextForPlayer(i, string, 3000, 4);
            }
        }
        default:
        {
            SCM(playerid, COLOR_RED, "* Invalid weapon ID.");
        }
    }
    return 1;
}
Another alternative is `GameTextForAll` function so you will only have to give weapon in the foreach loop.


Re: Command help - SymonClash - 30.01.2019

Now is perfect, thank you very much for your patience. Cheers!