SA-MP Forums Archive
/color - 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: /color (/showthread.php?tid=513062)



[FIXED] /color - AiRaLoKa - 14.05.2014

hi all...

can anyone fix my code and then explain to me why it doesn't work?

pawn Код:
CMD:color(playerid,params[])
{
    new tempcolor[32];
    format(tempcolor, sizeof tempcolor, "0x%sFF", params);
    strmid(tempcolor, tempcolor, 0, strlen(tempcolor), 32);
    SetPlayerColor(playerid, tempcolor[0]);
    printf("%s",tempcolor[0]); // for debugging
    return 1;
}



Re: /color - AiRaLoKa - 14.05.2014

bump...
anyone?
i really really need an answer...


Re: /color - Vince - 14.05.2014

Colors aren't strings, they're numbers. Use sscanf's 'x' specifier to convert hexadecimal string input to the proper number.


Re: /color - AiRaLoKa - 14.05.2014

like this?

pawn Код:
CMD:color(playerid,params[])
{
    new tempcolor[32];
    format(tempcolor, sizeof tempcolor, "0x%xFF", params);
    strmid(tempcolor, tempcolor, 0, strlen(tempcolor), 32);
    SetPlayerColor(playerid, tempcolor[0]);
    printf("%s",tempcolor[0]); // for debugging
    return 1;
}



Re: /color - RajatPawar - 14.05.2014

Quote:
Originally Posted by Vince
Посмотреть сообщение
..hexadecimal string input to the proper number.
I am pretty sure that the "x" specifier also handles normal numbers, making the job more easier! Correct me if I am wrong. Cheers (source?)

EDIT: I still have no idea what that page tells, I'll just run some code, thanks


Re: /color - AiRaLoKa - 14.05.2014

Quote:
Originally Posted by RajatPawar
Посмотреть сообщение
I am pretty sure that the "x" specifier also handles normal numbers, making the job more easier! Correct me if I am wrong. Cheers (source?)
will you show me the example?


Re: /color - AiRaLoKa - 14.05.2014

bump
come on guys....
i really need it...


Re: /color - Vince - 14.05.2014

Come on dude have some patience!

Quote:
Originally Posted by RajatPawar
Посмотреть сообщение
I am pretty sure that the "x" specifier also handles normal numbers, making the job more easier! Correct me if I am wrong. Cheers (source?)
No. Because, for example, 11 (0x11) in the hexadecimal number system is 17 in the decimal number system.


pawn Код:
CMD:color(playerid, params[])
{
    new color;
   
    if(sscanf(params, "x", color))
        return SendClientMessage(playerid, -1, "Invalid input");
       
    color <<= 8; // shift left to make room for alpha
    color |= 0xFF; // Add the alpha
   
    SetPlayerColor(playerid, color);
    return 1;
}
Pro tip: use the Programmer's mode in Windows Calculator to help you with hexadecimal operations.


Re: /color - Konstantinos - 14.05.2014

If the length of params wasn't 6, then I'd send an error message because let's say the player typed "00FF00FF00FF" - the result would be: 0xFF00FFFF

PS: I knew it was possible to add the alpha to the variable (integer type) but I couldn't remember how it was. Thanks for reminding me that, Vince!


Re: /color - AiRaLoKa - 15.05.2014

Quote:
Originally Posted by Vince
Посмотреть сообщение
pawn Код:
CMD:color(playerid, params[])
{
    new color;
   
    if(sscanf(params, "x", color))
        return SendClientMessage(playerid, -1, "Invalid input");
       
    color <<= 8; // shift left to make room for alpha
    color |= 0xFF; // Add the alpha
   
    SetPlayerColor(playerid, color);
    return 1;
}
Pro tip: use the Programmer's mode in Windows Calculator to help you with hexadecimal operations.
thank's for the explanation Vince....

i've been edited it a bit into
pawn Код:
CMD:color(playerid, params[])
{
    new color;

    if(sscanf(params, "x", color))
        return SendClientMessage(playerid, -1, "Invalid input");
    if(strlen(params) != 6)
        return SendClientMessage(playerid, -1, "Invalid input"); // remind the player if the color is invalid

    color <<= 8; // shift left to make room for alpha
    color |= 0xFF; // Add the alpha

    SetPlayerColor(playerid, color); // set the player's color
    return 1;
}
and it's worked!....

thank's
+REP

EDIT:

should i change the mysql query from "i" into "x"?