SA-MP Forums Archive
[Include] rCmd.inc - Easiest way to create commands! - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Filterscripts (https://sampforum.blast.hk/forumdisplay.php?fid=17)
+---- Forum: Includes (https://sampforum.blast.hk/forumdisplay.php?fid=83)
+---- Thread: [Include] rCmd.inc - Easiest way to create commands! (/showthread.php?tid=323777)

Pages: 1 2 3 4 5 6


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): 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
Notes Changelog
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