[Tutorial] Making a weapon shop
#1

Hello there, and welcome to my tutorial on how to make a simple weapon shop. For this tutorial, you will not need any specific includes, although you can use the Streamer plugin by Incognito. I have explained most things with comments, so it is easy to understand when you look at the code.
First of all, we need to define the variable for the weapon pickup, example:
pawn Code:
new gunpickup; // Variable for the gun pickup
Now, under OnGameModeInit (or OnFilterScriptInit if you are adding to a filterscript), you will need to create the pickup, example:
pawn Code:
public OnGameModeInit()
{
     gunpickup = CreatePickup(1239,1, -314.4824,824.4612,14.2422, -1); // Information icon, outside Fort Carson ammunation, visible in all virtual worlds
     return 1;
}
We now need to add the code for when the player enters the pickup, using the OnPlayerPickUpPickup callback

pawn Code:
public OnPlayerPickUpPickup(playerid, pickupid)
{
     if(pickupid == gunpickup) // If the player picks up our gun pickup
     {
        ShowPlayerDialog(playerid,1,DIALOG_STYLE_LIST,"Guns","Desert Eagle ($900)","Choose","Cancel"); // Show them the gun menu so they can buy the weapons
     }
     return 1;
}
Finally, we need to add the code for when the player buys the weapon, using OnDialogResponse
pawn Code:
public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
    switch(dialogid) // We use switch since it is quicker than using multiple 'if' statements
    {
        case 1: // Same as: if(dialogid == 1)
        {
            if(response) // If the player didn't cancel the dialog
            {
                switch(listitem) // Switch through the list items, quicker and is easier for adding more guns to the dialog
                {
                    case 0: // List item 0
                    {
                        if(GetPlayerMoney(playerid) < 900) return SendClientMessage(playerid,0xFF0000FF,"You cannot afford this!"); // If the player does not have $900, send them a message in red saying they cannot afford it
                        GivePlayerWeapon(playerid,24,900); // Give the player the weapon
                        GivePlayerMoney(playerid,-900); // Give the player $-900
                    }
                }
            }
        }
    }
    return 1;
}
Thank you for reading this tutorial, and criticism is always welcomed.

PS: If the indentation looks messed up, it's due to the way that it was copied onto SA:MP's forum.
Reply
#2

Using switch() with one case is slower than using if() statement.
Reply
#3

Quote:
Originally Posted by Djole1337
View Post
Using switch() with one case is slower than using if() statement.
It's highly unlikely the person would only want 1 weapon in their dialog. I left it as 'switch' since it'd be useful when the person would want to add more weapons.
Reply
#4

GOOD JOB
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)