23.05.2010, 20:20
[TUT] How To Make Menus
By: Z-R0
What You Will Need
1. Pawno
2. Good Attention Span
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:
Code:
#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:
Code:
new Menu:Test;
Now if you did this correctly, your script should look like this at the top:
Code:
#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:
Code:
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:
Code:
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:
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:
Code:
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:
Code:
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:
Code:
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:
Code:
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:
Code:
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:
Code:
public OnPlayerExitedMenu(playerid) { return 1; }
We are gonna do like we did before, make a free line above "return 1;" and type the following:
Code:
TogglePlayerControllable(playerid,1);
So that now the call should look like this:
Code:
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 I took time to make this tutorial