PDA

View Full Version : Strcmp returns 0?


Homerman
25/11/2011, 12:13 PM
Hi.
I've got a simple command defined like

if(!strcmp("/lol", cmdtext, true, 11))
{

which works without any problem. However, when I use strcmp inside it, it always returns 0; - meaning it just pops up "SERVER: Unknown command".

if(!strcmp("/lol", cmdtext, true, 11))
{
new tmp[256], idx;
tmp = strtok(cmdtext, idx);
if(!strlen(tmp))
{
SendClientMessage(playerid, RED, "Syntax: /lol something");
return 1;
}
if(strcmp(tmp, "something", true) == 0)
{
// SOMETHING INSIDE HERE
}
return 1;
}

Looks like this (of course I edited it, this is not the whole command just preview.
Anybody knows what's the problem here please? Thanks!

Kostas'
25/11/2011, 12:15 PM
if(!strcmp("/lol", cmdtext, true, 4)) // This is 4, not 11
{
new tmp[256], idx;
tmp = strtok(cmdtext, idx);
if(!strlen(tmp)) {
SendClientMessage(playerid, RED, "Syntax: /lol something");
return 1;
}
if(strcmp(tmp, "something", true) == 0) {
// Code
return 1
}
return 0;
}

Homerman
25/11/2011, 12:31 PM
Shows two SERVER: Unknown command now.
I honestly can't understand what's wrong...

|_ⒾⓇⓄN_ⒹⓄG_|
25/11/2011, 12:32 PM
use zcmd, ycmd or dcmd... its a lot easier :)

Homerman
25/11/2011, 12:34 PM
use zcmd, ycmd or dcmd... its a lot easier :)

Thanks a lot mister Obvious, now I would like to solve my problem how it is now.
(sorry if I offended you tho, but what you just did is a pure smartass'ing)

Kostas'
25/11/2011, 12:38 PM
Why did you put the command into the other. To be honest, I don't remember a lot about strcmp because I used it only for 1.5 week.
Try something like this

public OnPlayerCommandText(playerid, cmdtext[])
{
if(!strcmp("/lol something", cmdtext, true, 14))
{
new tmp[256], idx;
tmp = strtok(cmdtext, idx);
if(!strlen(tmp)) return SendClientMessage(playerid, RED, "Syntax: /lol something");
else {
// Code
}
return 1;
}
return 0;
}

Homerman
25/11/2011, 12:41 PM
Because I've got more those "something's" in the script, it's like /lol something1 or /lol something2 etc.
What you just did is not what I'm looking for..

Kostas'
25/11/2011, 12:50 PM
Then sorry, but I can't help you.
Something last, in case you don't fix it, is to use someyhing like this
public OnPlayerCommandText(playerid, cmdtext[])
{
new tmp[256], idx;
tmp = strtok(cmdtext, idx);
if(!strcmp("/lol something", cmdtext, true, 14)) {
// Code
return 1;
}
if(!strcmp("/lol anything", cmdtext, true, 13)) {
// Code
return 1;
}
if(!strcmp("/lol nothing", cmdtext, true, 12)) {
// Code
return 1;
}
// rest of Code
return 0;
}

Homerman
25/11/2011, 12:54 PM
Thanks for effort, I'll keep on searching then.

MP2
25/11/2011, 12:58 PM
http://wiki.sa-mp.com/wiki/Debugging