Tabletop Simulator

Tabletop Simulator

View Stats:
Oo Aug 29, 2021 @ 5:07am
Access player that clicked a button?
Hello together, :)

i have a problem to access the player that clicked a button.
The plan is to put a button to hide/show handcards of a player to all by clicking the button in players zone. Has somebody an idea how to do it? I already got problems if I try to access player that clicked the button.

Has somebody an idea how to do it?

Button creation in XML:
<button onClick="hideOrShowHandCards(objectButtonClicked, playerColorClicked)"
position="0 0 -30"
width="600"
height="200"
fontSize="72">
Hide/Show
</button>

Code for the method:
function hideOrShowHandCards(playerColorClicked, objectButtonClicked)
print(playerColorClicked)
print('Not Hid2')
print(Turns.getNextTurnColor())
print(playerColorClicked.held_by_color)
end

If i print Turns.turn_color nothing in case of an empty line is printed out.
This: print(playerColorClicked.held_by_color) gives the following error:
cannot access field userdata<LuaPlayer>
Last edited by Oo; Aug 29, 2021 @ 5:09am
Originally posted by Corvin:
I don't think you can use parameters when starting a function from a xml button. But the default parameters for XML elements are already (player, option, id) - so I don't think i can follow you on what's the problem?

XML:
<Button
onClick = "hideOrShowHand"
id = "Hide Show Control"
width = "100"
height = "50"
text = "Hide/Showl"
position = "0 0 - 30"
rotation = "180 0 0"
/>

LUA:
function hideOrShowHand (player, option, id)
print(player.color)
print('Not Hid2')
print(Turns.getNextTurnColor())
print(player.getHandObjects())
end

But I have no idea what "held_by_color" should do? It's not in the API...Maybe you thought of https://api.tabletopsimulator.com/player/instance/#gethandobjects ?

PS; I tinkered around with hand visibilities for specific players, and that seems quite an annoying thing to do...so, good luck.
< >
Showing 1-9 of 9 comments
The author of this thread has indicated that this post answers the original topic.
Corvin Aug 29, 2021 @ 10:32am 
I don't think you can use parameters when starting a function from a xml button. But the default parameters for XML elements are already (player, option, id) - so I don't think i can follow you on what's the problem?

XML:
<Button
onClick = "hideOrShowHand"
id = "Hide Show Control"
width = "100"
height = "50"
text = "Hide/Showl"
position = "0 0 - 30"
rotation = "180 0 0"
/>

LUA:
function hideOrShowHand (player, option, id)
print(player.color)
print('Not Hid2')
print(Turns.getNextTurnColor())
print(player.getHandObjects())
end

But I have no idea what "held_by_color" should do? It's not in the API...Maybe you thought of https://api.tabletopsimulator.com/player/instance/#gethandobjects ?

PS; I tinkered around with hand visibilities for specific players, and that seems quite an annoying thing to do...so, good luck.
Last edited by Corvin; Aug 29, 2021 @ 10:44am
Oo Sep 2, 2021 @ 12:17pm 
Thank you very much Corvin. I am now able to Show/hide cards in Hand by clicking on the button. You helped me a lot!

1.But it seams to be not possible to assign a button to a player and if that button is clicked in another players turn the cards cannot be shown or hide. Does somebody have an idea?

2. How I can access a button that is created in lua script? With self.createButton(...)? I tried to get the GUIID somehow but it doesn't work. Any idea?

Greetings
Corvin Sep 2, 2021 @ 12:23pm 
1) You could check the button event for who started it...so something like an inbound if clause in the button function like if player == player.White then ... end

2) With the GUID of the object its attached on
Oo Sep 3, 2021 @ 12:13am 
Thx again Corvin. I think I have to try a little bit for the first issue. Sounds like a good starting point.

For the second issue: I know that I can access the guild if the button is clicked and the attached function is runned because the object is an parameter of the function. But if I create the button over the method

self.createButton(….)
button2=getObjectByGuiid()

I want to access the previously created button directly after its creation in the Lua script, without clicking it. (did not use the xml or gui to create the button)

So I think I need it’s GUIID to get the object with getObjectByGuiId() method directly after its creation in the code. The createButton method returns just a boolean and not the object itself. Any idea to solve that issue? Is there another way to do that?

Corvin Sep 3, 2021 @ 12:48am 
I think editing the button afterwards should work with editButton():
https://api.tabletopsimulator.com/object/#editbutton

But you would still need the GUID of the object the button is attached to (the "self" in the creation).

So, say you have a card/token with an attached button. The inbound creation method of the card would read:

self.createButton({label="Original Label"})

Later on, you want to edit the label, triggered by from somewhere else. Then it would read:

getObjectFromGUID("CardGUID").editButton({index=0, label="New Label"})

You just have to check what index that specific button has (0 is always the first one you created, then 1, 2 and so on).
Oo Sep 3, 2021 @ 3:42am 
Ok, is there a way to check the current index/guiid or the number of custom created objects before the creation of a new object? So that you know which index will be assigned to the next created button? In my case an undefined number of objects were already created before.

The guiid for the first created object is therefore “1“?
Last edited by Oo; Sep 3, 2021 @ 3:47am
Corvin Sep 3, 2021 @ 4:05am 
The GUID for the first created object is always 0, wich is quite weird, because in lua - opposed to nearly every other programming language on this planet - tables usually start at 1.

Concerning how much you already have: The API is your friend ;) https://api.tabletopsimulator.com/object/#getbuttons

So
tableOfAllButtons = getObjectFromGUID("CardGUID").getButtons() -- the Buttons
will give you a table containing all buttons of that object. And with "#" in front of that it will give you the length of the table - aka the number of your buttons.
numberOfButtons = #getObjectFromGUID("CardGUID").getButtons() -- Number of Buttons

http://lua-users.org/wiki/TablesTutorial
You can get the "length" of an array using the # operator: myTable = {"a", "b", "c"} myTableLength = #myTable print(myTableLength) -- will be 3
Last edited by Corvin; Sep 3, 2021 @ 4:08am
Oo Sep 4, 2021 @ 1:32pm 
Thx again, i searched very long time iside the API. But it is not clear for me how to use some of the methods and classes to realise my plans in code.

So maybe i was to inprecise: I tried to creat buttons in the Global file for every player in the near of their handzones within the LUA. But it seems to me that i cannot create buttons close to the handzones in the global. So i adapted my approch to add the buttons with each of the handzones lua files. I think there is no other way to read out all handzones of seated player to add them within the global file, right?

1.Now I want to make a button that only shows an icon. Therefore I loaded up an asset as "eye.png". The icon is not displayed! Any idea why? My plan is to change the icon afterwards within the executed method or somehow like a toggle button. Do you have an idea or somebody else?

This is the code:

function onload()
-- Parameters that create the button
hideButton = {
click_function = 'hideOrShowHand',
function_owner = self,
position = {x=-0.7, y=0.25, z=1.1},
width = 100,
height = 50,
icon = "eye",
iconwidth = 100,
iconAlignment = "Left",
font_size = 50,
tooltip = "Add wound",
color = {r=1, g=0, b=0}
}
self.createButton(hideButton) -- create add wound button

end

2. Do you have any idea how to lock the view to a top down perspectiv? i was able to set the view as top down but it is not possible to lock that in context of position. i want to enable the zooming but therefore i need a way to read out the camera distance. Just can set it by lookat()-Method. So that it cannot be manipulated by other players in context of movement xy axis but enable zooming? Or simply lock the camera perspective to a fixed camera position for all hot seat players.

Thanks again for you help! :) I wish a nice weekend to you.
Last edited by Oo; Sep 5, 2021 @ 9:23am
Corvin Sep 5, 2021 @ 11:16pm 
I'll need to play this through in detail later that week (if it's not solved by anynoe else - @Baryonyx @mannyano, looking your way ;) )

Regarding "reading the handzones for seated players": sounds about right for me.

I think 1) breaks down to the icon name - it should not be "eye" but the full path of the uploaded decal (you created a dical, right? Cause you need to, for it to work as a button icon)

And 2) that's tricky. There was a discussion some time ago from a wargamer that wanted to lock other players view on their baords (some Battelships style behaviour). The discussion edned with "get thrustworthy friends that don't peek where they are not supposed to ;)
< >
Showing 1-9 of 9 comments
Per page: 1530 50