12.08.2011, 10:10
(
Last edited by MadeMan; 26/02/2012 at 01:19 PM.
)
ZCMD is a good way to process commands, but there is a problem using it with scripts that have many commands defined already, using other methods that require the OnPlayerCommandText callback (such as strcmp or dcmd). If you want to use ZCMD there, you would have to convert ALL the commands into ZCMD. But if you have many commands, it could take a lot of time. Luckily, there is a fix for that problem.
You can put the contents of zcmd.inc inside your gamemode/filterscript.
EDIT: There is an easier way --> CLICK HERE
Define
First choose how you define your commands.
If you are using COMMAND:mycmd(playerid, params[])
put this into your script
If you are using CMD:mycmd(playerid, params[])
put this into your script
If you are using command(mycmd, playerid, params[])
put this into your script
If you are using cmd(mycmd, playerid, params[])
put this into your script
OnPlayerCommandText
Now to the OnPlayerCommandText callback.
If you want to send your own message when command was not found instead of default "SERVER: Unknown command."
NOTE: Use it ONLY in the GAMEMODE script, because returning 1 in a filterscript will disable commands in other scripts.
And you are done.
Example
Now you can use ZCMD with strcmp for example
NOTE: You don't need the #include <zcmd> line anymore.
Credits to ZeeX for the original ZCMD include.
You can put the contents of zcmd.inc inside your gamemode/filterscript.
EDIT: There is an easier way --> CLICK HERE
Define
First choose how you define your commands.
If you are using COMMAND:mycmd(playerid, params[])
put this into your script
pawn Code:
#define COMMAND:%1(%2) \
forward cmd_%1(%2); \
public cmd_%1(%2)
put this into your script
pawn Code:
#define CMD:%1(%2) \
forward cmd_%1(%2); \
public cmd_%1(%2)
put this into your script
pawn Code:
#define command(%1,%2) \
forward cmd_%1(%2); \
public cmd_%1(%2)
put this into your script
pawn Code:
#define cmd(%1,%2) \
forward cmd_%1(%2); \
public cmd_%1(%2)
Now to the OnPlayerCommandText callback.
pawn Code:
public OnPlayerCommandText(playerid, cmdtext[])
{
new
pos,
funcname[32];
while (cmdtext[++pos] > ' ')
{
funcname[pos-1] = tolower(cmdtext[pos]);
}
format(funcname, sizeof(funcname), "cmd_%s", funcname);
while (cmdtext[pos] == ' ') pos++;
if (!cmdtext[pos])
{
return CallLocalFunction(funcname, "is", playerid, "\1");
}
return CallLocalFunction(funcname, "is", playerid, cmdtext[pos]);
}
NOTE: Use it ONLY in the GAMEMODE script, because returning 1 in a filterscript will disable commands in other scripts.
pawn Code:
public OnPlayerCommandText(playerid, cmdtext[])
{
new
pos,
funcname[32];
while (cmdtext[++pos] > ' ')
{
funcname[pos-1] = tolower(cmdtext[pos]);
}
format(funcname, sizeof(funcname), "cmd_%s", funcname);
while (cmdtext[pos] == ' ') pos++;
if (!cmdtext[pos]) cmdtext[pos] = '\1';
if(!CallLocalFunction(funcname, "is", playerid, cmdtext[pos]))
{
SendClientMessage(playerid, -1, "Invalid command. Use /help");
}
return 1;
}
Example
Now you can use ZCMD with strcmp for example
pawn Code:
public OnPlayerCommandText(playerid, cmdtext[])
{
if(strcmp(cmdtext, "/help", true) == 0)
{
SendClientMessage(playerid, -1, "Available commands: ");
return 1;
}
if(strcmp(cmdtext, "/kill", true) == 0)
{
SetPlayerHealth(playerid, 0);
return 1;
}
new
pos,
funcname[32];
while (cmdtext[++pos] > ' ')
{
funcname[pos-1] = tolower(cmdtext[pos]);
}
format(funcname, sizeof(funcname), "cmd_%s", funcname);
while (cmdtext[pos] == ' ') pos++;
if (!cmdtext[pos])
{
return CallLocalFunction(funcname, "is", playerid, "\1");
}
return CallLocalFunction(funcname, "is", playerid, cmdtext[pos]);
}
Credits to ZeeX for the original ZCMD include.