[Tutorial][En] The pickup, type command and show menu script
#1

Hi all, I see alot of ppl asking about pickups and typing commands while in the radius of the pickup to show a menu to choose from. So stop asking and read, this here is a tutorial to teach you how to:

1) Make a pickup
2) Make it not dissappear up 'pickup'
3) Make a command to bring a menu up while only in the radius of this pickup
4) Make the menu
5) Tie script shards to the menu

First thing...we need to declare all the variables we will use:

Put this at the top of your script (Declarations area)
pawn Код:
// Declarations

new carbuyingzone; // Car buying "pickup"/zone
new hasboughtcar; // For if the player has bought a car (1) or not (0)

new Menu:carlist;
The place holder *carbuyingzone* will be our pickup, we will use a boolean (true and false/1 and 0) value place holder *hasboughtcar* to tell us whether the car buyer has bought a car or not

The Menu:carlist will be the menu holder.

Under OnGameModeInit put:
pawn Код:
carbuyingzone = CreatePickup(1559,23,-1571.9027,87.5118,3.6547); // Created pickup

// Car buying list

    carlist = CreateMenu("Car list",2,100,100,100,100); // Menu add for Car list
    SetMenuColumnHeader(carlist,0,"Car");
    SetMenuColumnHeader(carlist,1,"Cost");
    if(IsValidMenu(carlist))
    {
         AddMenuItem(carlist,0,"Bullet"); // Vehicle id: 541
         AddMenuItem(carlist,1,"~w~$1 450 000");
         AddMenuItem(carlist,0,"NRG"); // Vehicle id: 522
         AddMenuItem(carlist,1,"~w~$950 000");
         AddMenuItem(carlist,0,"Elegy"); // Vehicle id: 562
         AddMenuItem(carlist,1,"~w~$1 300 000");
         AddMenuItem(carlist,0,"Jester"); // Vehicle id: 559
         AddMenuItem(carlist,1,"~w~$1 200 000");
    }
The first line tells us that we will be using the already defined previously carbuyingzone variable to hold a pickup. That's self explanitory.

As we already declared the global variable *carlist* we will assign it a menu titled "Car List" which has 2 columns. The next two lines:

pawn Код:
SetMenuColumnHeader(carlist,0,"Car");
SetMenuColumnHeader(carlist,1,"Cost");
Assigns column headers to column 1 (0) and 2 (1) respectively, while AddMenuItem adds a menu item to the menu carlist in a desired column with a title chosen by the author.

In avoiding server crashes, one must always use the IsValidMenu function:

pawn Код:
if(IsValidMenu(carlist))
{
     AddMenuItem(carlist,0,"Bullet"); // Vehicle id: 541
     AddMenuItem(carlist,1,"~w~$1 450 000");
     AddMenuItem(carlist,0,"NRG"); // Vehicle id: 522
     AddMenuItem(carlist,1,"~w~$950 000");
     AddMenuItem(carlist,0,"Elegy"); // Vehicle id: 562
     AddMenuItem(carlist,1,"~w~$1 300 000");
     AddMenuItem(carlist,0,"Jester"); // Vehicle id: 559
     AddMenuItem(carlist,1,"~w~$1 200 000");
}
In doing this the client is able to verify that this menu actually does exist.


We will be using this code shard under the OnPlayerCommandText public function:
pawn Код:
if(strcmp("/buy", cmdtext, true, 4) == 0)
{
   
  // Car buying point code shard
  if(PlayerToPoint(playerid,5,-1571.9027,87.5118,3.5547)) // Check distance from physical pickup (carbuyingzone)
  {
    if(hasboughtcar == 0) // Check if player has already bought a car
    {
      ShowMenuForPlayer(carlist,playerid);
      TogglePlayerControllable(playerid, false);
           
      return 1;
    }
    else
    {
      SendClientMessage(playerid,COLOR_RED,"You cannot buy two cars, who's going to drive the other one?");
             
      return 1;
    }
  }
  else
  {
    SendClientMessage(playerid,COLOR_RED,"You need to be at a buy zone");
       
    return 1;
  }
}
This code shard also explains the principle of nesting, tieing multiple if else command into each other. If the player types /buy as a command and he/she is not within the radius of the pickup carbuyingzone (which is found by using a PlayerToPoint script shard) then he will recieve a message telling him that he needs to be in the radius of the created pickup. If he IS in the radius, the code goes through another pass of if statements, this time, the script is checking to see if the player has bought a car or not (hasboughtcar variable) and if so then deny access to spawn another.

pawn Код:
public OnPlayerSelectedMenuRow(playerid, row)
{
  if(GetPlayerMenu(playerid) == carlist) // If the player's current open menu is carlist
  {
    switch(row) // Upon switching rows (seletion)
    {
      case 0: // In the case that Bullet is selected
      {
        CreateVehicle(541,-1576.8210,92.7691,3.1536,136.8306,-1,-1,9999999999); // Bullet spawn with random colors
        TogglePlayerControllable(playerid,true);
        HideMenuForPlayer(carlist, playerid);
        hasboughtcar = 1;
      }
      case 1: // In the case that NRG is selected
      {
        CreateVehicle(522,-1576.8210,92.7691,3.1536,136.8306,-1,-1,9999999999); // Nrg spawn with random colors
        TogglePlayerControllable(playerid,true);
        HideMenuForPlayer(carlist, playerid);
        hasboughtcar = 1;
      }
      case 2: // In the case that Elegy is selected
      {
        CreateVehicle(562,-1576.8210,92.7691,3.1536,136.8306,-1,-1,9999999999); // Elegy spawn with random colors
        TogglePlayerControllable(playerid,true);
        HideMenuForPlayer(carlist, playerid);
        hasboughtcar = 1;
      }
      case 3: // In the case that Jester is selected
      {
        CreateVehicle(559,-1576.8210,92.7691,3.1536,136.8306,-1,-1,9999999999); // Jester spawn with random colors
        TogglePlayerControllable(playerid,true);
        HideMenuForPlayer(carlist, playerid);
        hasboughtcar = 1;
      }
   }
}
Pretty much self explanitory. All it is is that when the player selects the respective rows (also called Items) it runs the nested code...One note about rows, Item 1 will be in row 0, Item 2 will be in row 1 and so on. Each one creates the respective vehicles, then toggles PlayerControllable to 1 (so as to allow the player to move after accessing the menu) and hides the menu. After which sets the variable hasboughtcar to 1, so that the player cannot spam cars.

This is a code shard i scratched up on the forums here, put this at the very bottom of your script:
pawn Код:
// Player to point declaration

stock PlayerToPoint(playerid, Float:radius, Float:px, Float:py, Float:pz) //px, py, pz used to express
{
  if(!IsPlayerConnected(playerid)) return -1; // Checks if player is connected
  {
   new Float:X, Float:Y, Float:Z; // Base x, y, z values
   GetPlayerPos(playerid, X, Y, Z); //
   X = floatsub(X, px);
   Y = floatsub(Y, py);
   Z = floatsub(Z, pz);
   if( ( radius > X && X > -radius ) &&
   ( radius > Y && Y > -radius ) &&
   ( radius > Z && Z > -radius ) ) return 1;
  }
return 0;
}
This is the shard that allows the script to calculate the distance a player is from a certian point.

That's it, oh, don't forget to put this under your OnPlayerExitMenu function:
pawn Код:
TogglePlayerControllable(playerid,true);
Hope you leaned something from this excersice

Note: This code is from my script that is WIP, feel free to use it anywhere you want :P
Reply
#2

Nice, good explained!
Reply
#3

nice tut
Reply
#4

Код:
new hasboughtcar;
Код:
new hasboughtcar[MAX_PLAYERS];
As it is a global variable. I may be wrong so please correct me if I am.
Reply
#5

man i am looking for this TUT. its great but here is 1 problem ...

i wanna make ''travel menu'' at airport... and when i pick ''i'' icone my travel menu dont open ... what is wrong with this ? and i am learning this for 1 week damit...

http://pastebin.com/d4f73766a --- here u have my gamemode and u can check

EDIT: and when i type /travel it say ''you must be at airport to sue this command'' how i can make when he type this command at airport it show the menu
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)