[Include] cmdparam - The simplest command splitter
#1

Simplest + Extremely efficient
Command Splitter
Made by me
Link:
http://pastebin.com/BvUU5FjD

Код:
cmdparam(full[], first[], length=sizeof first)
full[] - the whole string
first[] - the output; the part of the string until the first ' ' (space)
length - the maximum size of first[]
Example usage:

Код:
public OnPlayerCommandText(playerid, cmdtext[])
{
	new command[32];
	cmdparam(cmdtext, command);
	printf("command: %s | params: %s", command, cmdtext);
}

public OnPlayerCommandText(playerid, cmdtext[])
{
	new command[32], param[2][128];
	cmdparam(cmdtext, command);
	cmdparam(cmdtext, param[0]);
	cmdparam(cmdtext, param[1]);
	printf("command: %s | param 1: %s | param 2: %s | param 3: %s", command, param[0], param[1], cmdtext);
}

public OnPlayerCommandText(playerid, cmdtext[])
{
    new command[32];
    cmdparam(cmdtext, command);
    if(strcmp("/pm", command, true) == 0)
    {
            new param_id[6];
	    cmdparam(cmdtext, param_id);
	    new id = strval(param_id);
            ........
            SendClientMessage(id, 0xffffffff, cmdtext);
    }
}
Is basically cuts off the first part of the string and puts it in another string.
* I didn't test any of the examples, they are just for demonstration.


Version 2 (optional):
This version is way more comfortable as long you use it right.

Link:
http://pastebin.com/3fskuTxc

Example:

Код:
    new cmdtext[128], param1[24], param2[24];
	format(cmdtext, 128, "/param1 param2 param3");
	param1 = cmdparam(cmdtext);
	param2 = cmdparam(cmdtext);
	printf("%s|%s|%s", param1, param2, cmdtext);
Will output: "/param1|param2|param3"

Код:
    new cmdtext[128], command[24], entrancefee, price;
	format(cmdtext, 128, "/createbiz 100 10000 Business Name");
	command = cmdparam(cmdtext);
	entrancefee = strval(cmdparam(cmdtext));
	price = strval(cmdparam(cmdtext));
	printf("%s|%d|%d|%s", command, entrancefee, price, cmdtext);
Will output: "/createbiz|100|10000|Business Name"

In case a player doesn't use the command properly, this:
Код:
    new cmdtext[128], command[24], entrancefee, price;
	format(cmdtext, 128, "/createbiz");
	command = cmdparam(cmdtext);
	entrancefee = strval(cmdparam(cmdtext));
	price = strval(cmdparam(cmdtext));
	printf("%s|%d|%d|%s", command, entrancefee, price, cmdtext);
Will output: "/createbiz|-1|-1|"
So you can identify when a player doesn't insert an integer value properly e.g. if(entrancefee == -1) return SendClientMessage(..);

Note:
the compiler will generate an error if you try to load a param with less than MAX_PARAM_LEN cells, which is legitimate because I can't think of a word/value with more than the set default of 24 chars. It doesn't affect the last param though, that should be used for the rest of the string that includes spaces such as a message or a name.
Reply


Messages In This Thread

Forum Jump:


Users browsing this thread: 1 Guest(s)