Author Topic: [TUTORIAL] How to use ShowPlayerDialog?  (Read 635 times)

xian

  • Administrator
  • Newbie
  • **********
  • Posts: 8
  • Reputation: 0
    • View Profile
[TUTORIAL] How to use ShowPlayerDialog?
« on: April 22, 2013, 09:22:11 pm »
Hello guys, before I start this tutorial, you should have a include called ZCMD because we'll use it later.

Remember to put ZCMD:
Code: [Select]
#include <a_samp>
#include <zcmd>

First, if you've ZCMD now, please find this and remove it:
Code: [Select]
public OnPlayerCommandText(playerid, cmdtext[])
{
   if (strcmp("/mycommand", cmdtext, true, 10) == 0)
   {
      // Do something here
      return 1;
   }
   return 0;
}

After that, make a new command with your newly ZCMD. >.>
Code: [Select]
CMD:dialog(playerid,params[])
{
   /*We'll learn here!*/
   return 1;
}

Alright, if you've copied the "/*We'll learn here*/", remove it, and make a ShowPlayerDialog.
Code: [Select]
CMD:dialog(playerid,params[])
{
   ShowPlayerDialog();
   return 1;
}

No, don't compile it yet. Now let's see the ShowPlayerDialog's parameters:
Code: [Select]
(playerid, dialogid, style, caption[], info[], button1[], button2[])I will explain it here now.
"playerid" - The ID of the player to show the dialog to. (Obviously to you)
"dialogid" - An ID to assign this dialog to, so responses can be processed. Max dialogid is 32767. Using negative values will close any open dialog.
"style" - The style of the dialog. (4 types of dialog, "DIALOG_STYLE_MSGBOX, DIALOG_STYLE_LIST, DIALOG_STYLE_INPUT, DIALOG_STYLE_PASSWORD *not recommended*)
"caption[]" - The title of the dialog.
"info" - Its info in it.
"button1[]" - The first button, for example, "yes".
"button2[]" - The second button, for example, "no", you can also use "" if you want to hide it.

Now, you've understand it, if you don't understand it, read it again.

Alright, let's make a dialog!
Code: [Select]
CMD:dialog(playerid,params[])
{
ShowPlayerDialog(playerid, 1, DIALOG_STYLE_MSGBOX, "Dialog", "Hello, this is my first dialog", "Yay!", "Boo!");
return 1;
}

This is an example dialog without response. (DIALOG_STYLE_MSGBOX is the suitable one)

Now, let's make a dialog with response. (I suck at this)
Code: [Select]
CMD:dialog2(playerid,params[])
{
ShowPlayerDialog(playerid, 2, DIALOG_STYLE_LIST, "Dialog", "Response 1\nResponse 2\nResponse 3", "Choose", "Exit");
return 1;
}

Alright, after creating it, now search for this:
Code: [Select]
public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
return 1;
}

Now, let's make a response.
Code: [Select]
public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
switch(dialogid)
{
    case 2:
    {
    if(response)
    {
        switch(listitem)
        {
        case 0: return SendClientMessage(playerid, 0xFFFFFFAA, "Response 1");
        case 1: return SendClientMessage(playerid, 0xFFFFFFAA, "Response 2"), DestroyVehicle(1); //With two response in return
        case 2: return SendClientMessage(playerid, 0xFFFFFFAA, "Response 3"), SetPlayerPos(playerid, 0.0, 0.0, 3.0),DestroyVehicle(2); //With 3 response in return
        }
}
}
}
return 1;
}
*DestroyVehicle is just giving an example!

Now, you're done, good luck in scripting OnPlayerDialog (You will only use DIALOG_STYLE_MSGBOX and LIST only, that's why I don't give a tutorial in INPUT and PASSWORD, not really know with INPUT and PASSWORD)

Share on Bluesky Share on Facebook


greentarch

  • Global Moderator
  • Newbie
  • ********
  • Posts: 16
  • Reputation: 0
    • View Profile
Re: [TUTORIAL] How to use ShowPlayerDialog?
« Reply #1 on: April 22, 2013, 10:28:12 pm »
Input dialog example(s) :
Code: [Select]
ShowPlayerDialog(
    playerid,
    1337,
    DIALOG_STYLE_INPUT,
    "Food Shop",
    "(1). Burger\n(2). Pizza\n(3). Donuts\nPlease input the number of the food you want to buy.",
    "Input", "Exit"
);

At OnDialogResponse :
Code: [Select]
switch ( dialogid )
{
    case 1337:
    {
        if ( response )
        {
            new food = strval( inputtext );
            if ( !( 1 <= food <= 3 ) ) return SendClientMessage( playerid, -1, "Invalid food ID! <1-3>" );
            new food_name[ 100 ];
            switch ( food )
            {
                case 0: food_name = "Burger";
                case 1: food_name = "Pizza";
                case 2: food_name = "Donuts";
            }
            new str[128]; format(str, 128, "You have bought a %s", food_name);
            SendClientMessage(playerid, -1, str);
        }
    }
}

xian

  • Administrator
  • Newbie
  • **********
  • Posts: 8
  • Reputation: 0
    • View Profile
Re: [TUTORIAL] How to use ShowPlayerDialog?
« Reply #2 on: April 22, 2013, 11:07:08 pm »
Input dialog example(s) :
Code: [Select]
ShowPlayerDialog(
    playerid,
    1337,
    DIALOG_STYLE_INPUT,
    "Food Shop",
    "(1). Burger\n(2). Pizza\n(3). Donuts\nPlease input the number of the food you want to buy.",
    "Input", "Exit"
);

At OnDialogResponse :
Code: [Select]
switch ( dialogid )
{
    case 1337:
    {
        if ( response )
        {
            new food = strval( inputtext );
            if ( !( 1 <= food <= 3 ) ) return SendClientMessage( playerid, -1, "Invalid food ID! <1-3>" );
            new food_name[ 100 ];
            switch ( food )
            {
                case 0: food_name = "Burger";
                case 1: food_name = "Pizza";
                case 2: food_name = "Donuts";
            }
            new str[128]; format(str, 128, "You have bought a %s", food_name);
            SendClientMessage(playerid, -1, str);
        }
    }
}
yum, yum, that's delicious. Anyway, thanks for listing out a tutorial of INPUT. ;)

KiNG3

  • Global Moderator
  • Newbie
  • ********
  • Posts: 10
  • Reputation: 0
    • View Profile
Re: [TUTORIAL] How to use ShowPlayerDialog?
« Reply #3 on: April 25, 2013, 06:52:46 am »
Kelvin, you actually defined some of the stuff in the tutorial. Nicely done!