Tabletop Simulator

Tabletop Simulator

View Stats:
Kaija Aug 25, 2022 @ 3:13pm
TTS Scripting, need help with Lua (Beginner)
Hello there,

So i'm trying to implement a function from a mod, into another mod and i just cant wrap my head around it, i dont know, but it doesnt sound that hard to do.

so these are the two mods im working with:
https://steamcommunity.com/sharedfiles/filedetails/?id=1124124634
https://steamcommunity.com/sharedfiles/filedetails/?id=1135358272

What i'm trying to do is, implement the "Lock" function that exists in the "Better Notecards" Mod, into the Character Sheet Template, so that you have one button you can click, and all textfields getting locked, a thing that would be more awesome to add the multiple pages function, but thats not the main point.So if anyone has any idea or can help me, that would be awesome.Thanks for checking my post :)

Uploaded The Scripts of both mods on Pastebin for easy access.
Character Sheet Template Mod Code: https://pastebin.com/viRUfrwC
Better Notecards Mod Code: https://pastebin.com/qQPFS8hi
Last edited by Kaija; Aug 25, 2022 @ 3:16pm
< >
Showing 1-11 of 11 comments
Kaija Aug 26, 2022 @ 11:47am 
Originally posted by Finaryman:

disableSave = true
--Remember to set this to false once you are done making changes
--Then, after you save & apply it, save your game too

--Color information for button text (r,g,b, values of 0-1)
buttonFontColor = {0,0,0}
--Color information for button background
buttonColor = {1,1,1}
--Change scale of button (Avoid changing if possible)
buttonScale = {0.1,0.1,0.1}

--This is the button placement information
defaultButtonData = {
--Add checkboxes
checkbox = {
--[[
pos = the position (pasted from the helper tool)
size = height/width/font_size for checkbox
state = default starting value for checkbox (true=checked, false=not)
]]
{
pos = {1.12,0.1,-1.78},
size = 800,
state = true
},
--First checkbox
{
pos = {-0.977,0.1,-0.589},
size = 800,
state = false
},
--Second checkbox
{
pos = {-0.163,0.1,-0.594},
size = 800,
state = false
},
--Third checkbox
{
pos = {0.735,0.1,-0.592},
size = 800,
state = false
},
--End of checkboxes
},
--Add counters that have a + and - button
counter = {
--[[
pos = the position (pasted from the helper tool)
size = height/width/font_size for counter
value = default starting value for counter
hideBG = if background of counter is hidden (true=hidden, false=not)
]]
--First counter
{
pos = {-0.996,0.1,0.057},
size = 800,
value = 0,
hideBG = true
},
--Second counter
{
pos = {-0.151,0.1,0.043},
size = 800,
value = 0,
hideBG = false
},
--Third counter
{
pos = {0.736,0.1,0.051},
size = 800,
value = 0,
hideBG = true
},
--End of counters
},
--Add editable text boxes
textbox = {
--[[
pos = the position (pasted from the helper tool)
rows = how many lines of text you want for this box
width = how wide the text box is
font_size = size of text. This and "rows" effect overall height
label = what is shown when there is no text. "" = nothing
value = text entered into box. "" = nothing
alignment = Number to indicate how you want text aligned
(1=Automatic, 2=Left, 3=Center, 4=Right, 5=Justified)
]]
--First textbox
{
pos = {-0.552,0.1,1.213},
rows = 16,
width = 4000,
font_size = 300,
label = "Empty",
value = "",
alignment = 2
},
--Second textbox
{
pos = {0.572,0.1,0.768},
rows = 1,
width = 3000,
font_size = 600,
label = "Broke",
value = "",
alignment = 3
},
--Third textbox
{
pos = {0.596,0.1,1.393},
rows = 6,
width = 4000,
font_size = 400,
label = "Brickabrackless",
value = "",
alignment = 4
},
--End of textboxes
}
}

--Save function
function updateSave()
saved_data = JSON.encode(ref_buttonData)
if disableSave==true then saved_data="" end
self.script_state = saved_data
end

--Startup procedure
function onload(saved_data)
if disableSave==true then saved_data="" end
if saved_data ~= "" then
local loaded_data = JSON.decode(saved_data)
ref_buttonData = loaded_data
else
ref_buttonData = defaultButtonData
end

spawnedButtonCount = 0
createCheckbox()
createCounter()
createTextbox()
end



--Click functions for buttons



--Checks or unchecks the given box
function click_checkbox(tableIndex, buttonIndex)
if ref_buttonData.checkbox[1].state then
if ref_buttonData.checkbox[tableIndex].state == true then
ref_buttonData.checkbox[tableIndex].state = false
self.editButton({index=buttonIndex, label=""})
else
ref_buttonData.checkbox[tableIndex].state = true
self.editButton({index=buttonIndex, label=string.char(10008)})
end
updateSave()
end
end
--Predefined special checkbox (to lock other controls)
function click_checkboxE(tableIndex, buttonIndex)
if ref_buttonData.checkbox[tableIndex].state == true then
ref_buttonData.checkbox[tableIndex].state = false
self.editButton({index=buttonIndex, label=""})
else
ref_buttonData.checkbox[tableIndex].state = true
self.editButton({index=buttonIndex, label=string.char(10008)})
end
updateSave()
end

--Applies value to given counter display
function click_counter(tableIndex, buttonIndex, amount)
if ref_buttonData.checkbox[1].state then
ref_buttonData.counter[tableIndex].value = ref_buttonData.counter[tableIndex].value + amount
self.editButton({index=buttonIndex, label=ref_buttonData.counter[tableIndex].value})
updateSave()
end
end

--Updates saved value for given text box
function click_textbox(i, value, selected)
if ref_buttonData.checkbox[1].state then
if selected == false then
ref_buttonData.textbox[i].value = value
updateSave()
end
end
end

--Dud function for if you have a background on a counter
function click_none() end



--Button creation



--Makes checkboxes
function createCheckbox()
for i, data in ipairs(ref_buttonData.checkbox) do
--Sets up reference function
local buttonNumber = spawnedButtonCount
local funcName = "checkbox"..i
local func
if i == 1 then
func = function() click_checkboxE(i, buttonnumber) end
else
func = function() click_checkbox(i, buttonNumber) end
end
self.setVar(funcName, func)
--Sets up label
local label = ""
if data.state==true then label=string.char(10008) end
--Creates button and counts it
self.createButton({
label=label, click_function=funcName, function_owner=self,
position=data.pos, height=data.size, width=data.size,
font_size=data.size, scale=buttonScale,
color=buttonColor, font_color=buttonFontColor
})
spawnedButtonCount = spawnedButtonCount + 1
end
end

--Makes counters
function createCounter()
for i, data in ipairs(ref_buttonData.counter) do
--Sets up display
local displayNumber = spawnedButtonCount
--Sets up label
local label = data.value
--Sets height/width for display
local size = data.size
if data.hideBG == true then size = 0 end
--Creates button and counts it
self.createButton({
label=label, click_function="click_none", function_owner=self,
position=data.pos, height=size, width=size,
font_size=data.size, scale=buttonScale,
color=buttonColor, font_color=buttonFontColor
})
spawnedButtonCount = spawnedButtonCount + 1

--Sets up add 1
local funcName = "counterAdd"..i
local func = function() click_counter(i, displayNumber, 1) end
self.setVar(funcName, func)
--Sets up label
local label = "+"
--Sets up position
local offsetDistance = (data.size/2 + data.size/4) * (buttonScale[1] * 0.002)
local pos = {data.pos[1] + offsetDistance, data.pos[2], data.pos[3]}
--Sets up size
local size = data.size / 2
--Creates button and counts it
self.createButton({
label=label, click_function=funcName, function_owner=self,
position=pos, height=size, width=size,
font_size=size, scale=buttonScale,
color=buttonColor, font_color=buttonFontColor
})
spawnedButtonCount = spawnedButtonCount + 1

--Sets up subtract 1
local funcName = "counterSub"..i
local func = function() click_counter(i, displayNumber, -1) end
self.setVar(funcName, func)
--Sets up label
local label = "-"
--Set up position
local pos = {data.pos[1] - offsetDistance, data.pos[2], data.pos[3]}
--Creates button and counts it
self.createButton({
label=label, click_function=funcName, function_owner=self,
position=pos, height=size, width=size,
font_size=size, scale=buttonScale,
color=buttonColor, font_color=buttonFontColor
})
spawnedButtonCount = spawnedButtonCount + 1
end
end

function createTextbox()
for i, data in ipairs(ref_buttonData.textbox) do
--Sets up reference function
local funcName = "textbox"..i
local func = function(_,_,val,sel) click_textbox(i,val,sel) end
self.setVar(funcName, func)

self.createInput({
input_function = funcName,
function_owner = self,
label = data.label,
alignment = data.alignment,
position = data.pos,
scale = buttonScale,
width = data.width,
height = (data.font_size*data.rows)+24,
font_size = data.font_size,
color = buttonColor,
font_color = buttonFontColor,
value = data.value,
})
end
end

I really appreciate your help!, but i'm not sure i understand how i can lock und unlock the textfields.

it locks other stuff but sadly not the test fields i want to Lock.
Last edited by Kaija; Aug 26, 2022 @ 11:53am
Kaija Aug 26, 2022 @ 12:11pm 
Originally posted by Finaryman:
just forgot to add if to texfields as well, look on other examples

Hello, i'm really sorry i dont know much about scripting i have now this code:

function createTextbox() for i, data in ipairs(ref_buttonData.textbox) do --Sets up reference function local funcName = "checkbox"..i local func if i == 1 then func = function() click_checkboxE(i, buttonnumber) end else func = function() click_checkbox(i, buttonNumber) end end self.setVar(funcName, func) self.createInput({ input_function = funcName, function_owner = self, label = data.label, alignment = data.alignment, position = data.pos, scale = buttonScale, width = data.width, height = (data.font_size*data.rows)+24, font_size = data.font_size, color = buttonColor, font_color = buttonFontColor, value = data.value, }) spawnedButtonCount = spawnedButtonCount + 1 end end

but i does not really work.
Last edited by Kaija; Aug 26, 2022 @ 12:12pm
Finaryman Aug 26, 2022 @ 12:30pm 

disableSave = true
--Remember to set this to false once you are done making changes
--Then, after you save & apply it, save your game too

--Color information for button text (r,g,b, values of 0-1)
buttonFontColor = {0,0,0}
--Color information for button background
buttonColor = {1,1,1}
--Change scale of button (Avoid changing if possible)
buttonScale = {0.1,0.1,0.1}

--This is the button placement information
defaultButtonData = {
--Add checkboxes
checkbox = {
--[[
pos = the position (pasted from the helper tool)
size = height/width/font_size for checkbox
state = default starting value for checkbox (true=checked, false=not)
]]
{
pos = {1.12,0.1,-1.78},
size = 800,
state = true
},
--First checkbox
{
pos = {-0.977,0.1,-0.589},
size = 800,
state = false
},
--Second checkbox
{
pos = {-0.163,0.1,-0.594},
size = 800,
state = false
},
--Third checkbox
{
pos = {0.735,0.1,-0.592},
size = 800,
state = false
},
--End of checkboxes
},
--Add counters that have a + and - button
counter = {
--[[
pos = the position (pasted from the helper tool)
size = height/width/font_size for counter
value = default starting value for counter
hideBG = if background of counter is hidden (true=hidden, false=not)
]]
--First counter
{
pos = {-0.996,0.1,0.057},
size = 800,
value = 0,
hideBG = true
},
--Second counter
{
pos = {-0.151,0.1,0.043},
size = 800,
value = 0,
hideBG = false
},
--Third counter
{
pos = {0.736,0.1,0.051},
size = 800,
value = 0,
hideBG = true
},
--End of counters
},
--Add editable text boxes
textbox = {
--[[
pos = the position (pasted from the helper tool)
rows = how many lines of text you want for this box
width = how wide the text box is
font_size = size of text. This and "rows" effect overall height
label = what is shown when there is no text. "" = nothing
value = text entered into box. "" = nothing
alignment = Number to indicate how you want text aligned
(1=Automatic, 2=Left, 3=Center, 4=Right, 5=Justified)
]]
--First textbox
{
pos = {-0.552,0.1,1.213},
rows = 16,
width = 4000,
font_size = 300,
label = "Empty",
value = "",
alignment = 2
},
--Second textbox
{
pos = {0.572,0.1,0.768},
rows = 1,
width = 3000,
font_size = 600,
label = "Broke",
value = "",
alignment = 3
},
--Third textbox
{
pos = {0.596,0.1,1.393},
rows = 6,
width = 4000,
font_size = 400,
label = "Brickabrackless",
value = "",
alignment = 4
},
--End of textboxes
}
}

--Save function
function updateSave()
saved_data = JSON.encode(ref_buttonData)
if disableSave==true then saved_data="" end
self.script_state = saved_data
end

--Startup procedure
function onload(saved_data)
if disableSave==true then saved_data="" end
if saved_data ~= "" then
local loaded_data = JSON.decode(saved_data)
ref_buttonData = loaded_data
else
ref_buttonData = defaultButtonData
end

spawnedButtonCount = 0
createCheckbox()
createCounter()
createTextbox()
end



--Click functions for buttons



--Checks or unchecks the given box
function click_checkbox(tableIndex, buttonIndex)
if ref_buttonData.checkbox[1].state then
if ref_buttonData.checkbox[tableIndex].state == true then
ref_buttonData.checkbox[tableIndex].state = false
self.editButton({index=buttonIndex, label=""})
else
ref_buttonData.checkbox[tableIndex].state = true
self.editButton({index=buttonIndex, label=string.char(10008)})
end
updateSave()
end
end
--Predefined special checkbox (to lock other controls)
function click_checkboxE(tableIndex, buttonIndex)
if ref_buttonData.checkbox[tableIndex].state == true then
ref_buttonData.checkbox[tableIndex].state = false
self.editButton({index=buttonIndex, label=""})
else
ref_buttonData.checkbox[tableIndex].state = true
self.editButton({index=buttonIndex, label=string.char(10008)})
for i, text in ipairs(ref_buttonData.textbox) do
self.editInput({index=i-1,value=ref_buttonData.textbox[i].value})
end
end
updateSave()
end

--Applies value to given counter display
function click_counter(tableIndex, buttonIndex, amount)
if ref_buttonData.checkbox[1].state then
ref_buttonData.counter[tableIndex].value = ref_buttonData.counter[tableIndex].value + amount
self.editButton({index=buttonIndex, label=ref_buttonData.counter[tableIndex].value})
updateSave()
end
end

--Updates saved value for given text box
function click_textbox(i, value, selected)
if ref_buttonData.checkbox[1].state then
if selected == false then
ref_buttonData.textbox[i].value = value
updateSave()
end
else
if selected == false then
Wait.time(function() self.editInput({index=i-1,value=ref_buttonData.textbox[i].value}) end,0.1)
broadcastToAll("someone tried edit text field while not edible.")
end
end
end

--Dud function for if you have a background on a counter
function click_none() end



--Button creation



--Makes checkboxes
function createCheckbox()
for i, data in ipairs(ref_buttonData.checkbox) do
--Sets up reference function
local buttonNumber = spawnedButtonCount
local funcName = "checkbox"..i
local func
if i == 1 then
func = function() click_checkboxE(i, buttonnumber) end
else
func = function() click_checkbox(i, buttonNumber) end
end
self.setVar(funcName, func)
--Sets up label
local label = ""
if data.state==true then label=string.char(10008) end
--Creates button and counts it
self.createButton({
label=label, click_function=funcName, function_owner=self,
position=data.pos, height=data.size, width=data.size,
font_size=data.size, scale=buttonScale,
color=buttonColor, font_color=buttonFontColor
})
spawnedButtonCount = spawnedButtonCount + 1
end
end

--Makes counters
function createCounter()
for i, data in ipairs(ref_buttonData.counter) do
--Sets up display
local displayNumber = spawnedButtonCount
--Sets up label
local label = data.value
--Sets height/width for display
local size = data.size
if data.hideBG == true then size = 0 end
--Creates button and counts it
self.createButton({
label=label, click_function="click_none", function_owner=self,
position=data.pos, height=size, width=size,
font_size=data.size, scale=buttonScale,
color=buttonColor, font_color=buttonFontColor
})
spawnedButtonCount = spawnedButtonCount + 1

--Sets up add 1
local funcName = "counterAdd"..i
local func = function() click_counter(i, displayNumber, 1) end
self.setVar(funcName, func)
--Sets up label
local label = "+"
--Sets up position
local offsetDistance = (data.size/2 + data.size/4) * (buttonScale[1] * 0.002)
local pos = {data.pos[1] + offsetDistance, data.pos[2], data.pos[3]}
--Sets up size
local size = data.size / 2
--Creates button and counts it
self.createButton({
label=label, click_function=funcName, function_owner=self,
position=pos, height=size, width=size,
font_size=size, scale=buttonScale,
color=buttonColor, font_color=buttonFontColor
})
spawnedButtonCount = spawnedButtonCount + 1

--Sets up subtract 1
local funcName = "counterSub"..i
local func = function() click_counter(i, displayNumber, -1) end
self.setVar(funcName, func)
--Sets up label
local label = "-"
--Set up position
local pos = {data.pos[1] - offsetDistance, data.pos[2], data.pos[3]}
--Creates button and counts it
self.createButton({
label=label, click_function=funcName, function_owner=self,
position=pos, height=size, width=size,
font_size=size, scale=buttonScale,
color=buttonColor, font_color=buttonFontColor
})
spawnedButtonCount = spawnedButtonCount + 1
end
end

function createTextbox()
for i, data in ipairs(ref_buttonData.textbox) do
--Sets up reference function
local funcName = "textbox"..i
local func = function(_,_,val,sel) click_textbox(i,val,sel) end
self.setVar(funcName, func)

self.createInput({
input_function = funcName,
function_owner = self,
label = data.label,
alignment = data.alignment,
position = data.pos,
scale = buttonScale,
width = data.width,
height = (data.font_size*data.rows)+24,
font_size = data.font_size,
color = buttonColor,
font_color = buttonFontColor,
value = data.value,
})
end
end
Last edited by Finaryman; Aug 26, 2022 @ 12:46pm
Finaryman Aug 26, 2022 @ 12:30pm 
best i could do, it make it revert when checkbox is checked again to make editable
Kaija Aug 26, 2022 @ 12:33pm 
Originally posted by Finaryman:
best i could do, it make it revert when checkbox is checked again to make editable
mh is it not possible to give an error when its locked? and not letting you write?
Finaryman Aug 26, 2022 @ 12:47pm 
like that?
i edited last code post
Last edited by Finaryman; Aug 26, 2022 @ 12:47pm
Kaija Aug 26, 2022 @ 12:51pm 
Originally posted by Finaryman:
like that?
i edited last code post
If it now would revert the change you are trying to do, maybe after you click the checkbox again, then it would be perfect.
Finaryman Aug 26, 2022 @ 12:56pm 
i don't like these button types anyway because they don't allow controlling who clicked.

xml type is better, because u can control visibility and clickability seperatetly
Last edited by Finaryman; Aug 26, 2022 @ 12:57pm
Kaija Aug 26, 2022 @ 12:58pm 
Thank you so much thats exactly what i wanted!
Ermitaum Sep 25, 2022 @ 1:24pm 
This is too complex for my small monkey brain.
Finaryman Sep 25, 2022 @ 1:27pm 
just take from my first post (also last post)
Last edited by Finaryman; Sep 25, 2022 @ 1:43pm
< >
Showing 1-11 of 11 comments
Per page: 1530 50