#Creating counter buttons on cards
1 messages ยท Page 1 of 1 (latest)
Your button is probably inside the card because the Y position is 0.2, it's a relative position against the card's scale/size, to make it visible above the card, it should be slightly bigger than 0.5 the thickness of the card, i.e. the y position needs to be more than 0.5.
Got it
Creating counter buttons on cards
so that will change the position, but not the thickness, correct?
That worked, I can see the text now
Yea, the text is the same now on both the full and blank card
You don't need three buttons for this might want to remove the inc/dec buttons to reduce the clutter on the card (the more buttons on the card, the more difficult for players to actually select/grab the card itself and not accidentally click those buttons). Just use left click to increase, right click to decrease.
When you're happy with the position of the buttons, make them transparent by setting the color and font_color properly. Take a look at the Clickable Label variation in this post:
#scripting message
the function that is called when you click the button (in button#1's case--"inc"), three parameters are automatically sent:
- a reference to the object itself,
- the color of the player that clicked the button,
- whether or not a non-left-click button was the clicking button (true if not left-click, false if left-click was used)
so you can use parameter 3 here to determine what to do for left vs right click
i always use this general code for each of my buttons:
function buttonClickedFunc(button, color, altClick)
local hover_object = Player[color].getHoverObject()
if hover_object ~= button then return end --this prevents any "ghost" clicks
local deltaNum = altClick and -1 or 1
--my function
end
Let me give it a try
I can't get it to work, I think the code is being applied to the card itself and not the button text
This is the current code after all the changes
function onLoad()
self.createButton({click_function="dummy",
function_owner=self,
width=0,
height=0,
font_size=200,
label="5",
font_color={1,1,1,100},
position=Vector(-0.8,0.6,-1.2)})
myvalue=5 --start value
end
function inc()
--u do here (like in this example)
myvalue=myvalue+1 --this button increases value
self.editButton({index=2 --[[this depends on how manieth is button showing value, like explanation says count starts from zero.]],
label=myvalue..""})
end
function dec()
--u do here (like in this example)
myvalue=myvalue-1 --this button increases value
self.editButton({index=2 --[[this depends on how manieth is button showing value, like explanation says count starts from zero.]],
label=myvalue..""})
end
function buttonClickedFunc(button, color, altClick)
local hover_object = Player[color].getHoverObject()
if hover_object ~= button then return end --this prevents any "ghost" clicks
local deltaNum = altClick and -1 or 1
--my function
end
ah, ok. so your button is looking to call a function called "dummy". so i would either change "dummy" to "buttonClickedFunc", or rename the last function to be called "dummy"
then, youll also need to add this to the buttonClickedFunc:
myvalue = myvalue + deltaNum
button.editButton({
label = myvalue
})
Same as before, it's still not working on the button text
ok, lets get rid of those first 2 lines of code in that last function (buttonClickedFunc)
so now, you should only have
local deltaNum = altClick and -1 or 1
myvalue = myvalue + deltaNum
button.editButton({
label = myvalue
})
ok, you have the width and height set to 0, correct? what that does is it means the button itself (the clickable feature) doesn't exist
so you want to increase those
but you can make the button itself "invisible" by simply changing its color
It works perfectly now
yay!
As for the second button, I'm assuming it's going to take more than just duplicating the code?
lets go ahead and add those other 2 lines of code that we removed and see if it still works
actually, not much more actually
That's great to hear, and the button still works even with the two lines of code added back in
perfect.
ok. now, we create a second button. you can literally just copy paste the first createbutton code, just change the label to what you want the value to be
and at this point, lets get rid of the myvalue variable
lets make the code more generalizable
oh, also, you'll need to change the position too
I copy pasted the code but it just moved the first button, it didn't create a second one
well now that doesn't make sense
let me investigate
worked for me. this is what i have:
function onLoad()
self.createButton({click_function="buttonClickFunc",
function_owner=self,
width=300,
height=300,
font_size=200,
label="5",
font_color={1,1,1,100},
color={0,0,0,0},
position=Vector(-0.8,1,-1.2)})
self.createButton({click_function="buttonClickFunc",
function_owner=self,
width=300,
height=300,
font_size=200,
label="5",
font_color={1,1,1,100},
color={0,0,0,0},
position=Vector(0.8,1,-1.2)})
end
oh, maybe you replicated the function onLoad() too?
I think so, let me try to adjust the code again
I was able to create the second button but now I'm getting an error
It's saying it's trying to perform arithmetic on a nil value
yeah. thats because we got rid of myvalue and didn't put anything in its place.
there are ways around this, but it becomes quite hairy.
we'll need to add it back in.
I thought so, but when I tried adding it back in I kept running into more errors
acutally, hang on
alrighty, lets just make this simple.
we'll use two functions isntead of 1. we'll need 2 variables then. so like myvalue1 and myvalue2. or button1 and button2, something
right now, you have buttonClickFunc; so we need to duplicate that function as well, and call them differente names: buttonClick1 and buttonClick 2. up to you.
Done, what's next?
well, here. i'll show you what i ended up with:
function onLoad()
self.createButton({click_function="buttonClick1",
function_owner=self,
width=300,
height=300,
font_size=200,
label=5,
font_color={1,1,1,100},
color={0,0,0,0},
position=Vector(-0.8,1,-1.2)})
self.createButton({click_function="buttonClick2",
function_owner=self,
width=300,
height=300,
font_size=200,
label=5,
font_color={1,1,1,100},
color={0,0,0,0},
position=Vector(0.8,1,-1.2)})
end
function buttonClick1(card, plyrColor, altClick)
local hover_object = Player[plyrColor].getHoverObject()
if hover_object ~= card then return end
local deltaNum = altClick and -1 or 1
local buttonVal = card.getButtons()[1].label
buttonVal = buttonVal + deltaNum
card.editButton({
index = 0,
label = buttonVal
})
end
function buttonClick2(card, plyrColor, altClick)
local hover_object = Player[plyrColor].getHoverObject()
if hover_object ~= card then return end
local deltaNum = altClick and -1 or 1
local buttonVal = card.getButtons()[2].label
buttonVal = buttonVal + deltaNum
card.editButton({
index = 1,
label = buttonVal
})
end
sorry. made some changes
i haven't been testing this though; it might now work fully; one sec...
yeah. thats what i thought... lol
one moment; have to change it again
Go ahead, you've been amazing help so far
ok. there we go
Same as before, arithmetic on a nil value
ok. looking again...
ok, changed it again. this time it IS tested, and it worked for me
It works!
yay! lol
had to add the index of the button so the function knew which button we were talking about
those 2 buttonClick functions are VERY much the same. the only difference is the index of the button used
if the index of the button was passed as a parameter into the function, we could just trim this down to 1 function
but it is not passed as a parameter (at least not automatically), and you can't just simply add it; it won't accept it.
it gets messy/hairy but there IS a way to do it, but it might be best to sort that out later, and only if you want
Only if it ever becomes an issue, the current code works perfectly for me
yeah, exactly.
now, you would just add this code onto every card
yay!
but there is a way to make that easy if you want
If there is then I'm interested
do you have all cards out on a table? or in a deck? what's their status
I just have them laid out one by one on a table
Sure
finding some oddities here... but can work around them...
could you please do me a favor?
in your Global.-1.ttslua file, could you please add this to your onLoad function?
What do you need?
function onLoad()
for _, obj in ipairs(getObjects()) do
if obj.type == "Card" then
print(logString(obj.getData()))
end
end
end
and then run it and show me the output?
are you using atom?
I am not
are you using any code/text editor or just the ingame stuff?
Just ingame stuff
Sure, is it just going to be in the tabletop files?
yeah, it should be under (on a windows PC) "Documents\My Games\Tabletop Simulator\Saves"
find the file that shows the name of your game/mod.json
looks right!
ok
checking something...
lol! i was making it harder than it needs to be
here, try this in your global file:
function onLoad()
local refCard = getObjectFromGUID("a3099f")
local script = refCard.getLuaScript()
for _, obj in ipairs(getObjects()) do
if obj.type == "Card" then
obj.setLuaScript(script)
end
end
end
This works too!
yay!!
now make sure to save your mod as is, but then delete this onLoad code
are you going to be adding more cards later though?
Yes
ok. well, you can run this same script (in the global onLoad) each time you have new cards that need the script. or you can copy/paste the code from any card's script onto the script for each card added. up to you. but once you save your mod with each card's script updated, remove or comment out this code so it isn't running every time you run the mod (that isn't necessary once each card is saved with its own script)
keep in mind that running that code will overwrite any changes you've made to any individual cards (like their blue or orange numbers)
this code also relies on the reference card existing in game, with the GUID of "a3099f"
Got it, I'll keep all that in mind
awesome! good luck modding!
Thanks for all your help!