Is there a boolean function to check if a certain textButton has been clicked?

Is there a boolean function in JUCE that returns true if any button has been clicked? I know there’s a void clicked function, but that’s one to override where you can write what happens in case you click a button, but I need one that returns true anytime I click a button.

Because I’m making my first GUI application, a tic tac toe game, and I made 9 buttons and put them into place on the grid but they don’t do anything now, so I want in the gameloop to basically have the equivalent of “waiting for input”/cin if it were a console application. So that my main function looks something activating the gameloop() function in the main.cpp constructor as well as setting the default values like gameover=false etc. then have

void gameloop()
{
    while (gameover == 0)
    {
        if (turn == X){
            if (/*any textButton has been pressed and the text in that button was "" (not X or 0 already) */ )
            //check for wins etc. and set gameover to 1 if there's a win or draw
            turn = 0
        }
        else{
        //same stuff as above
        }
    }//end gameover==0
}

How do I do this in JUCE?

You don’t, that’s not how GUI frameworks like JUCE work. The event loop has to keep running all the time, so you can’t have functions that block waiting for something to happen. Basically, there’s already the kind of semi-infinite loop in JUCE (or the operating system) itself that you wrote above, but you are not supposed to then make a similar blocking loop yourself in your own code.

You could maybe make your game loop run in another thread, but that would be a horrible solution for a problem that likely has a far simpler solution available. For example, you could use a JUCE timer and do your game state calculations and GUI repaints in the timer’s callback function.

Your container has a method, that will update the game state. Something along these lines:

class Game : public Component
{
public:
    Game()
    {
        for (int y=0; y < 3; ++y)
        {
            for (int x=0; x<3; ++x)
            {
                auto* button = buttons.add (new TextButton());
                addAndMakeVisible (button);
                button->onClick = [=]()
                {
                    setTicked (x, y, getPlayer());
                };
            }
        }
    }
    void setTicked (int x, int y, int player)
    {
        // set the box ticked and check if game is won
    }

    int getPlayer() const
    {
        // making this a method, so it doesn't get captured as value in the lambda, but is executed, when the button is clicked
        return player;
    }
private:
    OwnedArray<TextButton> buttons;
    int player = 0;
};

Bug in your example code, you capture x and y by reference into the lambda! :wink:

Ouch, thanks :slight_smile: I’ll edit