How to Create a Dialog

From SA-MP Wiki

Jump to: navigation, search

Contents

Creating Dialogs

How to Create a Dialog was added in SA-MP 0.3a This function was added in SA-MP 0.3a and will not work in earlier versions!


Introduction

Creating dialogs (the new 0.3 ones) may seem rather hard, though they're actually rather simple to make. This short tutorial will show you how to make a dialog list. This dialog is for demonstration purposes and will be fairly limited.

This tutorial will help you with all three styles of dialogs, DIALOG_STYLE_MSGBOX, DIALOG_STYLE_INPUT and DIALOG_STYLE_LIST. DIALOG_STYLE_PASSWORD is the same as DIALOG_STYLE_MSGBOX script-wise.

Prime explanation

First, I will explain the function ShowPlayerDialog.

I will explain the parameters for the function:

ShowPlayerDialog(playerid, dialogid, style, caption[], info[], button1[], button2[])
playeridThe ID of the player to show the dialog to.
dialogidAn ID to assign this dialog to, so responses can be processed.
styleThe style of the dialog.
caption[]The title at the top of the dialog.
info[]The text to display in the dialog. Use \n to start a new line and \t to tabulate.
button1[]The text on the left button.
button2[]The text on the right button.


And the callback which is fired whenever you select an option:

public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
playeridThe ID of the player wich clicked a button
dialogidThe ID of the dialog where the player clicked on a button
responseboolean, true if first button was pressed, false if second button was pressed
listitemOnly used in the style DIALOG_STYLE_LIST the first item is 0 and then counts up
inputtext[]Only used in the style DIALOG_STYLE_INPUT, it's a string with the input from the dialog


Now we've got that out of the way, we can proceed...

List dialog

First, let's start off by showing the dialog on a command. Add this under OnPlayerCommandText:

if(!strcmp(cmdtext, "/drinks", true))
{
    ShowPlayerDialog(playerid, 1, DIALOG_STYLE_LIST, "What is it that you want?", "Sprunk ($1)\nBeer ($2)\nWine ($3)", "Purchase", "Cancel");
    return 1;
}

Now that we've created the command along with the dialog, we have to setup the OnDialogResponse callback.

public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
if(response)// They pressed the first button. 
    {
    switch(dialogid)// If you only have one dialog, then this isn't required, but it's neater for when you implement more dialogs. 
        {
		case 1:// Our dialog!
    	    {
           	switch(listitem)// Checking which listitem was selected
        	{
        	    case 0:// The first item listed
        	    {
        	        if(GetPlayerMoney(playerid) < 1) return SendClientMessage(playerid, 0xFFFFFF, "You don't have enough cash.");
        	        GivePlayerMoney(playerid, -1);
        	        SetPlayerSpecialAction(playerid, SPECIAL_ACTION_DRINK_SPRUNK);
        	    }
        	    case 1: // The second item listed
        	    {
        	        if(GetPlayerMoney(playerid) < 2) return SendClientMessage(playerid, 0xFFFFFF, "You don't have enough cash.");
        	        GivePlayerMoney(playerid, -2);
        	        SetPlayerSpecialAction(playerid, SPECIAL_ACTION_DRINK_BEER);
        	    }
        	    case 2: // The third item listed
        	    {
        	        if(GetPlayerMoney(playerid) < 3) return SendClientMessage(playerid, 0xFFFFFF, "You don't have enough cash.");
        	        GivePlayerMoney(playerid, -3);
        	        SetPlayerSpecialAction(playerid, SPECIAL_ACTION_DRINK_WINE);
        	    }
        	}
    	    }
	}
    }
    return 1;
}

Let me explain what this does, there are two switches. One for the dialogid, and one for the listitem. The if statement at top is needed, otherwise it doesn't check which button you clicked.

MSGBOX dialog type

This is a simple one. It just is a textbox with two buttons no input no different choices just two buttons to process.

Add this under OnPlayerCommandText.

if(!strcmp(cmdtext, "/relax", true))
{
    ShowPlayerDialog(playerid, 2, DIALOG_STYLE_MSGBOX, "Are you sure?", "Are you sure you have time to relax?", "yes", "no");
    return 1;
}

This will show the dialog with ID 2 (we already used dialogid 1) to the player. The style here is DIALOG_STYLE_MSGBOX.

Now we need to set-up the response again...

public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
if(response)// They pressed the first button    
    {
    switch(dialogid)// Checking what dialog we're processing
        {
	    case 1:// Our first dialog
    	    {
           	switch(listitem)// Checking which item was chosen
        	{
        	    case 0: // The first listitem
        	    {
        	        if(GetPlayerMoney(playerid) < 1) return SendClientMessage(playerid, 0xFFFFFF, "You don't have enough cash.");
        	        GivePlayerMoney(playerid, -1);
        	        SetPlayerSpecialAction(playerid, SPECIAL_ACTION_DRINK_SPRUNK);
        	    }
        	    case 1: // The second listitem
        	    {
        	        if(GetPlayerMoney(playerid) < 2) return SendClientMessage(playerid, 0xFFFFFF, "You don't have enough cash.");
        	        GivePlayerMoney(playerid, -2);
        	        SetPlayerSpecialAction(playerid, SPECIAL_ACTION_DRINK_BEER);
        	    }
        	    case 2: // The third listitem
        	    {
        	        if(GetPlayerMoney(playerid) < 3) return SendClientMessage(playerid, 0xFFFFFF, "You don't have enough cash.");
        	        GivePlayerMoney(playerid, -3);
        	        SetPlayerSpecialAction(playerid, SPECIAL_ACTION_DRINK_WINE);
        	    }
        	}
    	    }
            //From here we added things
            case 2:// The new dialog
            {
                 ApplyAnimation(playerid,"BEACH","Lay_Bac_Loop",4.1,1,1,1,1,10);//this will let you relax for 10 seconds
            }
            // Till here
	}
    }
    return 1;
}

Explanation:

This only checks the dialogid, because it already checked if you pressed the first button which is 'yes', and then applies the animation for 10 seconds.

Input dialog

This dialog will have a line for input. (as the name implies).

We will make a chatdialog (what is typed in the dialog will appear in the chat).

This is a command created with strcmp, so it belongs under the OnPlayerCommandText callback.

if(!strcmp(cmdtext, "/chat", true))
{
    ShowPlayerDialog(playerid, 3, DIALOG_STYLE_INPUT, "Chat", "Type your chat input here.", "Submit", "Cancel");
    return 1;
}

Let's set-up the response, again..

public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
if(response)// Checking if they pressed the first button, if so continue:
    {
    switch(dialogid)//if your using only one dialog this isn't needed but you never know.
        {
	    case 1:// Our dialog
    	    {
           	switch(listitem)// Checking which listitem was chosen
        	{
        	    case 0: // The first item in the list 
        	    {
        	        if(GetPlayerMoney(playerid) < 1) return SendClientMessage(playerid, 0xFFFFFF, "You don't have enough cash.");
        	        GivePlayerMoney(playerid, -1);
        	        SetPlayerSpecialAction(playerid, SPECIAL_ACTION_DRINK_SPRUNK);
        	    }
        	    case 1: // The second item in the list
        	    {
        	        if(GetPlayerMoney(playerid) < 2) return SendClientMessage(playerid, 0xFFFFFF, "You don't have enough cash.");
        	        GivePlayerMoney(playerid, -2);
        	        SetPlayerSpecialAction(playerid, SPECIAL_ACTION_DRINK_BEER);
        	    }
        	    case 2: // The third item in the list
        	    {
        	        if(GetPlayerMoney(playerid) < 3) return SendClientMessage(playerid, 0xFFFFFF, "You don't have enough cash.");
        	        GivePlayerMoney(playerid, -3);
        	        SetPlayerSpecialAction(playerid, SPECIAL_ACTION_DRINK_WINE);
        	    }
        	}
    	    }
 
            case 2:// The new dialog
            {
                 ApplyAnimation(playerid,"BEACH","Lay_Bac_Loop",4.1,1,1,1,1,10);// This will let you "relax" for 10 seconds. 
            }
            // Here we add new things...
            case 3:
            {
                 if(strlen(inputtext) > 0)
                 {
                     SendPlayerMessageToAll(playerid, inputtext);
                 }
                 else
                 {
                     SendClientMessage(playerid,0xFFFFFFAA,"Your input was too short.");
                 }
            }
            // Until here.
	}
    }
    return 1;
}

As seen, this will print the inputtext string into a chat message for everyone.

Credits

Tutorial originally created by Calgon/FreddoX, but additions implemented by legodude. Re-written a majority of the tutorial because legodude failed to explain certain items as well as insufficient grammar (Calgon/FreddoX).

Personal tools
Navigation
Toolbox
In other languages