PDA

View Full Version : [Tutorial] [TUT] How To Make Menus


Z-R0
23/05/2010, 08:20 PM
[TUT] How To Make Menus

By: Z-R0

What You Will Need

1. Pawno
2. Good Attention Span

Steps

Step 1

Open your Pawno, And start by clicking File>New

Step 2

Now lets begin, go on and go to where it says:
#include <a_samp>

It should be located at the top of your script

Now hit enter a bit, don't have to be much, just enough to seperate..

After you do that go ahead and define a new menu by typing the following:
new Menu:Test;

Now if you did this correctly, your script should look like this at the top:
#include <a_samp>

new Menu:Test;

Step 3

Now that you have defined a new Menu, lets start adding to it..

Go ahead and go to your "OnGameModeInIt" call, should look like this:
public OnGameModeInit()
{
// Don't use these lines if it's a filterscript
SetGameModeText("Blank Script");
AddPlayerClass(0, 1958.3783, 1343.1572, 15.3746, 269.1425, 0, 0, 0, 0, 0, 0);
return 1;
}

Right under the "AddPlayerClass" line, type the following:
Test = CreateMenu("Test Menu",1,20,120,150,40);

This code actually creates the menu!

The word "Test" is the name of the menu which you defined earlier

The function "CreateMenu" obviously creates a menu

The "Test Menu" is what will appear at the top of your menu as its name

The numbers following "Test Menu" defines the position of the menu, but we will not mess with that for now since this is a simple menu!

Step 4

So far you have defined and created your menu, but now its time to start adding options to it..

So right under your "Test = CreateMenu" line, type the following code:
AddMenuItem(Test,0,"Hello");

What does this do, you may ask? Let me explain it a bit,..

"AddMenuItem" obviously adds an option to your menu, "Test" is the name of the menu to add the option to

"0" is the column to add it to, for now do not mess with it :P

And last but extremly not least is the "Hello", this is what will appear as the name of the option your adding

So then your code should now look like this:

public OnGameModeInit()
{
// Don't use these lines if it's a filterscript
SetGameModeText("Blank Script");
AddPlayerClass(0, 1958.3783, 1343.1572, 15.3746, 269.1425, 0, 0, 0, 0, 0, 0);
Test = CreateMenu("Test Menu",1,20,120,150,40);
AddMenuItem(Test,0,"Hello");
return 1;
}

Step 5

Now lets go ahead and make a command to open the menu

Start by searching for your "OnPlayerCommandText" call, which looks like this:
public OnPlayerCommandText(playerid, cmdtext[])
{
if (strcmp("/mycommand", cmdtext, true, 10) == 0)
{
// Do something here
return 1;
}
return 0;
}

Go ahead and replace the text "/mycommand" with "/menu"

Then go ahead and replace the text "// Do something here" with "TogglePlayerControllable(playerid,0);

After that hit enter once, and type "ShowMenuForPlayer(Text,playerid);

After all that your code should look like this:
public OnPlayerCommandText(playerid, cmdtext[])
{
if (strcmp("/menu", cmdtext, true, 10) == 0)
{
TogglePlayerControllable(playerid,0);
ShowMenuForPlayer(Test,playerid);
return 1;
}
return 0;
}

Let me explain, "/menu" is the command that you will type in order to open your menu

"TogglePlayerControllable(playerid,0);" will freeze your player from moving to be able to acess the menu

"ShowMenuForPlayer(Test,playerid);" will show the menu we created (Test) to the player

Step 6

You are almost done! Now its time to determine what happens when the person selects the "Hello" option, remember?

So then start by looking for the call called "OnPlayerSelectedMenuRow" which looks like this:
public OnPlayerSelectedMenuRow(playerid, row)
{
return 1;
}

Once you find it, make a new line by hitting enter right before "return 1;" so that you have a free line above "return 1;"

In this empty space go ahead and type this:
if(GetPlayerMenu(playerid) == Menu)
{
switch(row)
{
case 0:
{
GameTextForPlayer(playerid,"~r~Hello!",3000,4);
TogglePlayerControllable(playerid, 1);
HideMenuForPlayer(Menu, playerid);
}
}
}

Now let me explain this one...

This call is called when a person selects an option on your menu

"case 0" is the number of the option which you are selecting, in Pawno, 0 is a number in cases, which becomes the first number rather than 1

"GameTextForPlayer(playerid,"~r~Hello!",3000,4);" makes it so that if you select the option "Hello" then big red text saying "Hello" will appear on your screen.

"TogglePlayerControllable(playerid,1);" makes it so that if you selected the "Hello" option, then you will also be unfrozen so you can move your character again.

"HideMenuForPlayer(Test,playerid);" makes it so that if you selected the "Hello" option, then the menu will also close.

Step 7

We are almost done now, now we just need to do some "Make sure" 's....

What do I mean? Well lets say that your player decides that he does not like the options in the menu or something

And decides to press "f" or "enter" in game to close the menu, sure it will close, but guess what, he will still be frozen!

So now we need to make sure to unfreeze the person if he or she decides to close the menu without selecting an option..

To do this, go ahead and look for your "OnPlayerExitedMenu" call, which looks like this:
public OnPlayerExitedMenu(playerid)
{
return 1;
}

We are gonna do like we did before, make a free line above "return 1;" and type the following:
TogglePlayerControllable(playerid,1);

So that now the call should look like this:
public OnPlayerExitedMenu(playerid)
{
TogglePlayerControllable(playerid,1);
return 1;
}

So now when a player closes a menu by hitting "f" or "enter" key, it will unfreeze them...

Step 8

Now for the final step, SAVE and COMPILE your script!!!!!!!

Your script should work without problems and should compile without errors or warnings if everything has been done correctly, but just in case it doesn't,

Feel free to PM me in the forum or reply to this thread with your suggestions, thoughts, comments, or questions!!!

Have fun scripting!!!!! Enjoy your new Menu, and don't forget to thank me >:D I took time to make this tutorial

Joey09
23/05/2010, 08:47 PM
Wow nice.

[MK]Soprano
23/05/2010, 09:01 PM
Nice and helpful :)

Z-R0
23/05/2010, 09:04 PM
Thanks guys, just hearing that makes it worth posting :mrgreen:

Kayaque
24/05/2010, 02:40 AM
Good tutorial! Thanks!

zSuYaNw
24/05/2010, 02:53 AM
I liked it, nice tutorial.

v0nz
24/05/2010, 03:19 AM
Great tutorial, thanks.

Smokey619
24/05/2010, 03:24 AM
it give me errors and i just copied everything


C:\Documents and Settings\Owner\My Documents\Gta Servers\Server 0.3 R7\filterscripts\storemenu.pwn(181) : error 017: undefined symbol "Menu"
C:\Documents and Settings\Owner\My Documents\Gta Servers\Server 0.3 R7\filterscripts\storemenu.pwn(189) : error 017: undefined symbol "Menu"
Pawn compiler 3.2.3664 Copyright (c) 1997-2006, ITB CompuPhase


2 Errors.

Saurik
24/05/2010, 04:20 AM
good work!

Z-R0
24/05/2010, 08:17 PM
it give me errors and i just copied everything


C:\Documents and Settings\Owner\My Documents\Gta Servers\Server 0.3 R7\filterscripts\storemenu.pwn(181) : error 017: undefined symbol "Menu"
C:\Documents and Settings\Owner\My Documents\Gta Servers\Server 0.3 R7\filterscripts\storemenu.pwn(189) : error 017: undefined symbol "Menu"
Pawn compiler 3.2.3664 Copyright (c) 1997-2006, ITB CompuPhase


2 Errors.


your "Menu" is undefined :S
meaning you probly didnt do this line at the top:

new Menu:Menu;

if your going to name your own menu, just replace every "Test" text you see in the tutorial to your liking and it should work
if that dont work, go to lines: 181, and 189 and copy those lines into here so that i can see...

Antonio [G-RP]
25/05/2010, 12:41 AM
Where was this when I started scripting?

Z-R0
25/05/2010, 01:26 AM
Where was this when I started scripting?


lol

hab2ever
25/05/2010, 01:17 PM
Good, nice TUT

Vladyy
25/05/2010, 01:21 PM
nice, it was easy to do: P

Thebest96
15/07/2010, 07:42 AM
C:\Users\Antonio Carlos\Desktop\Nuno\gta sa\gamemodes\mynewscript.pwn(99) : error 017: undefined symbol "Test"

matthewdriftking
15/07/2010, 07:52 AM
Good Work man!

Aleluja
15/07/2010, 11:12 AM
Nice tutorial :)

DreamKillah
18/07/2010, 05:01 AM
When I compile it , it says ERROR, undefines symbol " Menu "

iGetty
04/06/2011, 05:21 PM
When I compile it , it says ERROR, undefines symbol " Menu "

Then you need to define it. :P

Steeve_Smith
08/07/2011, 12:12 AM
Because you should replace

if(GetPlayerMenu(playerid) == Menu) to if(GetPlayerMenu(playerid) == Test)

and

HideMenuForPlayer(Menu, playerid); to HideMenuForPlayer(Test, playerid);

Anyway nice tuto mate!

robintjeh
22/07/2011, 06:23 PM
You should explain how to make more buttons instead of only Hello...

juraska
22/07/2011, 08:04 PM
Nice tutorial, it's can by useful ;)

emokidx
23/07/2011, 04:54 AM
wow,, great tut!

fie
28/07/2011, 02:46 PM
how do you make another option on the menu?

thanks :)

Unte99
29/07/2011, 12:46 AM
how do you make another option on the menu?

thanks :)


"AddMenuItem" obviously adds an option to your menu


Did you even tried to read the first post ?

So it would look like this:


public OnGameModeInit()
{
// Don't use these lines if it's a filterscript
SetGameModeText("Blank Script");
AddPlayerClass(0, 1958.3783, 1343.1572, 15.3746, 269.1425, 0, 0, 0, 0, 0, 0);
Test = CreateMenu("Test Menu",1,20,120,150,40);
AddMenuItem(Test,0,"Option1");
AddMenuItem(Test,1,"Option2");
AddMenuItem(Test,2,"Option3");
AddMenuItem(Test,3,"Option4");
return 1;
}

Neonman
19/11/2011, 10:18 AM
nice and useful tutorial. Good job.

stylerof619
19/11/2011, 08:20 PM
i think thats one of the best tuts i ever saw :)

Sneaky_
27/12/2011, 09:06 AM
Can you put here an example file?

Mikkel
29/12/2011, 02:34 AM
Great tutorial! I used this, it's easy to follow and easy to understand, great explanations aswell!