[Tutorial] How to make a weapon shop dialog
#1

Introduction

Hello guys!In this tutorial ,i will teach you how to create a working weapon shop dialog.
The first i ever did in pawn was creating a weapon shop dialog,but that one had lot of if() statements and had too many lines of code. But having spent a little time in sa-mp forums ,from what i've learned i will teach you an even better way to do this.

Includes and Defines

pawn Код:
#include<a_samp>
#include<zcmd>

#define WEAP_DIALOG 10
First things first,we include a_samp(which will include all other necessary includes like a_players,a_objects etc) and then we include zcmd which we will use for command processing since we are going to show the weapon dialog when a command is executed.

Then we define the weapon dialogid as 10, whenever there is a word called WEAP_DIALOG it will be replaced by the number 10 (Note it doesnt get affected when added in a string)

Creating the command and dialog

pawn Код:
CMD:weaponshop(playerid,params[])
{
    ShowPlayerDialog(playerid,WEAP_DIALOG,DIALOG_STYLE_LIST,"Weapon Shop","knife($1000)\nmp5($2000)\ndeagle($5000)\nspas($10000)\nRPG($20000)","Buy","Close");
    return 1;
}
Then we show them the dialog with weapon list.
Here,
playerid - the id of the player for which the dialog is shown
WEAP_DIALOG -is the id of the dialog which i earlier said will get replaced by 10 during compilation
DIALOG_STYLE_LIST -is the type of dialog(in our case it is a list type dialog,so we use DIALOG_STYLE_LIST)
Weapon Shop - this is the title of the dialog
The next thing is the list of items ,the \n brings the text to a new line
The other two are response(buy) and no response(close) buttons.

Making the dialog's functionality

pawn Код:
public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
    if(dialogid==WEAP_DIALOG)
    {
        if(response)
        {
            switch(listitem)
            {
                case 0:SellWeapon(playerid,1000,4,1);
                case 1:SellWeapon(playerid,2000,29,500);
                case 2:SellWeapon(playerid,5000,24,500);
                case 3:SellWeapon(playerid,10000,27,500);
                case 4:SellWeapon(playerid,20000,35,10);
            }
        }
    }
    return 1;
}
next we are checking some conditions in OnDialogResponse
in the line
Код:
if(dialogid==WEAP_DIALOG)
we check if the dialogid matches with our weapon dialogid.
Код:
if(response)
it checks if the player has responded to the dialog.
Код:
switch(listitem)
this carries out different functions for different values(cases) of listitem
case 0 refers to knife,case 1 refers to mp5 and so on.
the next few lines can be understood well if you understand the following codes

Creating the stock

pawn Код:
stock SellWeapon(playerid,money,weapon,ammo)
{
    new playermoney = GetPlayerMoney(playerid);
    if(playermoney>money)
    {
        new string[80],weaponname[32];
        GivePlayerMoney(playerid,-money);
        GivePlayerWeapon(playerid,weapon,ammo);
        GetWeaponName(weapon,weaponname,sizeof(weaponname));
        format(string,sizeof(string),"You bought %i ammo of weapon %s for $%i",ammo,weaponname,money);
        return SendClientMessage(playerid,-1,string);
    }
    else return SendClientMessage(playerid,-1,"You don't have enough money");
}
in the first line we assign the value of the player's cash to the variable 'playermoney'
in the next line we check if the player's money is greater than the weapon's cost.If not we return them a message saying they dont have enough cash.
if they have enough cash,we first declare two strings,one to format the purchase message and the other to store the weapon name.
In the next line ,we deduct the weapon's cost from the player's money and then we give the player the weapon with the specified ammo.
In the next line, we get the weapon name and then format a message showing the amount of ammo,weapon name and cost and then we return the formatted message.

This is a stock,and this is what was used in the OnDialogResponse callback,the weapon,money,ammo get replaced by what values we there.




Since this is my first tutorial,please give feedbacks and notify me if anything in my code could be improved.

Also if you want me to make more tutorials, you can request me here or in PM.
Reply
#2

well explained..
but make it more attractive :P
and also make ur stock function first so newbies can understand it better

anyway good job!!
Reply
#3

GivePlayerMoney(playerid,playermoney-money);
should be reset once you give the money
Reply
#4

Quote:
Originally Posted by VivianKris
Посмотреть сообщение
GivePlayerMoney(playerid,playermoney-money);
should be reset once you give the money
Thanks for notifying. I changed playermoney-money to -money.

@PMH thanks, I'll do it when i get on my pc
Reply
#5

after this add this ";"
Quote:

ShowPlayerDialog(playerid,WEAP_DIALOG,DIALOG_STYLE _LIST,"Weapon Shop","knife($1000)\nmp5($2000)\ndeagle($5000)\nsp as($10000)\nRPG($20000)","Buy","Close")

Then it will look like this :

Quote:

ShowPlayerDialog(playerid,WEAP_DIALOG,DIALOG_STYLE _LIST,"Weapon Shop","knife($1000)\nmp5($2000)\ndeagle($5000)\nsp as($10000)\nRPG($20000)","Buy","Close");

Reply
#6

Quote:
Originally Posted by Rissam
Посмотреть сообщение
after this add this ";"


Then it will look like this :
Oops my bad, corrected it.
Reply
#7

Quote:
Originally Posted by DavidBilla
Посмотреть сообщение
Oops my bad, corrected it.
By the way nice tutorial +rep
Reply
#8

It's ok.

But good job.
Reply
#9

nice tutorial! + Rep
Reply
#10

nice tutorial!! i like it.
Reply
#11

Great job.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)