Advertisement
Guest User

Untitled

a guest
May 25th, 2016
358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. function Player:sendShopWindow(potions)
  2. local function buttonCallback(button, choice)
  3. -- Modal Window Functionallity
  4. -- Variables
  5. local count = 0
  6. local price = 0
  7. local potion = potions[choice.id]
  8.  
  9. -- This checks to make sure the choice ID is true. If not return error
  10. if not potion then
  11. print("Something went wrong with choice id: " .. choice.id)
  12. return false
  13. end
  14.  
  15. -- Button funcctionallity
  16. -- If the player selects buy 100 set count and price for 100 potions.
  17. if button.text == "Buy 100" then
  18. count = 100
  19. price = potion.price100
  20.  
  21. -- If the player selects buy 50 set count and price for 100 potions.
  22. elseif button.text == "Buy 50" then
  23. count = 50
  24. price = potion.price50
  25.  
  26. -- If the player selects buy 10 set count and price for 100 potions.
  27. elseif button.text == "Buy 10" then
  28. count = 10
  29. price = potion.price10
  30. else
  31.  
  32. -- If the window returns something else or if somthing funny happens return error.
  33. print("Something went wrong with button: " .. button.text)
  34. return false
  35. end
  36.  
  37. -- Player checks
  38. -- Check if player has enough money. If not return moneyMsg.
  39. if self:getMoney() < price then
  40. self:say(potions.moneyMsg, TALKTYPE_MONSTER_SAY)
  41. return false
  42. end
  43.  
  44. -- Check if player has enough capacity If not return capacityMsg if true add item to player.
  45. local potionEx = Game.createItem(potion.itemID, count)
  46. if self:addItemEx(potionEx, false) ~= RETURNVALUE_NOERROR then
  47. self:getPosition():sendMagicEffect(CONST_ME_POFF)
  48. self:say(potions.capacityMsg, TALKTYPE_MONSTER_SAY)
  49. return true
  50. end
  51.  
  52. -- If player passes checks remove money, send buyMsg and resend window.
  53. self:removeMoney(price)
  54. self:sendTextMessage(MESSAGE_EVENT_ADVANCE, potions.buyMsg .. count .. "x ".. potion.potion .."s")
  55. self:sendShopWindow(potions)
  56. end
  57.  
  58. -- Modal window design
  59. local window = ModalWindow {
  60. title = potions.titleMsg, -- Title of the modal window
  61. message = potions.mainMsg, -- The message to be displayed on the modal window
  62. }
  63.  
  64. -- Add buttons to the window (Note: if you change the names of these you must change the functions in the modal window functionallity!)
  65. window:addButton("Buy 10", buttonCallback)
  66. window:addButton("Buy 50", buttonCallback)
  67. window:addButton("Buy 100", buttonCallback)
  68. window:addButton("Cancel")
  69.  
  70. -- Set what button is pressed when the player presses enter or escape
  71. window:setDefaultEnterButton("Buy 100")
  72. window:setDefaultEscapeButton("Cancel")
  73.  
  74. -- Add choices from the action script
  75. for i = 1, #potions do
  76. local o = potions[i].potion
  77. local choice = window:addChoice(o)
  78. choice.id = i
  79. end
  80.  
  81. -- Send the window to player
  82. window:sendToPlayer(self)
  83. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement