[Tutorial] Making A Simple /givecash Command
#1

Hello Guys,This Is My First Tutorial Here,

I See Many People Having A Problem In This Command Especially In SA-MP Forums And Nearly Everyone Having Problem In It.

Includes Needed:

sscanf (Can Be Downloaded Here)
ZCMD (Can Be Downloaded Here)

IMPORTANT NOTE:

For Using ZCMD,Then You Must Include It At The Top Of The Script Like This:
pawn Код:
#include <ZCMD>
1- The First Thing:

We Add These As The Header Of The Command:

pawn Код:
COMMAND:givecash(playerid, params[])
{
Or:
pawn Код:
CMD:givecash(playerid, params[])
{
Anywhere,And We Must Make A Public Function Using Special Pre-defined Macro

2- Then We Add Some Other Things:

We Will Add This:
pawn Код:
new giveplayerid, amount, Message[128], pName[MAX_PLAYER_NAME],pName1[MAX_PLAYER_NAME],Message1[128];
#define COLOR_RED 0xFF0000AA
#define COLOR_ORANGE 0xFF9500FF
Under
pawn Код:
CMD:givecash(playerid, params[])
{
new giveplayerid Means That We A Defining A New Integer To And We Will Use It For Giving A Player Money By His ID Like Example: /sendcash 2 20000 Means That I Am Sending The Cash To The Player ID 2,And The Next Value Is How Much I Am Sending Him From My Money, Then We Define 2 Variables Called pName And pName2 Because We Will Use Them Later To Get The Players' Names (1 Player That Will Receive The Money And The Other Will Send Money) While Formatting,Then We Define Other 2 Variables Called: Message[128] And Message1[128],We Will Use Them As Strings For Formatting Too.
We Will Use Them Later..

3- Using sscanf:

We Will Use It Because It Is Better And Easier Than strtok,So Under The Definitions We Will Put This:
pawn Код:
if (sscanf(params, "ud", giveplayerid, amount)) SendClientMessage(playerid, COLOR_RED, "Usage: /sendcash [playerid/partname] [amount]");
That Will Send A Message If The Player Didn't Type The Full Command.
How To Use Sscanf: Params Is The Paramters String,What Is "ud"? "u" Is User Takes a Name Or A Part Of A Name Who Performed This Command Already And Returns The ID If The Player Is Connected,"d" Is A Decimal,It Is Used For Getting A Normal Number Like Example: 2235245,We Use "d" For It Not "u",NOTE: If You Want To Make Your Command Usage Like This:
pawn Код:
USAGE: /sendcash [amount] [playerid/partname]
Then Replace The "giveplayerid"'s Place To "amount"'s Place It Will Be Like This:
pawn Код:
if (sscanf(params, "du", amount, giveplayerid)) SendClientMessage(playerid, COLOR_RED, "Usage: /sendcash [amount] [playerid/partname]");
But You Must Change "ud" To "du" If You Use This Function,But I Prefer The First Function Easier.

4- Checking If The Player That Will Receive The Money Is Connected:

We Will Check If The Player That Will Receive The Money Is Connected To Avoid Losing The Money With This Codes Under The Previous Codes:
pawn Код:
else if (!IsPlayerConnected(giveplayerid)) SendClientMessage(playerid, COLOR_RED, "Player Not Connected");
Note: There Is A Difference Between These Functions: !IsPlayerConnected(giveplayerid) And IsPlayerConnected(giveplayerid),!IsPlayerConnected (giveplayerid) Means That The Player Is Disconnected Already,But IsPlayerConnected(giveplayerid) Means That The Player Is Already Connected In The Server.

It Uses A Valid Integer Like: playerid,giveplayerid Or Anything But The Most Important That It Is An Integer Not A String To Avoid This Error:
pawn Код:
error 035: argument type mismatch (argument 1)
And It Must Be Defined As An Another Player ID.

5- Checking If The Player That Will Receive The Money = The Player That Sends The Money:

We Will Check If The Player That Will Receive The Money Equals The Player That Sends The Money To Avoid Being Confused Using These Codes Under The Previous Codes:
pawn Код:
else if (giveplayerid == playerid) SendClientMessage(playerid, COLOR_RED, "You Cannot Send Money To Yourself!");
Note: If You Put A String With An Integer,It Will Be Impossible And More Complicated.

6- Checking If The Amount That The Player Has Type Equals Or Smaller Than He Has Got To Send:

Sometimes,When You Forget This,You Will Have Negative Sign Near Your Money When You Type Amount Of Money To Send Larger Than The Amount Of Money You Got,So I Suggest To Use This Code, Too, Under The Previous Codes:
pawn Код:
else if (amount > GetPlayerMoney(playerid)) SendClientMessage(playerid, COLOR_RED, "You Don't Have That Much!");
Note:

Remember, This Sign ">" Means That Something Larger Than Something Else,But This Sign "<" Means That Something Is Smaller Than Something Else,But Here Is The Thing, In Some Codes,You Sometimes See "<=" Means That Something Is Smaller Or Equal Than (To) Something Else,But This ">=" Means That Something Is Larger Or Equal Than (To) Something Else.

(Optional) 7- The Limit Of Sending Money Once:

This Is Not Required To Be Used,But I Use It As A Limit Of Sending A Money Once,Anyway,The Limit Of Sending Money Once I Use Is 500K ($500000),Here Is The Code Of It:
pawn Код:
else if (amount > 500000) SendClientMessage(playerid, COLOR_RED, "You Cannot Send More Than $500000!");
NOTE: Sometimes,For Get The Length Of The String,We Use Strlen Function In Strings Because It May Not Work With Normal Getting Lengths Like This: if(Message > 500000) That Will Return This Error:
pawn Код:
error 033: array must be indexed (variable "Message")
Even If You Added [0] Near The Message It Seems Like The Error Gone But It Won't Be Performed And It Will Bug.

(Optional) 8- Avoid Sending Negative Money (EX: Sending -10000):

When I Didn't Make This Idea,The Command Made A Problem In My Server,If You Sent A Negative Number (Example (EX): /givecash [playerid] -10000) This Will Cause Decreasing The Money Of The Player That Should Receive Money,If You Want To Add It,Here Is The Codes For It:
pawn Код:
else if (amount < 0) SendClientMessage(playerid, COLOR_RED,"Wrong Value!");
This Code Checks If The Amount Of The Player That Has Type To Send If It's Smaller Than 0.

NOTE: "-" Is A Negative Sign Like Example: -250000,It Is Famous To Be Used On "GivePlayerMoney" Function,But "+" Is A Positive Sign Like Example: +250000,But It Is Better To Be Used Like This: 250000 Normally In Example: GivePlayerMoney Function.

9- The Main Codes:

Under All These Things That We Have Added,We Will Add This:
pawn Код:
else
    {
        GetPlayerName(playerid,pName,MAX_PLAYER_NAME);
        GetPlayerName(giveplayerid,pName1,MAX_PLAYER_NAME);
        GivePlayerMoney(giveplayerid, amount);
        GivePlayerMoney(playerid, -amount);
        format(Message, sizeof(Message), "Received $%d From %s (%d)!",amount, pName, playerid);
        SendClientMessage(giveplayerid,COLOR_ORANGE, Message);
        format(Message1, sizeof(Message1), "Successfully Sent $%d To %s (%d)!",amount, pName1, giveplayerid);
        SendClientMessage(playerid,COLOR_ORANGE, Message1);
    }
    return 1;
}
The Parameters Of GetPlayerName Is: (playerid, const name [], len), And We Use It To Get The Players Name (As I Said Earlier),const name [] Means An Array Into Which To Store The Name And It's Passed By Reference Like Example: pName[MAX_PLAYER_NAME] While Defining It, It Can Be Used As A Const Name In GetPlayerName Function,len Is The Length Of The String That Should Be Stored. Recommended To Be "MAX_PLAYER_NAME".

GivePlayerName Function Is Used To Give The Player Amount Of Money,The Parameters Of It: (playerid, money)
playerid Is The ID Of The Player That We Use To Take The Money From Or Give.
Money Is The Amount Of The Money That If We Want To Give Money,We Type Like Example: 220000, If We Want To Take Money,We Put Minus (Negative) Sign Near The Numbers Like Example: -220000.

Format Function It Is Used For Formatting A String To Include Variables and Other Strings Inside It,Example: We Are Formatting Message[128] To Be Used In SendClientMessage,SendClientMessageToAll,Or Sometimes With Other Functions,REMEMBER: It Is Used For Formatting Strings Not Integers,The Parameters Of Format: (output[], len, const format[], {Float,_}:...),What Is output[]? It Is The String To Output The Result To,Like Example: Message,What Is len? (We Have Already Said It In The Parameters Of GetPlayerName Function) It Is The Maximum Length Output Can Contain Like Example: sizeof(Message) We Must Use That To Strings,What Is const format []?
It Is The Format String To Use It Like Sending A Message,Printing On The Server Logs,And More,And It's Like: "Received $%d From %s (%d)!", What Is {Float,_}:...? It Is The Indefinite Number Of Arguments Of Any Tag Like Example: ,amount, pName1, giveplayerid,Then In The Main Part Of It,The Format Specifiers: %d Is Used For Integer (Whole Number). %i Is Used For Integer (Whole Number) But The Prefered Is With %d, %s Is Used For String, %f Is Used For Floating-Point Number (Float: Tag), %c Is Used For ASCII character (Not Popular), %x Is Used For Hexadecimal number (Not Popular), %b Is Used For Binary number, %% Is Used For Literal '%' (Not Popular) And All of These Specifiers Can Be Used For Making A Tag For It Like Defining Variables,Example Of It: format(Message, sizeof(Message), "Received $%d From %s (%d)!",amount, pName16, playerid);

It Will Be Sent As Example: Received $20000 From Youssef (0)!

IMPORTANT: This Is Only Formatting,For Sending The Message,You Must Use "SendClientMessage" Or "SendClientMessageToAll" Function, Like Example: SendClientMessage(giveplayerid,0xFF9900AA, Message); , We Put Message Instead Of "messagehere" Because It Is Already Formatted And It Can Be Used For Sending Messages,To Know How To Use SendClientMessage,It Is Used To Send A Message To A Specific Player With Chosen Color In The Chat, Like Example:
pawn Код:
public OnPlayerConnect(playerid)
{
    SendClientMessage(playerid, -1, "Hello! This Is Youssef.");
    return 1;
}
This Will Send The With The White Color,Better To Know The Parameters Now: (playerid, color, const message[])
playerid Is Used For The ID Of The Player To Send The Message To.

color Is Used To Display The Color Of The Message,You Can Find And Make And Define Colors Using This Link: http://colorpicker.com/ And Then When You Add It To Your Script,Add In The Left Of It: 0x And Then In The Right Of It Add: FF Or AA Like Example: We Got This Color HTML: D7F6A1 We Add The Things To It That I Have Said,When We Add 0x It Will Be Like: 0xD7F6A1,And When We Add AA It Will Be Like This With 0x: 0xD7F6A1AA Or When We Add FF It Will Be Like This With 0x: 0xD7F6A1FF.

const message[]) Is Used To Write What Is The Text That Should Be Sent To The Player Like Example: "Hello! This Is Youssef.",Or Simply,Putting A Formatted String In It's Place Like Example: Message Without The " ", Example: SendClientMessage(playerid, -1, Message); Or SendClientMessage(playerid, 0xD7F6A1FF, Message);

NOTE: -1 Is The White Color.

Ok All These Codes If You Have Something Wrong Or Want To Check Something,Here Is Full Codes Of It:
pawn Код:
CMD:givecash(playerid, params[])
{
    new giveplayerid, amount, Message[128], pName16[MAX_PLAYER_NAME],pName17[MAX_PLAYER_NAME],Message1[128];
    #define COLOR_RED 0xFF0000AA
    #define COLOR_ORANGE 0xFF9500FF
    if (sscanf(params, "ud", giveplayerid, amount)) SendClientMessage(playerid, COLOR_RED, "Usage: /givecash [playerid/partname] [amount]");
    else if (!IsPlayerConnected(giveplayerid)) SendClientMessage(playerid, COLOR_RED, "Player Not Connected");
    else if (giveplayerid == playerid) SendClientMessage(playerid, COLOR_RED, "You Cannot Send Money To Yourself!");
    else if (amount > GetPlayerMoney(playerid)) SendClientMessage(playerid, COLOR_RED, "You Don't Have That Much!");
    else if (amount > 500000) SendClientMessage(playerid, COLOR_RED, "You Cannot Send More Than $500000!");
    else if (amount < 0) SendClientMessage(playerid, COLOR_RED,"Wrong Value!");
    else
    {
        GetPlayerName(playerid,pName,MAX_PLAYER_NAME);
        GetPlayerName(giveplayerid,pName1,MAX_PLAYER_NAME);
        GivePlayerMoney(giveplayerid, amount);
        GivePlayerMoney(playerid, -amount);
        format(Message, sizeof(Message), "Received $%d From %s (%d)!",amount, pName, playerid);
        SendClientMessage(giveplayerid,COLOR_ORANGE, Message);
        format(Message1, sizeof(Message1), "Successfully Sent $%d To %s (%d)!",amount, pName1, giveplayerid);
        SendClientMessage(playerid,COLOR_ORANGE, Message1);
    }
    return 1;
}

Hope I Have Helped,

Regards,

Youssef Ahmad.
Reply
#2

You need to explain more in some parts. Saying "Add this and that" doesn't help that much.

Also since you stated that strtok is bad (and it is indeed) and you used sscanf. You should've used ZCMD or y_commands instead of dcmd (yes - it's slower).
Reply
#3

Sorry,Because this is my first tutorial..
Reply
#4

Even newbies can make these simple commands by referring to SA-MP Wiki.
Reply
#5

Quote:
Originally Posted by Sojo12
Посмотреть сообщение
Even newbies can make these simple commands by referring to SA-MP Wiki.
True, but people could be more at ease looking at tutorials instead of just reading stuff from the wiki (like the callbacks)

Good tutorial anyhow!
Reply
#6

Good tutorial.
Well explained !
Reply
#7

good
Reply
#8

now it is updated now with V1.2,please feedback
Reply
#9

Quote:
Originally Posted by Youssef214
Посмотреть сообщение
V1.2
Much better.
Everything was good until step 9, there you had a text which was too big. Under that else code.
But good one as a first.
Reply
#10

Thanks For Your Feedback
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)