OnDialogResponse

From SA-MP Wiki

Jump to: navigation, search



OnDialogResponse was added in SA-MP 0.3a This callback was added in SA-MP 0.3a and will not work in earlier versions!


This callback is called when a player responds to a dialog shown using ShowPlayerDialog by either clicking a button, pressing ENTER/ESC or double-clicking a list item (if using a list style dialog).


Parameters:
(playerid, dialogid, response, listitem, inputtext[])
playeridThe ID of the player that responded to the dialog.
dialogidThe ID of the dialog the player responded to, assigned in ShowPlayerDialog.
response1 for left button and 0 for right button (if only one button shown, always 1)
listitemThe ID of the list item selected by the player (starts at 0) (only if using a list style dialog).
inputtext[]The text entered into the input box by the player or the selected list item text.


ReturnsReturning 0 in this callback will pass the dialog to another script in case no matching code were found in your gamemode's callback.


DIALOG_STYLE_MSGBOX

// Define the dialog ID so we can handle responses
#define DIALOG_RULES 1
 
// In some command
ShowPlayerDialog(playerid, DIALOG_RULES, DIALOG_STYLE_MSGBOX, "Server Rules", "- No Cheating\n- No Spamming\n- Respect Admins\n\nDo you agree to these rules?", "Yes", "No");
 
public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
    if(dialogid == DIALOG_WELCOME)
    {
        if(response) // If they clicked 'Yes' or pressed enter
        {
            SendClientMessage(playerid, COLOR_GREEN, "Thank you for agreeing to the server rules!");
        }
        else // Pressed ESC or clicked cancel
        {
            KickWithMessage(playerid, COLOR_RED, "You MUST agree to the server rules to play here.");
            //For info & code of this function please refer to the bottom of this article.
        }
        return 1; // We handled a dialog, so return 1. Just like OnPlayerCommandText.
    }
 
    return 0; // You MUST return 0 here! Just like OnPlayerCommandText.
}


DIALOG_STYLE_INPUT/DIALOG_STYLE_PASSWORD

#define DIALOG_LOGIN 2
 
// In some command
ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_INPUT, "Login", "Please enter your password:", "Login", "Cancel");
 
public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
    if(dialogid == DIALOG_LOGIN)
    {
        if(!response) // If they clicked 'Cancel' or pressed esc
        {
            KickWithMessage(playerid, COLOR_RED, "You MUST login to play here. Please change your name.");
            //For info & code of this function please refer to the bottom of this article.
        }
        else // Pressed ENTER or clicked 'Login' button
        {
            if(CheckPassword(playerid, inputtext))
            {
                SendClientMessage(playerid, COLOR_RED, "You are now logged in!");
            }
            else
            {
                SendClientMessage(playerid, COLOR_RED, "LOGIN FAILED.");
 
                // Re-show the login dialog
                ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_INPUT, "Login", "Please enter your password:", "Login", "Cancel");
            }
        }
        return 1; // We handled a dialog, so return 1. Just like OnPlayerCommandText.
    }
 
    return 0; // You MUST return 0 here! Just like OnPlayerCommandText.
}


DIALOG_STYLE_LIST

#define DIALOG_WEAPONS 3
 
// In some command
ShowPlayerDialog(playerid, DIALOG_WEAPONS, DIALOG_STYLE_LIST, "Weapons", "Desert Eagle\nAK-47\nCombat Shotgun", "Select", "Close");
 
public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
    if(dialogid == DIALOG_WEAPONS)
    {
        if(response) // If they clicked 'Select' or double-clicked a weapon
        {
            // Give them the weapon
            if(listitem == 0) // They selected the first item - Desert Eagle
            {
                GivePlayerWeapon(playerid, WEAPON_DEAGLE, 14); // Give them a desert eagle
            }
            if(listitem == 1) // They selected the second item - AK-47
            {
                GivePlayerWeapon(playerid, WEAPON_AK47, 120); // Give them an AK-47
            }
            if(listitem == 2) // They selected the third item - Desert Eagle
            {
                GivePlayerWeapon(playerid, WEAPON_SHOTGSPA, 28); // Give them a Combat Shotgun
            }
        }
        return 1; // We handled a dialog, so return 1. Just like OnPlayerCommandText.
    }
 
    return 0; // You MUST return 0 here! Just like OnPlayerCommandText.
}

KickWithMessage(); function:

forward KickPublic(playerid);
public KickPublic(playerid) { Kick(playerid); }
 
stock KickWithMessage(playerid, color, message[])
{
    SendClientMessage(playerid, color, message);
    SetTimerEx("KickPublic", 1000, 0, "d", playerid); 	//Delay of 1 second before kicking the player so he recieves the message
}

For more information on this technique please visit the article of Kick().


Related Functions

The following functions might be useful, as they're related to this callback in one way or another.

Personal tools