Skip to content

Instantly share code, notes, and snippets.

@jorgen99
Created February 5, 2020 01:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jorgen99/10f40e3ed74058455d05a4a58c5fd579 to your computer and use it in GitHub Desktop.
Save jorgen99/10f40e3ed74058455d05a4a58c5fd579 to your computer and use it in GitHub Desktop.
Example on drawing random dice from a bag in Tabletop Simulator
-- Example on how to take a die from a shuffled bag
-- and put the die on its space.
-- A reply to this question on Reddit:
-- https://www.reddit.com/r/tabletopsimulator/comments/eft9h6/scripting_how_to_randomly_choose_objects_in_a_bag/
d20_guid = "2f4af6"
d12_guid = "d90df3"
d10_guid = "327eec"
d8_guid = "213698"
d4_guid = "0b8595"
dice_guids = { d20_guid, d12_guid, d10_guid, d8_guid, d4_guid }
bag_guid = "bb4f30"
-- Use the guid as the key and positions as values in
-- in the positions table. When we draw a die from the
-- bag we use this lookup table to see where we shuld
-- place it.
positions = {
-- x, y, z for each die.
-- y is the height over the table and setting it to
-- a number higher than zero will make it fall down
-- to the board
[d20_guid] = {-3.05, 3, 3.04},
[d12_guid] = {-1.02, 3, 3.05},
[d10_guid] = { 1.01, 3, 3.05},
[ d8_guid] = { 3.04, 3, 3.05},
[ d4_guid] = { 5.07, 3, 3.05}
}
-- The buttons are small go-stones that we
-- use to create the button-labels on
draw_button_guid = "bba56e"
reset_button_guid = "c43e43"
function onLoad()
button_setup("Draw random dice", draw_button_guid, 'drawDice', 7000)
button_setup("Reset", reset_button_guid, 'resetDice', 4000)
end
function drawDice()
math.randomseed(os.time())
-- If we want to use yield to wait/sleep in the waitFrames
-- function we need to start a Coroutine. See:
-- https://api.tabletopsimulator.com/base/#startluacoroutine
startLuaCoroutine(Global, "drawDiceCoroutine")
end
function drawDiceCoroutine()
local bag = getObjectFromGUID(bag_guid)
bag.shuffle()
-- find out how many dice there are in the bag
local dice = bag.getObjects()
for i=1, #dice do
-- take the first die
local die = bag.takeObject()
-- look up the position based on the guid
local die_position = positions[die.guid]
die.setPositionSmooth(die_position)
waitFrames(200)
end
-- https://api.tabletopsimulator.com/base/#startluacoroutine
return 1 -- otherwise scripting error
end
function resetDice()
local bag = getObjectFromGUID(bag_guid)
for _, die_guid in ipairs(dice_guids) do
local die = getObjectFromGUID(die_guid)
bag.putObject(die)
end
end
function waitFrames(frames)
while frames > 0 do
coroutine.yield(0)
frames = frames - 1
end
end
function button_setup(label, guid, function_name, width, height, font_size)
local button = getObjectFromGUID(guid)
if button == nil then
print("Button '", label, "' does not exist, skipping it...")
return
end
local button_parameters = {}
button_parameters.click_function = function_name
button_parameters.label = label
button_parameters.function_owner = nil
button_parameters.position = { 0, 0.2, 0 }
button_parameters.rotation = {0, 180, 0}
button_parameters.width = width
button_parameters.height = 1500
button_parameters.font_size = 800
button.createButton(button_parameters)
return button
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment