Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Universal Counter Tokens coded by: MrStump
- --Saves the count value into a table (data_to_save) then encodes it into the Tabletop save
- function onSave()
- local data_to_save = {}
- data_to_save.saved_counts = {}
- for i,v in pairs(buttons.counts) do
- data_to_save.saved_counts[tostring(i)] = v or 0
- end
- saved_data = JSON.encode(data_to_save)
- --Uncomment this line to reset the save data, necessary when messing with variable names
- --saved_data = ''
- return saved_data
- end
- --Loads the saved data then creates the buttons
- function onload(saved_data)
- buttons = {}
- buttons.index = 0
- buttons.counts = {}
- buttons.params = {}
- buttons.params.positions = {}
- buttons.params.positions.offsets = {}
- buttons.params.sizes = {}
- buttons.targetFunc = {}
- ------------------------------
- --Edit below this line
- ------------------------------
- --to add a location, simply add a new button.whatever table with a type
- buttons.centralTorso = {type = 'armor'}
- buttons.head = {type = 'armor'}
- buttons.rightTorso = {type = 'armor'}
- buttons.leftTorso = {type = 'armor'}
- buttons.rightArm = {type = 'armor'}
- buttons.leftArm = {type = 'armor'}
- buttons.rightLeg = {type = 'armor'}
- buttons.leftLeg = {type = 'armor'}
- --if you added a location, you need to give it a position, coordinates are relative to the center of the model
- buttons.params.positions.centralTorso = {x = 0, y = 0.1, z = -0.75}
- buttons.params.positions.head = {x = 0, y = 0.1, z = -1.4}
- buttons.params.positions.rightTorso = {x = 0.47, y = 0.1, z = -1.1}
- buttons.params.positions.leftTorso = {x = -0.47, y = 0.1, z = -1.1}
- buttons.params.positions.rightArm = {x = 1, y = 0.1, z = -1}
- buttons.params.positions.leftArm = {x = -1, y = 0.1, z = -1}
- buttons.params.positions.rightLeg = {x = 0.67, y = 0.1, z = 1.1}
- buttons.params.positions.leftLeg = {x = -0.67, y = 0.1, z = 1.1}
- --make a style of button by adding a new size
- buttons.params.sizes.armor = {}
- buttons.params.sizes.armor.display = {width = 200, height = 100, font = 100}
- buttons.params.sizes.armor.button = {width = 50, height = 50, font = 50}
- buttons.params.sizes.armor.offsets = {
- topButtons = {x = 0.1, y = 0, z = -0.17},
- bottomButtons = {x = 0.1, y = 0, z = 0.17}
- }
- ------------------------------
- --Don't touch below this line
- ------------------------------
- for i,v in pairs(buttons) do
- buttons.counts[tostring(i)] = 0
- if tostring(i) != 'index' and tostring(i) != 'counts' and tostring(i) != 'params' and tostring(i) != 'targetFunc' then
- self.setVar(tostring(i) .. 'PlusOne', function () plus(tostring(i), 1) end)
- self.setVar(tostring(i) .. 'PlusFive', function () plus(tostring(i), 5) end)
- self.setVar(tostring(i) .. 'MinusOne', function () minus(tostring(i), 1) end)
- self.setVar(tostring(i) .. 'MinusFive', function () minus(tostring(i), 5) end)
- end
- end
- generateButtonParamiters()
- --Checks if there is a saved data. If there is, it gets the saved value for 'buttons.counts.ct'
- if saved_data != '' then
- local loaded_data = JSON.decode(saved_data)
- buttons.counts = loaded_data.saved_counts
- for i,v in pairs(buttons) do
- if tostring(i) != 'index' and tostring(i) != 'counts' and tostring(i) != 'params' and tostring(i) != 'targetFunc' then
- if buttons.counts[tostring(i)] == nil then
- buttons.counts[tostring(i)] = 0
- end
- end
- end
- else
- for i,v in pairs(buttons) do
- if tostring(i) != 'index' and tostring(i) != 'counts' and tostring(i) != 'params' and tostring(i) != 'targetFunc' then
- buttons.counts[tostring(i)] = 0
- end
- end
- end
- for i,v in pairs(buttons) do
- if tostring(i) != 'index' and tostring(i) != 'counts' and tostring(i) != 'params' and tostring(i) != 'targetFunc' then
- buttons[tostring(i)].display.label = tostring(buttons.counts[tostring(i)])
- self.createButton(buttons[tostring(i)].display)
- self.createButton(buttons[tostring(i)].plusOne)
- self.createButton(buttons[tostring(i)].plusFive)
- self.createButton(buttons[tostring(i)].minusOne)
- self.createButton(buttons[tostring(i)].minusFive)
- end
- end
- end
- function dud()
- end
- function plus(location, amount)
- buttons.counts[location] = buttons.counts[location] + amount
- updateDisplay()
- end
- function minus(location, amount)
- --Prevents count from going below 0
- if buttons.counts[location] > amount - 1 then
- buttons.counts[location] = buttons.counts[location] - amount
- else
- buttons.counts[location] = 0
- end
- updateDisplay()
- end
- --This is activated when onload runs. This sets all paramiters for our buttons.
- --I do not have to put this all into a function, but I prefer to do it this way.
- function setupButton(targetFunc, positions, sizes, label, offsets, isLeft)
- local buttonInfo = {}
- buttonInfo.function_owner = self
- buttonInfo.index = buttons.index
- buttonInfo.click_function = targetFunc
- if isLeft then
- orientationModifier = -1
- else
- orientationModifier = 1
- end
- if offsets == nil then
- buttonInfo.position = {positions.x, positions.y, positions.z}
- else
- buttonInfo.position = {positions.x + (offsets.x) * orientationModifier, positions.y + offsets.y, positions.z + offsets.z}
- end
- buttonInfo.width = sizes.width
- buttonInfo.height = sizes.height
- buttonInfo.font_size = sizes.font
- buttonInfo.label = label
- buttons.index = buttons.index + 1
- return buttonInfo
- end
- function generateButtonParamiters()
- for i,v in pairs(buttons) do
- if tostring(i) != 'index' and tostring(i) != 'counts' and tostring(i) != 'params' and tostring(i) != 'targetFunc' then
- buttons[tostring(i)].display = setupButton('dud', buttons.params.positions[tostring(i)], buttons.params.sizes[buttons[tostring(i)].type].display, '', nil, false)
- buttons[tostring(i)].plusOne = setupButton(tostring(i) .. 'PlusOne', buttons.params.positions[tostring(i)], buttons.params.sizes[buttons[tostring(i)].type].button, '+1', buttons.params.sizes[buttons[tostring(i)].type].offsets.bottomButtons, false)
- buttons[tostring(i)].plusFive = setupButton(tostring(i) .. 'PlusFive', buttons.params.positions[tostring(i)], buttons.params.sizes[buttons[tostring(i)].type].button, '+5', buttons.params.sizes[buttons[tostring(i)].type].offsets.topButtons, false)
- buttons[tostring(i)].minusOne = setupButton(tostring(i) .. 'MinusOne', buttons.params.positions[tostring(i)], buttons.params.sizes[buttons[tostring(i)].type].button, '-1', buttons.params.sizes[buttons[tostring(i)].type].offsets.bottomButtons, true)
- buttons[tostring(i)].minusFive = setupButton(tostring(i) .. 'MinusFive', buttons.params.positions[tostring(i)], buttons.params.sizes[buttons[tostring(i)].type].button, '-5',buttons.params.sizes[buttons[tostring(i)].type].offsets.topButtons, true)
- end
- end
- end
- function updateDisplay()
- for i,v in pairs(buttons) do
- if tostring(i) != 'index' and tostring(i) != 'counts' and tostring(i) != 'params' and tostring(i) != 'targetFunc' then
- buttons[tostring(i)].display.label = tostring(buttons.counts[tostring(i)])
- self.editButton(buttons[tostring(i)].display)
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement