rCmd.inc - Easiest way to create commands! [NEW v0.2.0 ─ 6/01/2013] -
RyDeR` - 06.03.2012
Introduction
You're probably like "Oh no, another command system!" - Yes, but this one is different than all others and I'm pretty sure you'll like it! You can use dynamic parameters in your command function which makes everything faster and easier to use for you. The best thing is, in this new version,
sscanf2 plugin is integrated and it automatically makes use of it. See more below!
Syntax
Код:
rCmd["specifiers"]->commandname(playerid, success, ...)
NEW (IMPORTANT NOTES):
- You can use all sscanf specifiers in the specifiers input, yes, literally whole sscanf.
- Note the quotes around specifiers in this new version. It's not necessary, but when you use brackets for optional parameters, it will give you errors without the quotes.
- You have to use & in front of all non-array variables (floats, integers, ..., NOT playerid and success) because DynamicParams.inc is passes the address of the value. If you skip this, the value of the integer/float will be wrong. Example:
Код:
rCmd["us[24]"]->setname(playerid, success, &targetid, name[]) {
// ...
return 1;
}
Specifiers
Simple answer, sscanf is integrated with this new version, so just use it as you would use sscanf, it will behave the same, without any problems.
Example(s)
pawn Код:
rCmd["uiI(500)"]->giveweapon(playerid, success, &targetid, &weaponid, &ammo) {
if(!success) return SendClientMessage(playerid, 0xFF0000FF, "ERROR: /giveweapon [player id/name] [weapon] [ammo (default 500)]");
if(targetid == INVALID_PLAYER_ID) return SendClientMessage(playerid, 0xFF0000FF, "ERROR: Player is not connected!");
GivePlayerWeapon(targetid, weaponid, ammo);
SendClientMessage(playerid, -1, "You gave the player a weapon!");
return 1;
}
Note the beastliness of sscanf integrated (with optional parameters and stuff).
pawn Код:
rCmd["us[24]"]->setname(playerid, success, &targetid, name[]) {
if(!success) return SendClientMessage(playerid, 0xFF0000FF, "ERROR: /setname [player id/name] [name]");
if(targetid == INVALID_PLAYER_ID) return SendClientMessage(playerid, 0xFF0000FF, "ERROR: Player is not connected!");
SetPlayerName(targetid, name);
SendClientMessage(playerid, -1, "You changed the players name!");
return 1;
}
Please make sure to use
& before all your non-array variables (integers, floats, ...,
NOT playerid and success).
pawn Код:
rCmd["iI(-1)I(-1)"]->vehicle(playerid, success, &model, &color1, &color2) {
if(!success) return SendClientMessage(playerid, 0xFF0000FF, "ERROR: /vehicle [model] [color 1 (default -1)] [color 2 (default -1)]");
if(!(400 <= model <= 611)) return SendClientMessage(playerid, 0xFF0000FF, "ERROR: Invalid vehicle model!");
new Float: x, Float: y, Float: z;
GetPlayerPos(playerid, x, y, z);
CreateVehicle(model, x, y, z, 0.0, color1, color2, 60);
SendClientMessage(playerid, -1, "You spawned a vehicle!");
return 1;
}
Beautiful, isn't it?
pawn Код:
rCmd[]->time(playerid) {
new
szTime[32]
;
gettime(szTime[0], szTime[1], szTime[2]);
format(szTime, sizeof(szTime), "<> Time: %02d:%02d:%02d", szTime[0], szTime[1], szTime[2]);
SendClientMessage(playerid, 0x00FF00FF, szTime);
return 1;
}
Note that there are no parameters.
pawn Код:
rCmd["uF(100.0)"]->sethealth(playerid, success, &targetid, &Float: health) {
if(!success) return SendClientMessage(playerid, 0xFF0000FF, "ERROR: /sethealth [player id/name] [health (default = 100.0)]");
if(targetid == INVALID_PLAYER_ID) return SendClientMessage(playerid, 0xFF0000FF, "ERROR: Player is not connected!");
if(!(0.0 <= health <= 100.0)) return SendClientMessage(playerid, 0xFF0000FF, "ERROR: Health has to be between 0.0 and 100.0!");
SetPlayerHealth(playerid, health);
SendClientMessage(playerid, -1, "You have set players health!");
return 1;
}
Some float example. Again, please don't forget the
& sign!
Callback(s)
We have two callbacks:
pawn Код:
forward OnPlayerCommandReceived(playerid, cmdtext[]);
This gets called before the actual command gets executed. So returning 0 here means the command won't be executed.
pawn Код:
forward OnPlayerCommandPerformed(playerid, cmdtext[], success);
This gets called after the command gets executed. Returning 0 in here will result in standart "SERVER: Unknown Command" message. You can adjust that message under this callback.
As you probably may have noticed, I used the same callbacks as used in zcmd as they were pretty useful.
pawn Код:
public OnPlayerCommandReceived(playerid, cmdtext[]) {
if(!strcmp(cmdtext, "/setname", true, 8)) {
SendClientMessage(playerid, 0xFF0000FF, "ERROR: This command is not enabled right now!");
return 0; // Command won't be executed
}
return 1;
}
pawn Код:
public OnPlayerCommandPerformed(playerid, cmdtext[], success) {
if(!success) {
return SendClientMessage(playerid, 0xFF0000FF, "ERROR: That command is unknown!");
}
return 1;
}
Download
- rCmd.inc
- DynamicParams.inc has also been updated, please make sure you redownload this!
- You can download sscanf here! Also, I want to thank ****** for this beautiful plugin!
Notes- When a command doesn't work, it most probably means that the value MAX_FUNCTIONS (1024) has been exceeded. Just increase it and recompile it and you should be good to go.
- The max length of the specifiers you can use is currently set to MAX_SSCANF_FORMAT (32). If you need longer specifiers, just increase it.
- You can use MAX_DYNAMIC_PARAMS (16) parameters with the current settings. Just increase if you would ever need more than 16 parameters.
Changelog
- 07/03/2012 - v0.1.0:
- 08/03/2012 - v0.1.1:
- Fixed some important bugs. Please re-download if you're using it.
- 21/04/2012 - v0.1.2:
- Changed the whole syntax so there's not rCmd_Init anymore (thanks ******):
Код:
rCmd[specifiers]->commandname(playerid, success, ...)
- Added some new useful specifiers h, b and u:
String | Character | Integer | Float | User name/id | Hex | Binary |
s | c | i or d | f | u | h | b |
- Fixed a bug where the stack didn't get restored when your command was incomplete.
- 06/01/2013 - v0.2.0:
- Rewrote the whole include, as well as DynamicParams.inc. I found some critical bugs that made this include completely worthless, but I'm sure it's completely fixed now and is %100 to use.
- sscanf plugin is fully integrated now and automatically makes use of all features of the sscanf plugin.
- Just make sure you read "Syntax" part of the topic again as there are some important things to note:
Quote:
Syntax
Код:
rCmd["specifiers"]->commandname(playerid, success, ...)
NEW (IMPORTANT NOTES):- You can use all sscanf specifiers in the specifiers input, yes, literally whole sscanf.
- Note the quotes around specifiers in this new version. It's not necessary, but when you use brackets for optional parameters, it will give you errors without the quotes.
- You have to use & in front of all non-array variables (floats, integers, ..., NOT playerid and success) because DynamicParams.inc is passes the address of the value. If you skip this, the value of the integer/float will be wrong. Example:
Код:
rCmd["us[24]"]->setname(playerid, success, &targetid, name[]) {
// ...
return 1;
}
|
Have fun using this!
Re: rCmd.inc - Easiest way to create commands! -
Kar - 06.03.2012
Wow, nice job! finally a new style of creating commands!
Respuesta: rCmd.inc - Easiest way to create commands! -
admantis - 06.03.2012
Wow.. I never expected something like this, i'm going to use it on my next script you know that.
Re: rCmd.inc - Easiest way to create commands! -
Ballu Miaa - 07.03.2012
Wow.
sscanf's use will become less but still gonna use sscanf
Re: rCmd.inc - Easiest way to create commands! -
Michael@Belgium - 07.03.2012
Nice work ! Maybe this will be my next use xp
Re: rCmd.inc - Easiest way to create commands! -
tyler12 - 07.03.2012
wow nice!
Respuesta: rCmd.inc - Easiest way to create commands! -
Jovanny - 07.03.2012
Good job RyDeR`
Re: rCmd.inc - Easiest way to create commands! -
Lorenc_ - 07.03.2012
I shall convert soon my friend <3
EDIT: I'm blind if you have posted this already, but any speed results?
Re: rCmd.inc - Easiest way to create commands! -
Jack_Wilson - 07.03.2012
Looks nice but I don't want to use it as I'm used to the simple zcmd and sscanf.
Re: rCmd.inc - Easiest way to create commands! -
Stylock - 07.03.2012
I think this would be way better if you combined it with sscanf to support optional parameters, arrays etc.
Re: rCmd.inc - Easiest way to create commands! -
System64 - 07.03.2012
Wow, very cool
rep+
also, can you make some benchmarks with ycmd, zcmd etc.. ?
Re: rCmd.inc - Easiest way to create commands! -
CyNiC - 07.03.2012
I thought, "is just more a command processor", but you had a nice idea with these params pre-included.
Re: rCmd.inc - Easiest way to create commands! -
RyDeR` - 07.03.2012
Thanks! That's the power of emit.
I have no idea about the performance but this is obviosly slower than zcmd. Will do a test later though. Don't mind about the performance.
Re: rCmd.inc - Easiest way to create commands! -
Calgon - 07.03.2012
Nice idea, I didn't think I'd ever see parameter splitting in the header of a command.
Re: rCmd.inc - Easiest way to create commands! -
Shadow_ - 07.03.2012
Very nice RyDeR`
Re: rCmd.inc - Easiest way to create commands! -
RyDeR` - 07.03.2012
Added the specifiers in the first post.
Small update:
Quote:
Changelog
- 08/03/2012 - v0.1.1:
- Fixed some important bugs. Please re-download if you're using it.
|
Re: rCmd.inc - Easiest way to create commands! -
Modrlicc - 07.03.2012
Amazing! This will make creating commands much more easier.
Re: rCmd.inc - Easiest way to create commands! -
Modrlicc - 07.03.2012
Sorry double post.
Re: rCmd.inc - Easiest way to create commands! -
RyDeR` - 08.03.2012
I have some benchmarks:
pawn Code:
[Benchmark] 'signle_int_zcmd' executed about 164.09 times/ms.
[Benchmark] 'signle_int_rcmd' executed about 109.70 times/ms.
[Benchmark] 'signle_string_zcmd' executed about 150.49 times/ms.
[Benchmark] 'signle_string_rcmd' executed about 72.40 times/ms.
[Benchmark] 'no_params_zcmd' executed about 187.40 times/ms.
[Benchmark] 'no_params_rcmd' executed about 136.98 times/ms.
[Benchmark] 'mixed_params_zcmd_sscanf' executed about 86.96 times/ms.
[Benchmark] 'mixed_params_rcmd' executed about 49.35 times/ms.
In most cases it's ~2 times slower than zcmd. It's actually still pretty fast! I will see in future versions if I'll be able to increase performance. But again, those results are nothing you should really worry about.
Respuesta: rCmd.inc - Easiest way to create commands! -
[DOG]irinel1996 - 08.03.2012
Really good job RyDeR`.
But I prefer zcmd.
Anyway, good job! 5/5