Jump to content
  • 0

pomoc Parametry příkazu pomocí sscanf


Boolove

Dotaz

Dobrý den, lze nějak pomocí sscanf nastavit parametry tak, aby mohl být volitelný také integer? Uvedu příklad.

Mám příkaz /hp s parametry

/hp ID MNOZSTVI

Po zadání všech parametrů nastaví počet HP dle zadaného ID.

Pro jednodušší používání bych chtěl, aby když hráč zadá

/hp MNOZSTVI

tedy přeskočí parametr ID a zadá pouze jednu hodnotu, aby to doplnilo ty HP jemu = automaticky se dosadilo jeho ID.

Děkuji.

Link to comment
Share on other sites

9 odpovědí na tuto otázku

Recommended Posts

  • 2

Nebo, můžeš použít velké specifikátory a tak jim hodit nějakou defaultní hodnotu, jestliže jí nenajde. Pak si za defaltní hodnotu dosadit nějaké nemožné ID (buď maximální a nebo -1) a to brát jako ID toho hráče:

command(hp, playerid, params[]) {
	new 
		id, 
		Float:hp;
	if (sscanf(params, "I(-1)f", id, hp)) return SendClientMessage(playerid, -1, "Použití: /hp <ID> <MNOŽSTVÍ> nebo /hp <MNOŽSTVÍ>");
	if (id == -1) SetPlayerHealth(playerid, hp);
	else SetPlayerHealth(id, hp);
	return 1;
}

 

Edited by Scydo
typo
  • Líbí se mi to! (+1) 1
Link to comment
Share on other sites

  • 2

Práve som testoval všetky ponúkané riešenia (svoje, obidve Scydove a ATomasovo) týmto scriptom:

Spoiler

#define FILTERSCRIPT

#include <a_samp>
#include <sscanf2>

ParseParamsDuFF(const params[], &bool:success, &id, &Float:hp)
{
    if (!sscanf(params, "df", id, hp))
    {
        success = true;
    }
    else if (!sscanf(params, "f", hp))
    {
        id = -1;
        success = true;
    }
    else
    {
        success = false;
    }
}

ParseParamsScydo1(const params[], &bool:success, &id, &Float:hp)
{
    if (sscanf(params, "I(-1)f", id, hp))
    {
        success = false;
        return;
    }
    
    success = true;
}

ParseParamsScydo2(const params[], &bool:success, &id, &Float:hp)
{
    new amount;
    if (sscanf(params, "I(-1)i", id, amount))
    {
        success = false;
        return;
    }
    
    hp = float(amount);
    success = true;
}

ParseParamsATomas(const params[], &bool:success, &id, &Float:hp)
{
    new amount;
    if (sscanf(params, "Ii(99)", id, amount))
    {
        success = false;
        return;
    }

    hp = float(amount);
    success = true;
}

public OnFilterScriptInit()
{
    static const inputs[][2][] = {
        {"id and hp", "1 42"},
        {"id and hp with decimal point", "1 42.0"},
        {"hp only", "42"},
        {"hp only with decimal point", "42.0"},
        {"3 params", "1 2 3"},
        {"invalid input - empty", ""},
        {"invalid input - not a float", "notafloat"}
    };
    
    print("\nDuFF:");
    for (new i = 0; i < sizeof(inputs); ++i)
    {
        new bool:success = false, id, Float:hp;
        ParseParamsDuFF(inputs[i][1], success, id, hp);
        if (success)
        {
            printf("%s ('%s'): id = %d, hp = %.2f", inputs[i][0], inputs[i][1], id, hp);
        }
        else
        {
            printf("%s ('%s'): parsing failed", inputs[i][0], inputs[i][1]);
        }
    }
    
    print("\nScydo1:");
    for (new i = 0; i < sizeof(inputs); ++i)
    {
        new bool:success = false, id, Float:hp;
        ParseParamsScydo1(inputs[i][1], success, id, hp);
        if (success)
        {
            printf("%s ('%s'): id = %d, hp = %.2f", inputs[i][0], inputs[i][1], id, hp);
        }
        else
        {
            printf("%s ('%s'): parsing failed", inputs[i][0], inputs[i][1]);
        }
    }
    
    print("\nScydo2:");
    for (new i = 0; i < sizeof(inputs); ++i)
    {
        new bool:success = false, id, Float:hp;
        ParseParamsScydo2(inputs[i][1], success, id, hp);
        if (success)
        {
            printf("%s ('%s'): id = %d, hp = %.2f", inputs[i][0], inputs[i][1], id, hp);
        }
        else
        {
            printf("%s ('%s'): parsing failed", inputs[i][0], inputs[i][1]);
        }
    }
    
    print("\nATomas:");
    for (new i = 0; i < sizeof(inputs); ++i)
    {
        new bool:success = false, id, Float:hp;
        ParseParamsATomas(inputs[i][1], success, id, hp);
        if (success)
        {
            printf("%s ('%s'): id = %d, hp = %.2f", inputs[i][0], inputs[i][1], id, hp);
        }
        else
        {
            printf("%s ('%s'): parsing failed", inputs[i][0], inputs[i][1]);
        }
    }
    return 1;
}

 

Výstup:

Spoiler

DuFF:
id and hp ('1 42'): id = 1, hp = 42.00
id and hp with decimal point ('1 42.0'): id = 1, hp = 42.00
hp only ('42'): id = -1, hp = 42.00
hp only with decimal point ('42.0'): id = -1, hp = 42.00
3 params ('1 2 3'): id = 1, hp = 2.00
invalid input - empty (''): parsing failed
invalid input - not a float ('notafloat'): parsing failed

Scydo1:
id and hp ('1 42'): id = 1, hp = 42.00
id and hp with decimal point ('1 42.0'): id = 1, hp = 42.00
hp only ('42'): parsing failed
hp only with decimal point ('42.0'): parsing failed
3 params ('1 2 3'): id = 1, hp = 2.00
invalid input - empty (''): parsing failed
invalid input - not a float ('notafloat'): parsing failed

Scydo2:
id and hp ('1 42'): id = 1, hp = 42.00
id and hp with decimal point ('1 42.0'): parsing failed
hp only ('42'): parsing failed
hp only with decimal point ('42.0'): parsing failed
3 params ('1 2 3'): id = 1, hp = 2.00
invalid input - empty (''): parsing failed
invalid input - not a float ('notafloat'): parsing failed

ATomas:
sscanf warning: No default value found.
sscanf warning: Format specifier does not match parameter count.
id and hp ('1 42'): id = 1, hp = 42.00
sscanf warning: No default value found.
id and hp with decimal point ('1 42.0'): parsing failed
sscanf warning: No default value found.
hp only ('42'): parsing failed
sscanf warning: No default value found.
hp only with decimal point ('42.0'): parsing failed
sscanf warning: No default value found.
sscanf warning: Format specifier does not match parameter count.
3 params ('1 2 3'): id = 1, hp = 2.00
sscanf warning: No default value found.
invalid input - empty (''): parsing failed
sscanf warning: No default value found.
invalid input - not a float ('notafloat'): parsing failed

 

 

Moje pôvodné riešenie vyzerá, že funguje správne.

On 24. 5. 2021 at 14:22, DuFF said:

Nemám to otestované, ale niečo takéto by mohlo fungovať:

  Ukázat skrytý obsah


CMD:hp(playerid, params[])
{
    new targetid, Float:amount;
    if (!sscanf(params, "df", targetid, amount))
    {
        SetPlayerHealth(targetid, amount);
    }
    else if (!sscanf(params, "f", amount))
    {
        SetPlayerHealth(playerid, amount);
    }
    else
    {
        SendClientMessage(playerid, -1, "Použití: /hp <ID> <MNOŽSTVÍ> nebo /hp <MNOŽSTVÍ>");
    }
    return 1;
}

 

 

 

  • Smutný 1
Link to comment
Share on other sites

  • 0

Nemám to otestované, ale niečo takéto by mohlo fungovať:

Spoiler

CMD:hp(playerid, params[])
{
    new targetid, Float:amount;
    if (!sscanf(params, "df", targetid, amount))
    {
        SetPlayerHealth(targetid, amount);
    }
    else if (!sscanf(params, "f", amount))
    {
        SetPlayerHealth(playerid, amount);
    }
    else
    {
        SendClientMessage(playerid, -1, "Použití: /hp <ID> <MNOŽSTVÍ> nebo /hp <MNOŽSTVÍ>");
    }
    return 1;
}

 

 

Link to comment
Share on other sites

  • 0
On 24. 5. 2021 at 18:05, Scydo said:

Nebo, můžeš použít velké specifikátory a tak jim hodit nějakou defaultní hodnotu, jestliže jí nenajde. Pak si za defaltní hodnotu dosadit nějaké nemožné ID (buď maximální a nebo -1) a to brát jako ID toho hráče:


command(hp, playerid, params[]) {
	new 
		id, 
		Float:hp;
	if (sscanf(params, "I(-1)f", id, hp)) return SendClientMessage(playerid, -1, "Použití: /hp <ID> <MNOŽSTVÍ> nebo /hp <MNOŽSTVÍ>");
	if (id == -1) SetPlayerHealth(playerid, hp);
	else SetPlayerHealth(id, hp);
	return 1;
}

 

Děkuji za radu. Udělal jsem to podle návodu, ale když zadám příkaz pouze s jedním parametrem, tak mi to vrátí SCM se syntaxí a příkaz se neprovede. Čím to může být?

Link to comment
Share on other sites

  • 0
před 18hodinami, Boolove said:

Děkuji za radu. Udělal jsem to podle návodu, ale když zadám příkaz pouze s jedním parametrem, tak mi to vrátí SCM se syntaxí a příkaz se neprovede. Čím to může být?

Sscanf vidí int jako "dobrovolné" takže o tom žádná, že tam je někde chyba. Tím to určitě není. Pak mě ještě napadá možnost, že protože float už není, tak je možné, že tvoje číslo si sscanf dosadil do I. Zadával jsi celé číslo 100 a nebo i desetinnou tečkou 100.0 ?
Pak můžeš ještě f zaměnit za i, takže budeš zadávat čísla a pak hp jen konvertuješ na float pro funkci.

Link to comment
Share on other sites

  • 0

Mám to takhle:

if(sscanf(params, "I(99)i", id, am)) return SCM(playerid, 0x7CFC00FF, "Použití: /kredity <ID> <MNOŽSTVÍ> nebo /kredity <MNOŽSTVÍ>");

Pak samozřejmě porovnávám 99 a podle toho nastavuji, tam už by chyba být neměla takže přidávám pouze sscanf kód.

Jakmile zadám /kredity 400, tak mi to vyhodí zprávu o použití.

Link to comment
Share on other sites

  • 0
před 7hodinami, DuFF said:

Práve som testoval všetky ponúkané riešenia (svoje, obidve Scydove a ATomasovo) týmto scriptom:

  Opětovně skrýt obsah


#define FILTERSCRIPT

#include <a_samp>
#include <sscanf2>

ParseParamsDuFF(const params[], &bool:success, &id, &Float:hp)
{
    if (!sscanf(params, "df", id, hp))
    {
        success = true;
    }
    else if (!sscanf(params, "f", hp))
    {
        id = -1;
        success = true;
    }
    else
    {
        success = false;
    }
}

ParseParamsScydo1(const params[], &bool:success, &id, &Float:hp)
{
    if (sscanf(params, "I(-1)f", id, hp))
    {
        success = false;
        return;
    }
    
    success = true;
}

ParseParamsScydo2(const params[], &bool:success, &id, &Float:hp)
{
    new amount;
    if (sscanf(params, "I(-1)i", id, amount))
    {
        success = false;
        return;
    }
    
    hp = float(amount);
    success = true;
}

ParseParamsATomas(const params[], &bool:success, &id, &Float:hp)
{
    new amount;
    if (sscanf(params, "Ii(99)", id, amount))
    {
        success = false;
        return;
    }

    hp = float(amount);
    success = true;
}

public OnFilterScriptInit()
{
    static const inputs[][2][] = {
        {"id and hp", "1 42"},
        {"id and hp with decimal point", "1 42.0"},
        {"hp only", "42"},
        {"hp only with decimal point", "42.0"},
        {"3 params", "1 2 3"},
        {"invalid input - empty", ""},
        {"invalid input - not a float", "notafloat"}
    };
    
    print("\nDuFF:");
    for (new i = 0; i < sizeof(inputs); ++i)
    {
        new bool:success = false, id, Float:hp;
        ParseParamsDuFF(inputs[i][1], success, id, hp);
        if (success)
        {
            printf("%s ('%s'): id = %d, hp = %.2f", inputs[i][0], inputs[i][1], id, hp);
        }
        else
        {
            printf("%s ('%s'): parsing failed", inputs[i][0], inputs[i][1]);
        }
    }
    
    print("\nScydo1:");
    for (new i = 0; i < sizeof(inputs); ++i)
    {
        new bool:success = false, id, Float:hp;
        ParseParamsScydo1(inputs[i][1], success, id, hp);
        if (success)
        {
            printf("%s ('%s'): id = %d, hp = %.2f", inputs[i][0], inputs[i][1], id, hp);
        }
        else
        {
            printf("%s ('%s'): parsing failed", inputs[i][0], inputs[i][1]);
        }
    }
    
    print("\nScydo2:");
    for (new i = 0; i < sizeof(inputs); ++i)
    {
        new bool:success = false, id, Float:hp;
        ParseParamsScydo2(inputs[i][1], success, id, hp);
        if (success)
        {
            printf("%s ('%s'): id = %d, hp = %.2f", inputs[i][0], inputs[i][1], id, hp);
        }
        else
        {
            printf("%s ('%s'): parsing failed", inputs[i][0], inputs[i][1]);
        }
    }
    
    print("\nATomas:");
    for (new i = 0; i < sizeof(inputs); ++i)
    {
        new bool:success = false, id, Float:hp;
        ParseParamsATomas(inputs[i][1], success, id, hp);
        if (success)
        {
            printf("%s ('%s'): id = %d, hp = %.2f", inputs[i][0], inputs[i][1], id, hp);
        }
        else
        {
            printf("%s ('%s'): parsing failed", inputs[i][0], inputs[i][1]);
        }
    }
    return 1;
}

 

Výstup:

  Opětovně skrýt obsah


DuFF:
id and hp ('1 42'): id = 1, hp = 42.00
id and hp with decimal point ('1 42.0'): id = 1, hp = 42.00
hp only ('42'): id = -1, hp = 42.00
hp only with decimal point ('42.0'): id = -1, hp = 42.00
3 params ('1 2 3'): id = 1, hp = 2.00
invalid input - empty (''): parsing failed
invalid input - not a float ('notafloat'): parsing failed

Scydo1:
id and hp ('1 42'): id = 1, hp = 42.00
id and hp with decimal point ('1 42.0'): id = 1, hp = 42.00
hp only ('42'): parsing failed
hp only with decimal point ('42.0'): parsing failed
3 params ('1 2 3'): id = 1, hp = 2.00
invalid input - empty (''): parsing failed
invalid input - not a float ('notafloat'): parsing failed

Scydo2:
id and hp ('1 42'): id = 1, hp = 42.00
id and hp with decimal point ('1 42.0'): parsing failed
hp only ('42'): parsing failed
hp only with decimal point ('42.0'): parsing failed
3 params ('1 2 3'): id = 1, hp = 2.00
invalid input - empty (''): parsing failed
invalid input - not a float ('notafloat'): parsing failed

ATomas:
sscanf warning: No default value found.
sscanf warning: Format specifier does not match parameter count.
id and hp ('1 42'): id = 1, hp = 42.00
sscanf warning: No default value found.
id and hp with decimal point ('1 42.0'): parsing failed
sscanf warning: No default value found.
hp only ('42'): parsing failed
sscanf warning: No default value found.
hp only with decimal point ('42.0'): parsing failed
sscanf warning: No default value found.
sscanf warning: Format specifier does not match parameter count.
3 params ('1 2 3'): id = 1, hp = 2.00
sscanf warning: No default value found.
invalid input - empty (''): parsing failed
sscanf warning: No default value found.
invalid input - not a float ('notafloat'): parsing failed

 

 

Moje pôvodné riešenie vyzerá, že funguje správne.

 

No nekdy ta intuice nevyjde :) Ale scanff jsem nikdy nepouzil :) Jen jsem cekal ze bude drzet zavedene zvyklosti :D

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...