Creating a simple Menu

From SA-MP Wiki

Jump to: navigation, search

Contents

Menus in Pawn

Menus look very complicated and difficult to script for the most players, although it isn't. Here I will show you how to create a simple menu. At the end we will have created a teleport menu.

First menu steps

First we have to create a menu. Insert this line after the #includes, #defines, forwards etc:

new Menu:teleportmenu;

Okay, we created the variable to store the menu. Now we have to create the menu in there. Type this into OnGameModeInit():

teleportmenu = CreateMenu("Teleportmenu", 2, 200.0, 100.0, 150.0, 150.0);

Now a bit of an explanation about the CreateMenu arguments.

CreateMenu(title, columns, Float:x, Float:y, Float:col1width, Float:col2width);
title: This  argument defines the heading of the table
columns: The number here defines how much columns are used
Float:x: The heigth position of the menu
Float:y: The width position of the menu
Float:col1width: The width of the first column
Float:col2width: The width of the second column

Add some menu items

Ok, now we've got the Menu, but we need some items, under which you can choose in the menu. You add them by AddMenuItem. So we add under the CreateMenu:

AddMenuItem(teleportmenu, 0, "LS");
AddMenuItem(teleportmenu, 0, "LS");
AddMenuItem(teleportmenu, 0, "SF");
AddMenuItem(teleportmenu, 0, "SF");
AddMenuItem(teleportmenu, 0, "LV");
AddMenuItem(teleportmenu, 0, "LV");
 
AddMenuItem(teleportmenu, 1, "Grove Street");
AddMenuItem(teleportmenu, 1, "Starfish Tower");
AddMenuItem(teleportmenu, 1, "Wheel Arch Angels");
AddMenuItem(teleportmenu, 1, "Jizzys");
AddMenuItem(teleportmenu, 1, "4 Dragons");
AddMenuItem(teleportmenu, 1, "Come-a-Lot");

The explanation for AddMenuItem:

AddMenuItem(menuid, column, text);
menuid: The menuid of the menu where the item shall be displayed
column: The column in which the item shall be shown
text: The text of the item

Creating the effects for the items

Okay, in the foregoing steps we created a full menu with items. Now, what should happen, when you choose an item? In our example we want to make a teleportmenu, so we should get teleported to the position we choose. This is made in the callback public OnPlayerSelectedMenuRow(playerid, row). The best way to do it is to do it with a switch. The first step is to get the menuid to limit the effects on our menu. You should create a new menu variable for this. Mine is called:

new Menu:CurrentMenu = GetPlayerMenu(playerid);

Now, when somebody opens the menu, the menuid will be saved in Current.

Now we have to check the id in Current, whether it's our menu:

if (CurrentMenu == teleportmenu)
{
    ...
}

Moving on to next steps, it's time for our switch. This should look like this:

if(CurrentMenu == teleportmenu)
{
    switch(row)
    {
        case 0: //Grove Street
        {
            SetPlayerPos(playerid, 2493.9133, -1682.3986, 13.3382);
            SetPlayerInterior(playerid, 0);
            SendClientMessage(playerid, 0xFFFFFFFF, "Welcome to Grove Street");
        }
        case 1: //Starfish Tower
        {
            SetPlayerPos(playerid, 1541.2833, -1362.4741, 329.6457);
            SetPlayerInterior(playerid, 0);
            SendClientMessage(playerid, 0xFFFFFFFF, "Welcome to the top of Starfish Tower");
        }
        case 2: //Wheel Arch Angels
        {
            SetPlayerPos(playerid, -2705.5503, 206.1621, 4.1797);
            SetPlayerInterior(playerid, 0);
            SendClientMessage(playerid, 0xFFFFFFFF, "Welcome to the Wheel Arch Angels tuning-shop");
        }
        case 3: //Jizzys
        {
            SetPlayerPos(playerid, -2617.5156, 1390.6353, 7.1105);
            SetPlayerInterior(playerid, 0);
            SendClientMessage(playerid, 0xFFFFFFFF, "Welcome to Jizzy's Nightclub!");
        }
        case 4: //4Dragons
        {
            SetPlayerPos(playerid, 2028.5538, 1008.3543, 10.8203);
            SetPlayerInterior(playerid, 0);
            SendClientMessage(playerid, 0xFFFFFFFF, "Welcome to the Four Dragons Casino");
        }
        case 5: //Com-a-Lot
        {
            SetPlayerPos(playerid, 2169.1838, 1122.5426, 12.6107);
            SetPlayerInterior(playerid, 0);
            SendClientMessage(playerid, 0xFFFFFFFF, "Welcome to the Come-a-Lot casino!");
        }
    }
}

When everything is done correctly, like I've showed in those examples, the effects for menu items are created successfully.

Last steps

Now we need a command to show the menu. This is the easiest step. Just a comparison with strcmp and a ShowMenuForPlayer. As you should know, this comes in the callback OnPlayerCommandText

if(strcmp(cmdtext, "/teleport", true) == 0)
{
    ShowMenuForPlayer(teleportmenu,playerid);
    return 1;
}

Really easy, wasn't it?

Last words

Okay, after you read this AND understood it, try your own menu. As you could see, it isn't that difficult, but will impress the players on your server all the more. And you can script really cool effects with this. It's also very cool for general stores or supermarkets for the things you can buy. Then you can subtract some money as effect and the price is shown in another column in the menu. But now, work on your own.

I hope you enjoyed this tutorial. If you got any questions, ask in the forums.

A full test script for menus

// Test menu functionality filterscipt
 
#include <a_samp>
 
#define TEST_MENU_ITEMS 6
 
new Menu:TestMenu;
new TestMenuStrings[6][16] = {"Test1", "Test2", "Test3", "Test4", "Test5", "Test6"};
 
HandleTestMenuSelection(playerid, row)
{
	new s[256];
 
	if(row < TEST_MENU_ITEMS) {
		format(s,256,"You selected item %s",TestMenuStrings[row]);
		SendClientMessage(playerid,0xFFFFFFFF,s);
	}
}
 
InitTestMenu()
{
	TestMenu = CreateMenu("Test Menu", 1, 200.0, 150.0, 200.0, 200.0);
 
	for(new x=0; x < TEST_MENU_ITEMS; x++) {
    	        AddMenuItem(TestMenu, 0, TestMenuStrings[x]);
	}
}
 
public OnFilterScriptInit()
{
   	InitTestMenu();
}
 
public OnPlayerSelectedMenuRow(playerid, row)
{
        new Menu:PlayerMenu = GetPlayerMenu(playerid);
 
        if(PlayerMenu == TestMenu) {
	     HandleTestMenuSelection(playerid, row);
	}
}
 
public OnPlayerCommandText(playerid, cmdtext[])
{
	if(!strcmp(cmdtext, "/menutest", true))	{
    	     ShowMenuForPlayer(TestMenu, playerid);
    	     return 1;
	}
	return 0;
}
In other languages