[Tutorial] How to make a calculate command
#1

How to make a calculate command



~Introduction
Hello SA-MP scripters. Today I will show you how to make a calculate command using ZCMD(by Zeex), sscanf2(by ******). This is a bit advanced way, so to understand, you shall know the basics of PAWN scripting.

~Requirements
For this tutorial, what you need is sscanf2 and ZCMD.

~Getting started
As I said above, you need to understand a bit of PAWN coding. I suggest you using this if you don't understand something.So what are we waiting? Let's get started.


STEP 1.
First of all, let's include our includes and define 2 colors, White and Grad/Grey.
pawn Код:
// Includes
#include <zcmd>
#include <sscanf2>

// Defines
#define CWHITE     0xFFFFFFAA
#define CGRAD      0xBFC0C2FF
STEP 2.
Let's start with creating the command. Here, we will use the command /calculate, so we will do it like this:
pawn Код:
CMD:calculate(playerid, params[])
{
    return 1;
}
STEP 3.
Let's start with the defining. We will define 4 things. These are: "string" for the Client Messges, "method" for the method of calculating(Multiple, divide etc.), "value1" for the first value and "value2" for the second value.
pawn Код:
CMD:calcullate(playerid, params[])
{
    new string[128], method[20], value1, value2;
    return 1;
}

STEP 4.
Now, we will use the sscanf function. I can't tell many things for the sscanf, but you may find information for it here.
pawn Код:
CMD:calcullate(playerid, params[])
{
    new string[128], method[20], value1, value2;
    if(sscanf(params, "ds[20]d", value1, method, value2))
    {
        SendClientMessage(playerid, CWHITE, "[USAGE]: /calculate [value] [operation] [value].");
        SendClientMessage(playerid, CGRAD, "Operations: Add, Subtract, Multiply, Divide.");
        return 1;
    }
    return 1;
}
STEP 5.
Now let's start making the command's most necessery things. Here we are going to use the known "strcmp" function. You can find about it here.
pawn Код:
CMD:calcullate(playerid, params[])
{
    new string[128], method[20], value1, value2;
    if(sscanf(params, "ds[20]d", value1, method, value2))
    {
        SendClientMessage(playerid, CWHITE, "USAGE: /calculate [value] [operation] [value].");
        SendClientMessage(playerid, CGRAD, "Operations: Add, Subtract, Multiply, Divide.");
        return 1;
    }
   
    if(strcmp(method,"multiply",true) == 0 || strcmp(method,"*",true) == 0)
    {
        // Continued code below
    }
    if(strcmp(method,"divide",true) == 0 || strcmp(method,"/",true) == 0)
    {
        // Continued code below
    }
    if(strcmp(method,"add",true) == 0 || strcmp(method,"+",true) == 0)
    {
        // Continued code below
    }
    if(strcmp(method,"subtract",true) == 0 || strcmp(method,"-",true) == 0)
    {
        // Continued code below
    }
    return 1;
}
STEP 6.
This is the last step. Now let's add the code where I wrote "Continued code below". I will explain how they work on the code, so take a look at the comments. Also, that's the last code.
pawn Код:
CMD:calculate(playerid, params[])
{
    new string[128], method[20], value1, value2;
    if(sscanf(params, "ds[20]d", value1, method, value2))
    {
        SendClientMessage(playerid, CWHITE, "USAGE: /calculate [value] [operation] [value].");
        SendClientMessage(playerid, CGRAD, "Operations: Add, Subtract, Multiply, Divide.");
        return 1;
    }

    if(strcmp(method,"multiply",true) == 0 || strcmp(method,"*",true) == 0)
    {
        new sum = value1*value2; // We create a new define called sum which means the result of valu1 multiplied by value2
        format(string,sizeof(string),"%d multiplied by %d equals %d.",value1,value2,sum); // The format of the SendClientMessage
        SendClientMessage(playerid,CWHITE,string); // Sends message to the player which the format just above
    }
    if(strcmp(method,"divide",true) == 0 || strcmp(method,"/",true) == 0)
    {
        if(value2 == 0) // As Maths say that we can't divide by zero, we can't. So here we shall send him an error message.
        {
            SendClientMessage(playerid,CWHITE,"ERROR: Cannot divide by zero");
            return 1;
        }
        new sum = value1/value2; // We create a new define called sum which means the result of valu1 divided by value2
        format(string,sizeof(string),"%d divided by %d equals %d.",value1,value2,sum); // The format of the SendClientMessage
        SendClientMessage(playerid,CWHITE,string); // Sends message to the player which the format just above
    }
    if(strcmp(method,"add",true) == 0 || strcmp(method,"+",true) == 0)
    {
        new sum = value1+value2; // We create a new define called sum which means the result of valu1 added to value2
        format(string,sizeof(string),"%d added to %d equals %d.",value1,value2,sum); // The format of the SendClientMessage
        SendClientMessage(playerid,CWHITE,string); // Sends message to the player which the format just above
    }
    if(strcmp(method,"subtract",true) == 0 || strcmp(method,"-",true) == 0)
    {
        new sum = value1-value2; // We create a new define called sum which means the result of valu1 subtraced by value2
        format(string,sizeof(string),"%d subtracted by %d equals %d.",value1,value2,sum); // The format of the SendClientMessage
        SendClientMessage(playerid,CWHITE,string); // Sends message to the player which the format just above
    }
    return 1;
}
And that's the end of the tutorial. Hope you liked it. Please comment and feedback below. Tell me what you think about the tutorial.

Credits:
Zeex ~ ZCMD,
****** ~ sscanf,
Necip ~ Tutorial.
Reply


Messages In This Thread
How to make a calculate command - by Necip - 26.07.2013, 10:36
Re: How to make a calculate command - by iAnonymous - 26.07.2013, 10:40
Re: How to make a calculate command - by Necip - 26.07.2013, 10:43
Re: How to make a calculate command - by muhsah007 - 25.02.2015, 15:59

Forum Jump:


Users browsing this thread: 1 Guest(s)