20.03.2016, 23:43
Okay, first of all, use this function by Slice to replace the strings.
This should be placed on top of your script somewhere, above any callbacks.
Once you've done that, you can create your color conversion function, which will convert text like "{RED}", "{BLUE}", "{YELLOW}" etc. to their respective hex codes: "{FF0000}", "{0000FF}" etc.
Unfortunately, there is no way to do this using SA-MP native functions, but we can make our own.
This is the simplest way of doing it.
This function will basically replace every instance of {RED} with {FF0000}. Why do we use {RED} instead of RED? Well, if a player wants to create a label that says: "My favourite color is red.", then the string will end up saying "My favourite color is .". By adding the parenthesis, we can safely assume that they definitely want the color to be changed to red.
Now our /label command:
This doesn't require sscanf for this part, because you only want a string parameter, for which you can just use the 'params' given to you by zcmd.
pawn Код:
stock strreplace(string[], const search[], const replacement[], bool:ignorecase = false, pos = 0, limit = -1, maxlength = sizeof(string)) {
// No need to do anything if the limit is 0.
if (limit == 0)
return 0;
new
sublen = strlen(search),
replen = strlen(replacement),
bool:packed = ispacked(string),
maxlen = maxlength,
len = strlen(string),
count = 0
;
// "maxlen" holds the max string length (not to be confused with "maxlength", which holds the max. array size).
// Since packed strings hold 4 characters per array slot, we multiply "maxlen" by 4.
if (packed)
maxlen *= 4;
// If the length of the substring is 0, we have nothing to look for..
if (!sublen)
return 0;
// In this line we both assign the return value from "strfind" to "pos" then check if it's -1.
while (-1 != (pos = strfind(string, search, ignorecase, pos))) {
// Delete the string we found
strdel(string, pos, pos + sublen);
len -= sublen;
// If there's anything to put as replacement, insert it. Make sure there's enough room first.
if (replen && len + replen < maxlen) {
strins(string, replacement, pos, maxlength);
pos += replen;
len += replen;
}
// Is there a limit of number of replacements, if so, did we break it?
if (limit != -1 && ++count >= limit)
break;
}
return count;
}
Once you've done that, you can create your color conversion function, which will convert text like "{RED}", "{BLUE}", "{YELLOW}" etc. to their respective hex codes: "{FF0000}", "{0000FF}" etc.
Unfortunately, there is no way to do this using SA-MP native functions, but we can make our own.
pawn Код:
ConvertColors(input[144])
{
strreplace(input, "{RED}", "{FF0000}", true);
strreplace(input, "{GREEN}", "{00FF00}", true);
strreplace(input, "{YELLOW}", "{FFFF00}", true);
strreplace(input, "{BLUE}", "{0000FF}", true);
strreplace(input, "{WHITE}", "{FFFFFF}", true);
return input;
}
This function will basically replace every instance of {RED} with {FF0000}. Why do we use {RED} instead of RED? Well, if a player wants to create a label that says: "My favourite color is red.", then the string will end up saying "My favourite color is .". By adding the parenthesis, we can safely assume that they definitely want the color to be changed to red.
Now our /label command:
pawn Код:
CMD:label(playerid, params[])
{
if(!strlen(params)) return SendClientMessage(playerid, -1, "/label text");
new Float:x, Float:y, Float:z, string[144];
strcat(string, params);
format(string, sizeof(string), "%s", ConvertColors(string));
GetPlayerPos(playerid, x, y, z);
Create3DTextLabel(string, 0xFFFFFFFF, x, y, z, 40.0, 0, 0);
return 1;
}