[Tutorial] Creating Menus/Dialog Box's

Started by SA:MP, May 05, 2023, 05:48 AM

Previous topic - Next topic

0 Members and 4 Guests are viewing this topic.

SA:MP

[Tutorial] 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://sampforum.blast.hk/showthread.php?tid=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.


Source: [Tutorial] Creating Menus/Dialog Box's