SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help > Tutorials

Reply
 
Thread Tools Display Modes
Old 26/06/2020, 07:52 AM   #1
SkyFlare
Huge Clucker
 
SkyFlare's Avatar
 
Join Date: Apr 2015
Location: NZ
Posts: 221
Reputation: 43
Default Creating Menus/Dialog Box's


Creating A Menu with a Dialog


Syntax for Dialogs
PHP Code:
ShowPlayerDialog(playerid, **DIALOGID**, **DIALOGSTYLE**, "**DIALOGTITLE**""**DIALOGBODYCONTENT**""**LEFTBUTTONTEXT**""**RIGHTBUTTONTEXT**"); 
DialogID - This is used to reference a Dialog in different area's of your Gamemode/Filterscript.
DialogStyle - This is a multi choice depending what kind of Dialog Style you need or want, you will learn about these in this tutorial.
DialogTitle - This goes on the Dialog at the top, as the Title shown.
DialogBodyContent - This is what is in the middle of your Dialog, weather that is Text,Numbers,Items etc, you will also learn how to create Listitems in this tutorial.
Left & Right Button - This is to make your Dialogs Responsive and do different things, adding extra dialogs to open after you click it, or do certain tasks.


Setting Up Framework for Dialogs
We will use an Enumerator for this, so we don't have to constantly define numbers and have a possibility of forgetting DialogID's along the way.
If you don't know how to use an Enumerator, there is a great Article about this already.
https://forum.sa-mp.com/showthread.php?t=318307
PHP Code:
enum
{
    
DIALOG_UNUSED
}; 
First off the bat, you'll want to make sure this Enumerator is placed at the top of your script, below your Includes,Defines, beside any custom Global Variables you've created already.

What is DIALOG_UNUSED? This is basically DIALOG ID 0, we are using this as a placeholder and I will explain why.

Say you have your Help Command, which just shows all the available CMD's on your server, but it doesn't do anything else when you click the Menu Buttons on the Dialog? That mean's we wont need to use OnDialogResponse Callback's features for that Dialog, and if there is any more Dialogs you have, that have no functions with the buttons, you also wont need OnDialogResponse's Features, therefore you're able to use the same Dialog ID, so we can just use DIALOG_UNUSED like this
Code:
ShowPlayerDialog(playerid, DIALOG_UNUSED, DIALOG_STYLE_MSGBOX, "Messagebox Title 1", "This is Information inside the first Messagebox", "Ok", "");

ShowPlayerDialog(playerid, DIALOG_UNUSED, DIALOG_STYLE_MSGBOX, "Messagebox Title 2", "This is Information inside the second Messagebox", "Okay", "");
DIALOG_STYLE_MSGBOX

This is a MessageBox, You can fit a lot of text into these, It's useful for Help Commands such as /ahelp or /help
or even /stats or whatever feature you want, like maybe even Text Messages in a Roleplay Gamemode?
Literally your Imagination will be the limit of this Dialog's Usage.

So lets create one!
First we should have another look at that Enumerator
PHP Code:
enum
{
    
DIALOG_UNUSED
}; 
We can add more DialogID's we are actually going to use with response functions, so for now lets just show a Helpful MessageBox with information.
PHP Code:
enum
{
    
DIALOG_UNUSED,
    
DIALOG_HELP,
    
DIALOG_HELP2
}; 
So now we have the DialogID DIALOG_HELP and DIALOG_HELP2 defined and now ready to be used, so lets use them in a Command.

NOTE: I am not creating a tutorial on CMDS and Processors, this is ZCMD Format, if you can't understand it then I suggest you go study it a little.
PHP Code:
CMD:help(playeridparams[])
{
    
ShowPlayerDialog(playeridDIALOG_HELPDIALOG_STYLE_MSGBOX"Help Page 1""This is Page 1 of the Help Messagebox""Page 2""Exit");
    return 
1;

Now that we have that Command created, if you were to test it, you'd notice typing /help will show a Messagebox, with what we wrote in it, furthermore youll notice the Page 2 Button does not actually do anything, so lets change that and make it do something.

First I would like to mention, using Switches is the best method here, youll have less confusion with your code and switch brings some great benefits and in some cases less code to actually write.

And theres been mention if youre running filterscripts with dialogs in them, that when youre coding your dialog response you should be returning 0, ill quote something here for you guys.

Quote:
Originally Posted by l0gic View Post
OnDialogResponse
"Returning 0 in this callback will pass the dialog to another script in case no matching code were found in your gamemode's callback.
It is always called first in filterscripts so returning 1 there blocks other filterscripts from seeing it."
In my opinion all dialogs should be together and in one place however thats not always the case, so return however you need, thanks l0gic for this information, but lets pretend all our dialogs are just in the gamemode for this Tutorial.

So lets do our response for our help command we created.
PHP Code:
public OnDialogResponse(playeriddialogidresponselistiteminputtext[])
{
    switch(
dialogid)
    {
        case 
DIALOG_UNUSED: return 1//This is the Empty Dialog that isn't used as I previously mentioned.
        
case DIALOG_HELP://This is our Help Dialog called from CMD:help
        
{
            if(!
response) return 1;//This is the Exit Button we made, the right side Button
            
else if(response)//This is the Page 2 Button we made, the left side Button
            
{
                
//We want to show the Page 2 Dialog now that they have pressed the Help Page 2 Button
                
ShowPlayerDialog(playeridDIALOG_HELP2DIALOG_STYLE_MSGBOX"Help Page 2""This is Page 2 of the Help Messagebox""Page 1""Exit");
                return 
1;
            }
        }
        case 
DIALOG_HELP2://This is our Page 2 Dialog Response, called from DIALOG_HELP Button
        
{
            if(!
response) return 1;//This is the Exit Button we made, the right side Button
            
else if(response)//This is the Page 1 Button we made, the left side Button, to take us back to the Page 1 Dialog
            
{
                
//We want to show the Page 1 Dialog now that they have pressed the Help Page 1 Button
                
ShowPlayerDialog(playeridDIALOG_HELPDIALOG_STYLE_MSGBOX"Help Page 1""This is Page 1 of the Help Messagebox""Page 2""Exit");
                return 
1;
            }
        }
    }
    return 
1;

Now if you go ingame and test that, you'll see that /help still works the same, but the button "Page 2" Actually displays the Page 2 Dialog now.
and if we click the "Page 1" Button, you'll be directed back to the 1st Page of Help.
Things to note: You can add new lines in a Messagebox by writing "\n" within the Text for the Messagebox Content, See Below as an Example.
PHP Code:
ShowPlayerDialog(playeridDIALOG_UNUSEDDIALOG_STYLE_MSGBOX"Line Testing""This is Line 1\nThis is Line 2\nThis is Line 3""Ok"""); 



DIALOG_STYLE_INPUT

This is an Input Box, This can be used for many different things, Setting a Skin ID, by typing it into the Dialog Input, anything to do with Numbers really, or even Strings!, changing a players name can be done here aswell!

SOoOoO let's create a Name Change Input Box!

First off, we will add a new DialogID into our Enum, name it DIALOG_CHANGENAME
PHP Code:
enum
{
    
DIALOG_CHANGENAME
}; 
Now we have a Dialog ID for Change Name Dialog, we can make the Command to show the Dialog itself.

PHP Code:
CMD:changename(playeridparams[])
{
    
ShowPlayerDialog(playeridDIALOG_CHANGENAMEDIALOG_STYLE_INPUT"Change Name","Enter your new desired Name you wish to use:","Change","Exit");
    return 
1;

There we go, if you go ingame, you'll see when you type /changename, a dialog will show up, with an input field, to change your name, however it won't do anything, because again, we have not setup the response in OnDialogResponse.

So let's do that now!

PHP Code:
public OnDialogResponse(playeriddialogidresponselistiteminputtext[])
{
    switch(
dialogid)
    {
        case 
DIALOG_UNUSED: return 1//This is the Empty Dialog that isn't used as I previously mentioned.
        
case DIALOG_CHANGENAME://This is our Changename Dialog called from CMD:changename
        
{
            if(!
response) return 1;//This is the Exit Button we made, the right side Button
            
else if(response)//This is the Change Button we made, the left side Button
            
{
                if(!
strlen(inputtext) || strlen(inputtext) > 24) return SendClientMessage(playerid, -1"Name Changing requires the name to be of a length between 1-24");//This checks if they typed nothing or too much.
                //We want to Change the Players Username based on what they typed in the Box earlier since we now know its between 1-24, so we use "inputtext" for that again.
                
SetPlayerName(playeridinputtext);//This will change the players name based on what they wrote in the Input Dialog.
                
SendClientMessage(playerid, -1"You have successfully changed your name, enjoy!");
                return 
1;
            }
        }
    }
    return 
1;

Now when you go ingame, typing /changename and enter a name in the Input Box, you'll notice your name will be changed!, quite a small basic feature but you can imagine how complicated it would get, if you want to check if another player has that name, but this is for the sake of a Tutorial and learning to use the Dialog's themselves, not feature packed Functions within them.

DIALOG_STYLE_LIST

This is a ListBox, personally my favorite of them all, because of how Dynamic this can be, and its functions, so much use within this 1 Dialog type that it's hard for me to pick a Feature to use for the Tutorial, Teleport List? Yeah lets do it.

So again, we need that Enum to have our new Dialog ID, name it DIALOG_TELEPORTS.

PHP Code:
enum
{
    
DIALOG_UNUSED,
    
DIALOG_TELEPORTS
}; 
Now we have a Dialog ID for Teleport List, we can make the Command to show the Dialog itself.
PHP Code:
CMD:teleport(playeridparams[])
{
    
ShowPlayerDialog(playeridDIALOG_TELEPORTSDIALOG_STYLE_LIST"Teleport","Los Santos\nSan Fierro\nLas Venturas","Teleport","Exit");
    return 
1;

See how we are using that "\n" in the string there, thats because again we are making a new line, like how I exampled earlier, but this time, it does a little more than adding a new "line", let me explain this here.
"Los Santos" will be now on its own line, and when we click only that line, its response would be different than "San Fierro" or "Las Venturas"
they basically get split up into a ***LIST*** Simple right? sure is!

So if we go ingame, yet again we can use the Command associated with our new Dialog Type, /teleport, we can click all 3 but nothing will happen because again OnDialogResponse needs to be coded for it, so lets get going with our response code.

PHP Code:
public OnDialogResponse(playeriddialogidresponselistiteminputtext[])
{
    switch(
dialogid)
    {
        case 
DIALOG_UNUSED: return 1//This is the Empty Dialog that isn't used as I previously mentioned.
        
case DIALOG_TELEPORTS://This is our Teleports Dialog called from CMD:teleport
        
{
            if(!
response) return 1;//This is the Exit Button we made, the right side Button
            
else if(response)//This is the Teleport Button we made, the left side Button
            
{
                switch(
listitem)//This is switching ListItems, self explanatory BUT this is smart, them "\n" we added play the role on determining how many List Items we have.
                
{
                    
//We always start with "0" which is essentially the First List Item from the top.
                    
case 0://Los Santos Teleport
                    
{
                        
SetPlayerPos(playerid1532.0996,-1675.6741,13.3828);
                        
SendClientMessage(playerid, -1"You've teleported to Los Santos");
                        return 
1;
                    }
                    case 
1://San Fierro Teleport
                    
{
                        
SetPlayerPos(playerid, -2649.0630,21.0362,6.1328,357.6369); // San Fierro
                        
SendClientMessage(playerid, -1"You've teleported to San Fierro");
                        return 
1;
                    }
                    case 
2://Las Venturas Teleport
                    
{
                        
SetPlayerPos(playerid1687.0597,1446.8789,10.7688,275.8561); // Las Venturas
                        
SendClientMessage(playerid, -1"You've teleported to Las Venturas");
                        return 
1;
                    }
                }
            }
        }
    }
    return 
1;

That should be perfect, going ingame, typing /teleport should bring us to our Teleport Menu with our 3 Options, selecting one and pressing "Teleport" Button should teleport us to the desired Location now, you can easily add onto this list and create more teleports and modify the existing ones.

DIALOG_STYLE_PASSWORD

This is a Password Input, exactly the same type of Input Dialog as the standard Input one we used to change our name, but this one simply covers the password with the
hidden character, during input... using this is exactly the same as DIALOG_STYLE_INPUT.
Create one and see for yourself, you'd use this specifically for a password screen.
PHP Code:
ShowPlayerDialog(playeridPUTDIALOGIDHEREDIALOG_STYLE_PASSWORD"Password","Things you type here will be shown as a password:","Ok",""); 


And that concludes the Tutorial, I seen a few different Tutorials for Dialogs, I even created one myself a few years ago, however that was in 2012 and I wasn't as smart as I am now, so reiterating on it, with switches and decent examples of usage, hopefully this helps someone, it's good to know how to use these, even with all the Textdraws nowadays I still personally prefer Dialogs, feel free to comment any ideas for another Tutorial if you guys need help with something else.
__________________
Purchasable Scripting Services Available, Inbox me with your Requests.
My Project Progress Tracker
My Work
Link
Information
S-Store System V2 LinkIngame Store in Dialogs

Last edited by SkyFlare; 27/06/2020 at 12:07 AM.
SkyFlare is offline   Reply With Quote
Old 26/06/2020, 09:51 AM   #2
LaKhWaN
Little Clucker
 
Join Date: Apr 2020
Posts: 23
Reputation: 9
Default Re: Creating Menus/Dialog Box's

Good work, helpful for beginners keep it up!!

+Rep
__________________
Looking for someone who can help me in learning pawn language. for free xD
LaKhWaN is offline   Reply With Quote
Old 26/06/2020, 09:56 AM   #3
GameOvr
Huge Clucker
 
GameOvr's Avatar
 
Join Date: Jun 2018
Posts: 296
Reputation: 9
Default Re: Creating Menus/Dialog Box's

Perfect!
__________________
GameOvr is offline   Reply With Quote
Old 26/06/2020, 09:57 AM   #4
SkyFlare
Huge Clucker
 
SkyFlare's Avatar
 
Join Date: Apr 2015
Location: NZ
Posts: 221
Reputation: 43
Default Re: Creating Menus/Dialog Box's

Quote:
Originally Posted by LaKhWaN View Post
Good work, helpful for beginners keep it up!!

+Rep
Quote:
Originally Posted by GameOvr View Post
Perfect!
Thanks guys Glad this will be of use.
__________________
Purchasable Scripting Services Available, Inbox me with your Requests.
My Project Progress Tracker
My Work
Link
Information
S-Store System V2 LinkIngame Store in Dialogs
SkyFlare is offline   Reply With Quote
Old 26/06/2020, 01:45 PM   #5
l0gic
Huge Clucker
 
Join Date: Aug 2019
Posts: 205
Reputation: 59
Default Re: Creating Menus/Dialog Box's

OnDialogResponse
"Returning 0 in this callback will pass the dialog to another script in case no matching code were found in your gamemode's callback.
It is always called first in filterscripts so returning 1 there blocks other filterscripts from seeing it."
__________________
Coding experience with: PAWN, Java, Python, Javascript, PHP and SQL for about 6 years.
If I helped you, any reputation would be appreciated.
l0gic is offline   Reply With Quote
Old 27/06/2020, 12:01 AM   #6
SkyFlare
Huge Clucker
 
SkyFlare's Avatar
 
Join Date: Apr 2015
Location: NZ
Posts: 221
Reputation: 43
Default Re: Creating Menus/Dialog Box's

Quote:
Originally Posted by l0gic View Post
OnDialogResponse
"Returning 0 in this callback will pass the dialog to another script in case no matching code were found in your gamemode's callback.
It is always called first in filterscripts so returning 1 there blocks other filterscripts from seeing it."
Thanks man, forgot about return 0, ill make a note of that in the tutorial incase people are using dialogs in filterscripts, but its worth saying its not really a good idea to be wasting a filterscript on dialogs when mappings or features sometimes need to be loaded or unloaded, usually dialogs dont need to be unloaded, regardless the people should know that this is a possibility so itll be noted. 👍🏽😊
__________________
Purchasable Scripting Services Available, Inbox me with your Requests.
My Project Progress Tracker
My Work
Link
Information
S-Store System V2 LinkIngame Store in Dialogs
SkyFlare is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dialog menus with y_dialogs?? Greggu Scripting Help 0 30/03/2018 03:38 PM
Dialog More menus 1+ rep canip Scripting Help 3 14/04/2013 04:48 PM
dialog menus bugged hadzx Help Archive 1 27/01/2011 10:04 AM
Need help creating 2 menus!!!--TOPIC CLOSED-- Epic Shower Help Archive 6 09/03/2010 07:39 PM
More than 2 Dialog Menus dont work V1ceC1ty Help Archive 6 14/09/2009 05:45 PM


All times are GMT. The time now is 08:19 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.