/color
#1

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;
}
Reply
#2

bump...
anyone?
i really really need an answer...
Reply
#3

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

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;
}
Reply
#5

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
Reply
#6

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?
Reply
#7

bump
come on guys....
i really need it...
Reply
#8

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.
Reply
#9

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!
Reply
#10

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"?
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)