06.01.2013, 21:25
(
Last edited by JustinAn; 19/07/2013 at 11:53 PM.
)
Creating A /givemoney Command
Introduction
Hello guys, and today I'll be showing my tutorial on creating a /givemoney command using sscanf, zcmd. (Most useful in my opinion).
Getting Started
First, your going to want to define the include 'ZCMD', a fast command processor. Next you're going to want to define a include called 'sscanf2'.
pawn Code:
#include <ZCMD>
#include <sscanf2>
Here are the links to downloading the includes.
- sscanf2
- zcmd
You will also need to define some colors.
Here is defining the color 'COLOR_LIGHTBLUE'.
pawn Code:
#define COLOR_LIGHTBLUE 0x33CCFFAA
The structure in starting an command is this, there are several ways into doing this. But I will just be only explaining the latest structure that is formed.
pawn Code:
CMD:commandhere(playerid, params[])
{
return 1;
}
The params[] is the parameters string, playerid is an ID of the player who sends the command.
pawn Code:
CMD:commandhere(playerid, params[]) //Structure to begin an command.
This defines the name, it will get the playerid's name. And will replace the '_' underscore into nothing so FirstName LastName. Which is mostly useful for RolePlay servers.
pawn Code:
stock GetName(playerid)
{
new
name[24];
GetPlayerName(playerid, name, sizeof(name));
strreplace(name, '_', ' ');
return name;
}
Now, the command we're doing today is the /givemoney command which was said in the Introduction section.
Here is the code, and I will explain the whole code after this.
pawn Code:
CMD:givemoney(playerid, params[])
{
if(IsPlayerAdmin(playerid))
{
new string[128], targetid, money;
if(sscanf(params, "ud", targetid, money)) return SendClientMessage(playerid, COLOR_WHITE, "USAGE: /givemoney [playerid] [money]");
if(IsPlayerConnected(targetid))
{
GivePlayerMoney(targetid, money);
format(string, sizeof(string), "* You have set %s's cash to an amount of $%d.",GetName(targetid),money);
SendClientMessage(playerid, COLOR_LIGHTBLUE, string);
}
}
else
{
SendClientMessage(playerid, -1, "You are not authorized to use that command!");
}
return 1;
}
pawn Code:
CMD:givemoney(playerid, params[])
pawn Code:
if(IsPlayerAdmin(playerid))
Now, this code is defining a string, the target who sent the command and the amount of money.
pawn Code:
new string[128], targetid, money;
This is the part where we use sscanf, now. The 'if(sscanf(params' is the part which is also like 'isnull'. The "ud", these are the specifiers. 'u' is the playerid that the person who sent the command places the playerid who he/she wants to give the money to, the 'd' is the integer, which is for the money where the person who sends the command to give a certain amount to the player that he/she wants to. 'return SendClientMessage(playerid, COLOR_WHITE, "USAGE: /givemoney [playerid] [money]");' Is showing if they typed in a wrong parameter, it will guide them to showing them how to use the command correctly, just like 'isnull'. It will send the person a message, in white and saying the usage on how to use the command.
pawn Code:
if(sscanf(params, "ud", targetid, money)) return SendClientMessage(playerid, COLOR_WHITE, "USAGE: /givemoney [playerid] [money]");'
Specifiers from Y_Less.
Code:
Specifier(s) Name Example values i, d Integer 1, 42, -10 c Character a, o, * l Logical true, false b Binary 01001, 0b1100 h, x Hex 1A, 0x23 o Octal 045 12 n Number 42, 0b010, 0xAC, 045 f Float 0.7, -99.5 g IEEE Float 0.7, -99.5, INFINITY, -INFINITY, NAN, NAN_E u User name/id (bots and players) Y_Less, 0 q Bot name/id ShopBot, 27 r Player name/id Y_Less, 42
pawn Code:
if(IsPlayerConnected(targetid))
pawn Code:
GivePlayerMoney(targetid, money);
We're formatting the string, saying '* You have set %s's cash to an amount of $%d.' Now, the %s is the player's name, that person who executed the command that he wants to send the cash to. Now the %d is the integer, where the amount of money that the person who sended the certain amount of cash. After that, it will show 'GetName', this get's the name of the person who is receiving the cash. The money is the amount of money where the person who sends the amount of cash to, to the person he/she wants.
pawn Code:
format(string, sizeof(string), "* You have set %s's cash to an amount of $%d.",GetName(targetid),money);
pawn Code:
SendClientMessage(playerid, COLOR_LIGHTBLUE, string);
It will send the player a message in white which is '-1' by the way, saying 'You are not authorized to use that command!'
pawn Code:
else
{
SendClientMessage(playerid, -1, "You are not authorized to use that command!");
}
Conclusion
And that's it for today in my tutorial on creating the /givemoney command ONLY for logged in RCON admins.
Credits to...
- Y_Less for creating such a wonderful sscanf include.
- Zeex for creating the wonderful ZCMD include.
- And me for creating this tutorial.