(TGUI 1.0) Button single click

Started by Mikael, 27 November 2022, 17:22:51

Mikael

Hello, completely new here.

I couldn't find help in the official documents, tutorials nor examples about how to detect just a single mouse click/release on a button.

Button->OnClick() - Function executes multiple times when it's called?? Or is there another way to detect if the button was clicked? And it's not because of the for-loop. Even without it the functions get called multiple times.

 This is my code I'm trying to get to work:

void useItem(const Player::ptr& player, const Item::ptr& item)
{
if (!player)
return;

if (!item)
return;

item->use(player.get());
}

void HUD::update()
{
// Check if inventory slot was clicked
for (int i = 0; i < NUM_INVENTORY_SLOTS; ++i)
{
m_inventory[i]->onClick(useItem, m_player, m_player->getItemByIndex(i));
}

}

Mikael

I got it to work. I searched some more and realised you need to add the Button->onClick() when you are actually creating the button.

texus

#2
You don't actually need to do it when creating it, you can do it at any time.
If the callback gets called multiple times then you are probably calling onClick on the same button multiple times though (e.g. if the update() function is called more than once).
You can run button->onClick.disconnectAll() before connecting the callback function to remove all previously connected callbacks.