0 votes

I could add buttons to TreeItem items with the add_button() method, but then how am I supposed to use those buttons?

Tree has a get_pressed_button() method but it returns the same number every time, if I press different buttons in different rows and columns.

Pic

public void UpdateQueueTree()
{
    queue.Clear();
    queue.Columns = 4;
    var r = queue.CreateItem();
    queue.SetColumnTitlesVisible(true);
    queue.SetColumnTitle(0, "Queue");
    queue.SetColumnMinWidth(0, 5);
    queue.SetColumnTitle(1, "N.");
    foreach (var item in c.Production.queue.items)
    {
        var p = queue.CreateItem(r);
        p.SetText(0, buildingName(item.id));
        p.SetExpandRight(0, true);
        p.SetText(1, item.quantity.ToString());
        p.SetTextAlign(1, (TreeItem.TextAlign)1);
        p.AddButton(2, Res.treeButtonPlus);
        p.AddButton(3, Res.treeButtonMinus);
    }
}

void QueueButtons(InputEvent @event)
{
    if (@event is InputEventMouseButton)
    {
        InputEventMouseButton ev = (InputEventMouseButton)@event;
        if (ev.ButtonIndex == (int)ButtonList.Left && ev.Pressed)
        {
            GD.Print("pressed: " + queue.GetPressedButton());
        }
    }
}
Godot version 3.2.3 mono
in Engine by (12 points)

1 Answer

0 votes

The button id on its own doesn't help much, you'll have to combine it with the item to find out which button has been pressed.

The button_pressed() signal of a Tree uses three arguments: item, column and id.

You assign each of your four elements (title, quantity, plus, minus) to its own column. This causes the two buttons to both have the id 0, because they are both the first item in their column.

I don't know what's happening inside of your GetPressedButton() function, so I don't know where your values come from. But it should work if you take the button_pressed()'s item and column values into account to know which button of which item has been pressed.

by (30 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.