Examples/Dialogs_password_login_and_a_weapon_shop
Server-only example (no client script) using ShowPlayerDialog. A password prompt opens on join, then /shop shows a price list as a tab list with headers. Dialog ids are just your own numbers; here 1 = login, 2 = shop, 3 = confirmation.
DLG_LOGIN <- 1;
DLG_SHOP <- 2;
DLG_INFO <- 3;
// weapon, price, ammo - the row index the player picks is listitem
shopWeapon <- [WEP_COLT45, WEP_UZI, WEP_SHOTGUN, WEP_AK47];
shopPrice <- [100, 500, 800, 1500];
shopName <- ["Colt 45", "Uzi", "Shotgun", "AK-47"];
loginTries <- array(GetMaxPlayers(), 0);
function onPlayerJoin(player)
{
loginTries[player.ID] = 0;
print(player.Name + " LUID " + player.LUID); // 40 hex chars, per machine
ShowPlayerDialog(player, DLG_LOGIN, DIALOG_STYLE_PASSWORD,
"{FFD700}Welcome", "Type the server password:", "Login", "Quit");
}
function onPlayerCommand(player, cmd, params)
{
if (cmd == "shop")
{
local list = "Weapon\tPrice\n"; // first line = headers (style 5)
for (local i = 0; i < shopName.len(); i++)
list += shopName[i] + "\t{00FF00}$" + shopPrice[i] + "\n";
ShowPlayerDialog(player, DLG_SHOP, DIALOG_STYLE_TABLIST_HEADERS,
"Weapon shop", list, "Buy", "Close");
return true;
}
return false;
}
function onDialogResponse(player, dialogid, response, listitem, inputtext)
{
if (dialogid == DLG_LOGIN)
{
if (!response) { KickPlayer(player); return; } // pressed "Quit"
if (inputtext == "letmein")
{
MessagePlayer("Welcome, " + player.Name + "!", player);
player.Spawn();
}
else if (++loginTries[player.ID] >= 3) KickPlayer(player);
else ShowPlayerDialog(player, DLG_LOGIN, DIALOG_STYLE_PASSWORD,
"{FF0000}Wrong password",
"Try again (" + loginTries[player.ID] + "/3):", "Login", "Quit");
}
else if (dialogid == DLG_SHOP)
{
if (!response) return; // "Close" / ESC
if (listitem < 0 || listitem >= shopName.len()) return; // validate the index
if (player.Cash < shopPrice[listitem])
{
ShowPlayerDialog(player, DLG_INFO, DIALOG_STYLE_MSGBOX, "Shop",
"{FF0000}You cannot afford the " + shopName[listitem] + ".", "OK", "");
return;
}
player.Cash = player.Cash - shopPrice[listitem];
player.SetWeapon(shopWeapon[listitem], 100);
ShowPlayerDialog(player, DLG_INFO, DIALOG_STYLE_MSGBOX, "Shop",
"You bought the " + shopName[listitem], "OK", "");
}
}An input dialog works the same way: ShowPlayerDialog(player, 10, DIALOG_STYLE_INPUT, "Nickname", "Choose a name:", "OK", "Cancel"), then read inputtext when dialogid == 10 && response == 1. A plain list is DIALOG_STYLE_LIST with "Line 1\nLine 2\nLine 3".
Categories: Worked examples