[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
#2

Very nice tutorial. .
Everything is explained very clearly.
+repped
Reply
#3

Quote:
Originally Posted by iAnonymous
Посмотреть сообщение
Very nice tutorial. .
Everything is explained very clearly.
+repped
Thank you for your feedback
Reply
#4

Nice But there is a bug like when someone wants to calculate like that
Example: /calculate 2.5 / 7.4 or /calculate 2.5 * 8.4 or /calculate 2.5 + 8.4
it doesnt work the correct cmd is that which will also support points (x.xx).

PHP код:
CMD:calculate(playeridparams[])
{
    new 
string[128], method[20], Float:value1Float:value2;
    if(
sscanf(params"fs[20]f"value1methodvalue2))
    {
        
SendClientMessage(playeridCOLOR_WHITE"USAGE: /calculate [value] [operation] [value].");
        
SendClientMessage(playeridCOLOR_GRAD2"Operations: Add, Subtract, Multiply, Divide.");
        return 
1;
    }
    if(
strcmp(method,"multiply",true) == || strcmp(method,"*",true) == 0)
    {
        new 
Float:sum Float:value1*Float:value2;
        
format(string,sizeof(string),"%0.2f multiplied by %0.2f equals %0.2f.",value1,value2,sum);
        
SendClientMessage(playerid,COLOR_WHITE,string);
    }
    if(
strcmp(method,"divide",true) == || strcmp(method,"/",true) == 0)
    {
        if(
value2 == 0)
            return 
GameTextForPlayer(playerid"face~r~palm"10003);
        new 
Float:sum Float:value1/Float:value2;
        
format(string,sizeof(string),"%0.2f divided by %0.2f equals %0.2f.",value1,value2,sum);
        
SendClientMessage(playerid,COLOR_WHITE,string);
    }
    if(
strcmp(method,"add",true) == || strcmp(method,"+",true) == 0)
    {
        new 
Float:sum Float:value1+Float:value2;
        
format(string,sizeof(string),"%0.2f added to %0.2f equals %0.2f.",value1,value2,sum);
        
SendClientMessage(playerid,COLOR_WHITE,string);
    }
    if(
strcmp(method,"subtract",true) == || strcmp(method,"-",true) == 0)
    {
        new 
Float:sum Float:value1-Float:value2;
        
format(string,sizeof(string),"%0.2f subtracted by %0.2f equals %0.2f.",value1,value2,sum);
        
SendClientMessage(playerid,COLOR_WHITE,string);
    }
    return 
1;

Sorry For Bad English.
Thanks

If anyone like it you can show your support by +rep.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)