SA-MP Forums Archive
Can someone explain how this function works? - 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: Can someone explain how this function works? (/showthread.php?tid=350134)



Can someone explain how this function works? - MR_MAN - 11.06.2012

Basically I'm trying to send the players color as a hex value toa database, using a intTohex function to convert the integer value to hex!

Here is the function
Код:
#define IntToDual(%0) IntToBase(%0, 2)
#define IntToOctal(%0) IntToBase(%0, 8)
#define IntToHex(%0) IntToBase(%0, 16)
stock IntToBase(number, const base) {
    new str[32];
    if(1 < base < 37) {
        new
            m = 1,
            depth = 0;
        while (m <= number) {
            m *= base;
            depth++;
        }
        for ( ; (--depth) != -1; ) {
            str[depth] = (number % base);
            number = ((number - str[depth]) / base);
            if(str[depth] > 9) str[depth] += 'A'; else str[depth] += '0';
        }
    }
    return str;
}
And here is the code I'm using to send the value to the database
Код:
new string[128];
	format(string, sizeof(string), "INSERT INTO test (Name,Text,Color)  VALUES ('%s','%s','%x')", gPlayerName[playerid], text, IntToBase(GetPlayerColor(playerid)));
	mysql_query(string);
However I'm not getting the results I wanted... well none at all! Could someone explain how could I achieve this?


Re: Can someone explain how this function works? - Stevee - 11.06.2012

Please wrap your code in [code ][/ code] tags (to avoid problems, such as a smiley appearing)


Re: Can someone explain how this function works? - Vince - 11.06.2012

Sigh. Colors are numbers! Unless you intend to constantly peek and poke in the database manually, you should just save your colors as integers. You will end up with something like -16776961 for the color red, but that's absolutely fine.


Re: Can someone explain how this function works? - MR_MAN - 11.06.2012

My problem is that I'm trying to use the values in the database to set the color of a html element, so I need it in a hex format!


Re: Can someone explain how this function works? - [NWA]Hannes - 11.06.2012

Quote:
Originally Posted by Vince
Посмотреть сообщение
Sigh. Colors are numbers! Unless you intend to constantly peek and poke in the database manually, you should just save your colors as integers. You will end up with something like -16776961 for the color red, but that's absolutely fine.
GetPlayerColor returns the colors as numbers like you said.

But how can I use those numbers if I for example want to colour a string or set a players color?