Advertisement
TokMor

Untitled

Jan 17th, 2021
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. -- Universal Counter Tokens coded by: MrStump
  2.  
  3. --Saves the count value into a table (data_to_save) then encodes it into the Tabletop save
  4. function onSave()
  5. local data_to_save = {saved_count = count}
  6. saved_data = JSON.encode(data_to_save)
  7. return saved_data
  8. end
  9.  
  10. --Loads the saved data then creates the buttons
  11. function onload(saved_data)
  12. generateButtonParamiters()
  13. --Checks if there is a saved data. If there is, it gets the saved value for 'count'
  14. if saved_data != '' then
  15. local loaded_data = JSON.decode(saved_data)
  16. count = loaded_data.saved_count
  17. else
  18. --If there wasn't saved data, the default value is set to 0.
  19. count = 0
  20. end
  21.  
  22. --Generates the buttons after putting the count value onto the 'display' button
  23. b_display.label = tostring(count)
  24. if count >= 100 then
  25. b_display.font_size = 500
  26. else
  27. b_display.font_size = 1000
  28. end
  29. self.createButton(b_display)
  30. self.createButton(b_plus)
  31. self.createButton(b_minus)
  32. end
  33.  
  34. --Activates when + is hit. Adds 1 to 'count' then updates the display button.
  35. function increase()
  36. count = count + 1
  37. updateDisplay()
  38. end
  39.  
  40. --Activates when - is hit. Subtracts 1 from 'count' then updates the display button.
  41. function decrease()
  42. --Prevents count from going below 0
  43. if count > 0 then
  44. count = count - 1
  45. updateDisplay()
  46. end
  47. end
  48.  
  49.  
  50. function customSet()
  51. local description = self.getDescription()
  52. if description != '' and type(tonumber(description)) == 'number' then
  53.  
  54. count = tonumber(description)
  55. updateDisplay()
  56. end
  57. end
  58.  
  59. --function that updates the display. I trigger it whenever I change 'count'
  60. function updateDisplay()
  61. --If statement to resize font size if it gets too long
  62. if count >= 100 then
  63. b_display.font_size = 500
  64. else
  65. b_display.font_size = 1500
  66. end
  67. b_display.label = tostring(count)
  68. self.editButton(b_display)
  69. end
  70.  
  71. --This is activated when onload runs. This sets all paramiters for our buttons.
  72. --I do not have to put this all into a function, but I prefer to do it this way.
  73. function generateButtonParamiters()
  74. b_display = {
  75. index = 0, click_function = 'customSet', function_owner = self, label = '',
  76. position = {0,0.33,0}, width = 1000, height = 1000, font_size = 1000, color = {0,0,0,0}, font_color = {1,1,1,255}
  77. }
  78. b_plus = {
  79. click_function = 'increase', function_owner = self, label = '+',
  80. position = {1.4,0.33,0}, width = 500, height = 500, font_size = 1000, color = {0,0,0,0}, font_color = {1,1,1,255}
  81. }
  82. b_minus = {
  83. click_function = 'decrease', function_owner = self, label = '-',
  84. position = {-1.4,0.33,0}, width = 500, height = 500, font_size = 1000, color = {0,0,0,0}, font_color = {1,1,1,255}
  85. }
  86. end
  87.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement