28.01.2012, 23:04
(
Last edited by Mosslah; 02/02/2012 at 01:28 PM.
)
Making Commands with Parameters!
A Tutorial by Mosslah.
A Tutorial by Mosslah.
A lot of people usually pop up the question here and there with how to make commands with parameters, and how to create parameters within the original parameter for that command, so here it goes.
The first thing you want to do is create your command, the command we're going to be basing this around is a simple command that may have usage in some servers, but for this tutorial, it'll just guide you through the steps.
Inside this Tutorial!
- Building the basics of a command.- Adding parameters to a basic command.
- Adding parameters within parameters.
- Creating a simple /give money command, with parameters.
- String usage (not detailed, as there are plenty of string tutorials out there).
Building the Basics!
So, let's type our command out using sscanf and zcmd (if you don't have these, then you should download them right away as they are two of the best components for any script at the moment). We'll add the regular includes at the top, and two colours that we're going to define.pawn Code:
#include <a_samp>
#include <zcmd>
#include <sscanf2>
#define COLOR_WHITE 0xFFFFFFAA
#define COLOR_RED 0xAA3333AA
COMMAND:give(playerid, params[])
{
return 1;
}
pawn Code:
COMMAND:give(playerid, params[])
{
new option[10];
if(sscanf(params, "s[10]", option)) return SendClientMessage(playerid, COLOR_RED, "USAGE: /give [money | weapon]");
return 1;
}
The "new option[10];" is us adding the variable which is the 'option' part of our sscanf line. This is needed otherwise PAWNO will return an error, and also; the command will not work if it is not mentioned in the script. The [10] after the variable is the maximum amount of characters allowed in the string which follows the command, and is also added after the "s" in the sscanf line (the s stands for string, see: Sscanf). We've now got everything we need to start adding the actual functions of the command, and begin to create the parameters.
Creating the Options!
Let's begin by adding the two parameters which we want to add in to our /give command, which are "money" and "weapon". We do this by utilizing strcmp, which compares two strings to see if they are the same, and it is what we will be using for our parameters.We're going to add our parameters, starting in the same column as our sscanf line.
pawn Code:
COMMAND:give(playerid, params[])
{
new option[10];
if(sscanf(params, "s[10]", option)) return SendClientMessage(playerid, COLOR_RED, "USAGE: /give [money | weapon]");
if(!strcmp(option, "money", true))
{
return 1;
}
if(!strcmp(option, "weapon", true))
{
return 1;
}
return 1;
}
pawn Code:
if(!strcmp(option, "money", true))
Now we have the two parameters inside of our command, but again; they do nothing. However, to complicate this even further, we're now going to be adding parameters inside of our previous parameters (It's not as confusing as it seems).
Adding Parameters Within Parameters!
Giving a player money, or a weapon, cannot just be done with the command that we've previously made, it has to have it's own command inside of it. So, now we're going to build the parameters for our first parameter, "money".We'll start in the same way that we started the initial command, adding a sscanf function to add our new parameters.
pawn Code:
if(!strcmp(option, "money", true))
{
new TargetID,
Cash;
if(sscanf(params, "ui", TargetID, Cash)) return SendClientMessage(playerid, COLOR_RED, "USAGE: /give money [PlayerID] [Cash]");
return 1;
}
We're then going to check if the playerid that was added into the command actually exists, and someone hasn't just entered a random ID of a player that is not actually online, we do this by using:
pawn Code:
if(TargetID == INVALID_PLAYER_ID) return SendClientMessage(playerid, COLOR_RED, "Invalid Player ID!");
We're now going to repeat this process (not exactly) for the weapon part of our /give command, see if you can do it yourself first, and then check with the part below if you managed to do it correctly!
Hopefully, you should have this:
pawn Code:
if(!strcmp(option, "weapon", true))
{
new TargetID;
if(sscanf(params, "u", TargetID)) return SendClientMessage(playerid, COLOR_RED, "USAGE: /give weapon [PlayerID]");
{
if(TargetID == INVALID_PLAYER_ID) return SendClientMessage(playerid, COLOR_RED, "Invalid Player ID!");
return 1;
}
}
Again, we're following the same steps by checking if the ID is valid.
Now that this is done, we've pretty much created our parameters - but we still need to build a command. I'm going to quickly jump through a basic pay command inside of this parameter, for those of you who may wish to use this in your own script(s). It's simple really, and is basic PAWNO knowledge.
Creating Our Actual Commands!
So let us start off with the money parameter that we created. We're firstly going to check if the player has the right amount of cash, and then we're going to send that cash to the other player with two strings so that both players know what has happened - to avoid confusion.We're going to start off by checking if the numerical value entered under the "Cash" variable is actually cash that the player who did the command has, we're going to do this simply with a single line:
pawn Code:
if(GetPlayerMoney(playerid) < Cash) return SendClientMessage(playerid, COLOR_RED, "You do not have that much money!");
So, let's create some new variables for our strings and names, and we'll start with making the strings.
pawn Code:
new paystring[128],
paidstring[128],
player[MAX_PLAYER_NAME],
target[MAX_PLAYER_NAME];
Building the string:
Getting the names, and storing them into our variables:
pawn Code:
GetPlayerName(playerid, player, sizeof(player));
GetPlayerName(TargetID, target, sizeof(target));
pawn Code:
format(paystring, sizeof(paystring), "You have given $%i to %s.", Cash, target);
format(paidstring, sizeof(paidstring), "%s has paid you %i.", player, Cash);
pawn Code:
SendClientMessage(playerid, COLOR_WHITE, paystring);
SendClientMessage(TargetID, COLOR_WHITE, paidstring);
pawn Code:
GivePlayerMoney(playerid, -Cash);
GivePlayerMoney(TargetID, Cash);
On completion, your code should now look like this:
pawn Code:
#include <a_samp>
#include <zcmd>
#include <sscanf2>
#define COLOR_WHITE 0xFFFFFFAA
#define COLOR_RED 0xAA3333AA
COMMAND:give(playerid, params[])
{
new option[10];
if(sscanf(params, "s[10]", option)) return SendClientMessage(playerid, COLOR_RED, "USAGE: /give [money | weapon]");
if(!strcmp(option, "money", true))
{
new TargetID,
Cash;
if(sscanf(params, "ui", TargetID, Cash)) return SendClientMessage(playerid, COLOR_RED, "USAGE: /give money [PlayerID] [Cash]");
{
if(TargetID == INVALID_PLAYER_ID) return SendClientMessage(playerid, COLOR_RED, "Invalid Player ID!");
if(GetPlayerMoney(playerid) < Cash) return SendClientMessage(playerid, COLOR_RED, "You do not have that much money!");
new paystring[128],
paidstring[128],
player[MAX_PLAYER_NAME],
target[MAX_PLAYER_NAME];
GetPlayerName(playerid, player, sizeof(player));
GetPlayerName(TargetID, target, sizeof(target));
format(paystring, sizeof(paystring), "You have given $%i to %s.", Cash, target);
format(paidstring, sizeof(paidstring), "%s has paid you %i.", player, Cash);
SendClientMessage(playerid, COLOR_WHITE, paystring);
SendClientMessage(TargetID, COLOR_WHITE, paidstring);
GivePlayerMoney(playerid, -Cash);
GivePlayerMoney(TargetID, Cash);
return 1;
}
}
if(!strcmp(option, "weapon", true))
{
new TargetID;
if(sscanf(params, "u", TargetID)) return SendClientMessage(playerid, COLOR_RED, "USAGE: /give weapon [PlayerID]");
{
if(TargetID == INVALID_PLAYER_ID) return SendClientMessage(playerid, COLOR_RED, "Invalid Player ID!");
// Your own weapon code here.
return 1;
}
}
return 1;
}
Final Words!
Thank you reading, and please don't critique too much; this is my first tutorial, and the reason I wrote this was because when I first started scripting, I was never told how to create commands with parameters through a simple tutorial - and instead, I had to learn myself by messing around with a few things from sscanf.I hope that people will actually use this and I intend to create more in the future, on much more useful things rather than something as basic as this, but we all start some where - both tutorial makers, and scripters.
Once again; thanks for reading!