PDA

View Full Version : Colour in purple word in **


alanhutch
14/05/2018, 07:50 AM
Hi all, I'm trying to make a function in OnPlayerText where if the player says like 'Hello! *waving his hand*' the 'waving his hand' in purple.
I dunno what function should I use, with strfind the entire Onplayertext stopped working.

Vennox
18/05/2018, 01:09 AM
try this:


public OnPlayerText(playerid, text[]) {
if(strfind(text, "hello", true) != -1) strcat(text, "{ff8ed7} *Waving his hand*", sizeof(text));

return 0;
}

JasonRiggs
18/05/2018, 01:12 AM
try this:


public OnPlayerText(playerid, text[]) {
if(strfind(text, "hello", true) != -1) strcat(text, "{ff8ed7} *Waving his hand*", sizeof(text));

return 0;
}


Are you serious? Then it will only work with "Hello *waving his hand*" lol??

I believe strfind would work, Learn how to do it from here (http://wiki.sa-mp.com/wiki/Strfind)..

Vennox
18/05/2018, 01:21 AM
Are you serious? Then it will only work with "Hello *waving his hand*" lol??

I believe strfind would work, Learn how to do it from here (http://wiki.sa-mp.com/wiki/Strfind)..

idk what are you trying to say.
look closer at my code, the second time i used strcat, not strfind
public OnPlayerText(playerid, text[]) {
if(strfind(text, "hello", true) != -1) strcat(text, "{ff8ed7} *Waving his hand*", sizeof(text));

return 0;
}
if it detects the word "hello" in your phrase (for example: "hello dude"), it will attach "{ff8ed7} *Waving his hand*" to your text.

so when you text "hello dude", your message will be shown as "Player says: hello dude *Waving his hand*"


http://wiki.sa-mp.com/wiki/Strcat
http://wiki.sa-mp.com/wiki/Strfind

JasonRiggs
18/05/2018, 01:30 AM
idk what are you trying to say.
look closer at my code, the second time i used strcat, not strfind
public OnPlayerText(playerid, text[]) {
if(strfind(text, "hello", true) != -1) strcat(text, "{ff8ed7} *Waving his hand*", sizeof(text));

return 0;
}
if it detects the word "hello" in your phrase (for example: "hello dude"), it will attach "{ff8ed7} *Waving his hand*" to your text.

so when you text "hello dude", your message will be shown as "Player says: hello dude *Waving his hand*"
http://wiki.sa-mp.com/wiki/Strcat
http://wiki.sa-mp.com/wiki/Strfind



He doesn't want it like that mate, look I'll explain it a bit deeper..

He doesn't want anything from "Hello" or "Waving his hand"

Alllll what he wants to do is to detect a " * STRING HERE * " he wants two stars with text in between, if that was detected, remove the stars and send the string in purple color which is not like the other text, So not just "Hello"

But if -FOR EXAMPLE- I said "Fuck you *shows his middle finger*" I want to send the "Fuck you" in white color, and "shows his middle finger" in purple color but without "*" got it? ALL what you need in it is finding "*" in the player text then omit it and send message to nearby people with color purple(of the text surrounded by stars) easy.

Vennox
18/05/2018, 02:18 AM
He doesn't want it like that mate, look I'll explain it a bit deeper..

He doesn't want anything from "Hello" or "Waving his hand"

Alllll what he wants to do is to detect a " * STRING HERE * " he wants two stars with text in between, if that was detected, remove the stars and send the string in purple color which is not like the other text, So not just "Hello"

But if -FOR EXAMPLE- I said "Fuck you *shows his middle finger*" I want to send the "Fuck you" in white color, and "shows his middle finger" in purple color but without "*" got it? ALL what you need in it is finding "*" in the player text then omit it and send message to nearby people with color purple(of the text surrounded by stars) easy.

ty for explaining.

You can use strreplace from strlib.inc, by Slice: http://forum.sa-mp.com/showthread.php?t=362764
But the problem is that the variable text[] from OnPlayerText cannot be formated.
So you have to return 0 OnPlayerText and send the message by yourself to the nearby players.

public OnPlayerText(playerid, text[]){
new string[256];
new name[25];
GetPlayerName(playerid, name, 25);
format(string, 256, "%s says: %s", name, text);
if(strfind(string, "*")) strreplace(string, "*", "{ff00ff}", false, 0, -1, 256);
for(new i; i < MAX_PLAYERS; i++){
new Float:x, Float:y, Float:z;
GetPlayerPos(playerid, x, y, z);
if(IsPlayerInRangeOfPoint(i, 20, x, y, z)) SendClientMessage(i, -1, string);
}
return 0;
}
// extracted from strlib.inc
stock strreplace(string[], const search[], const replacement[], bool:ignorecase = false, pos = 0, limit = -1, maxlength = sizeof(string)) {
if (limit == 0)
return 0;
new
sublen = strlen(search),
replen = strlen(replacement),
bool:packed = ispacked(string),
maxlen = maxlength,
len = strlen(string),
count = 0
;
if (packed)
maxlen *= 4;
if (!sublen)
return 0;
while (-1 != (pos = strfind(string, search, ignorecase, pos))) {
strdel(string, pos, pos + sublen);
len -= sublen;
if (replen && len + replen < maxlen) {
strins(string, replacement, pos, maxlength);
pos += replen;
len += replen;
}
if (limit != -1 && ++count >= limit)
break;
}
return count;
}

Here is a preview:
https://i.imgur.com/qqhpizd.jpg

Logic_
18/05/2018, 04:44 AM
Your testing should be able to prove your code wrong at some point (Your code should be falsifiable), if there isn't then your code will work as you wanted. Many people don't think how their code can malfunction.

What if I type "something * hey", what do you expect here to be purple? I think sscanf would do the trick here as:
New highlight[20];
Sscanf(text, "{S}'*'s[20]'*'", highlight))

There can be a way to write my code with a different method as well.