PDA

View Full Version : Help me - Quick Strings ($ply, $cash e.t.c)


Airman123
17/05/2015, 06:28 AM
SOLVED

Threshold
17/05/2015, 07:42 AM
I'd recommend using strreplace by Slice instead.

CheckQuickStrings(playerid, string[])
{
if(strfind(string, "$loc", true) != -1)
{
new zone[MAX_ZONE_NAME];
GetPlayer2DZone(playerid, zone, MAX_ZONE_NAME);
strreplace(string, "$loc", zone, true, .maxlength = 144);
}
if(strfind(string, "$cash", true) != -1)
{
new pmoney[10];
format(pmoney, sizeof(pmoney), "$%d", GetPlayerCash(playerid));
strreplace(string, "$cash", pmoney, true, .maxlength = 144);
}
if(strfind(string, "$ply", true) != -1)
{
new id = GetPlayerWhoIsClosest(playerid), closeststr[32];
if(!IsPlayerConnected(id)) id = playerid;
format(closeststr, sizeof(closeststr), "%s (%d)", PlayerInfo[id][pName], id);
strreplace(string, "$ply", closeststr, true, .maxlength = 144);
}
return 1;
}

stock strreplace(string[], const search[], const replacement[], bool:ignorecase = false, pos = 0, limit = -1, maxlength = sizeof(string)) // Credit to Slice
{
if(!limit) return 0;
new sublen = strlen(search);
if(!sublen) return 0;
if(!strcmp(search, replacement, ignorecase)) return 0;
new
bool:packed = ispacked(string),
maxlen = maxlength,
replen = strlen(replacement),
len = strlen(string),
count = 0
;
if(packed) maxlen *= 4;
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;
}

Yashas
17/05/2015, 07:57 AM
If you are returning 0 in OnPlayerText then the chat message won't be sent.

Possible reasons I can think of:
Your do...while goes into an infinite loop - but you said that the messages are printed in your console(server log) so an infinite loop cannot be the problem

You are returning 0 at OnPlayerText, show us your OPT callback.

I am guessing that using string again is causing the problem.Try with another string.
Don't use the argument string.Make a local array to do the replacements then use strcat to copy it back.
@Threshold wouldn't you code be slower?PAWN is way way slower than natives.

Airman123
17/05/2015, 08:44 AM
- Solved.

Threshold
17/05/2015, 09:20 AM
Change return 1 to return 0.

EDIT:
@Threshold wouldn't you code be slower?PAWN is way way slower than natives.
What are you talking about? There is no native function to replace a string...

Airman123
17/05/2015, 09:21 AM
- Solved.

Threshold
17/05/2015, 09:23 AM
If OnPlayerText returns 1, it will send the original text to main chat... that is why you still see the quickstrings as '$loc' etc.

Return 0...

public OnPlayerText(playerid, text[])
{
// other codes

// end
new string11[144];
strcat(string11, text);
CheckQuickStrings(playerid, string11);
SendPlayerMessageToAll(playerid, string11);
return 0;
}

http://wiki.sa-mp.com/wiki/OnPlayerText

Airman123
17/05/2015, 09:24 AM
Thanks You Very much, @Threshold. +REP man

Thanks <3