Creating dynamic buttons in a container linked to one callable

Godot Version

4.2.2

Question

I’m trying to create dynamic buttons after a option button selection is made. I can create the buttons in the container but they currently do nothing.

I’m struggling with the code to actually get the buttons to be functional.

Here is the code that is part of a loop.

// Add button to FlowContainer
var button = new Button();
button.Text = entry.Key.ToString();
button.Name = entry.Key.ToString(); // Set the name to identify the button later
_flowContainer.AddChild(button);	

var callable = new Callable(this, nameof(OnButtonPressed));
button.Connect("button_down", this, callable, new Godot.Collections.Array { button.Name });

I wonder if I’m barking up the wrong tree here. Found myself going around in circles on this.

Any help would be appreciated.

I’ve been seeing a lof of “new Callable” stuff, where was this recommended to you?

The proper way to connect in 4.x is using the signal directly, I recommend pressed instead of button_down

var button = new Button();
button.Text = entry.Key.ToString();
button.Name = entry.Key.ToString(); // Set the name to identify the button later
_flowContainer.AddChild(button);	

button.Connect(Button.SignalName.Pressed, Callable.From(OnButtonPressed));
1 Like

Hi Gertkeno,

Thanks for your reply. I think the main culprit is me relying in chatgpt. I tend to search around on different sources to find an example that is similar to what I want. I then adapt the code with trial and error. However, if I get stuck I use chatgpt to help.

My struggle has been once I created the buttons, how to adapt OnButtonPressed’ to know which dynamic button had been pressed.

I’ve definitely come to the conclusion that chatgpt doesn’t work so well with c# and godot combined.

Do you have a suggestion how I can achieve this once the buttons are created?

Chat GPT don’t know almost nothing about Godot 4, he isn’t good in generating new code, some times he can help optimize your code with good prompt question. Most cases if CHAT GPT don’t know something he will just generate gibberish who look like answer

first place where you should look is docs.godotengine.org/

Button — Godot Engine (stable) documentation in English

public override void _Ready()
{
    var button = new Button();
    button.Text = "Click me";
    button.Pressed += ButtonPressed;
    AddChild(button);
}

private void ButtonPressed()
{
    GD.Print("Hello world!");
}
1 Like

I agree, it’s not very good with Godot. It’s great for work but then I know the subject I’m asking and mainly for things I’ve forgotten how to do.

I can create the buttons, but reason I went to chatgpt is because I don’t know how to write the code to pass ‘ButtonPressed’ which button is doing the pressing.
Depending on which button was pressed I want to do different actions.

Seems they recommend lambdas instead of binds for C#, and the += operator to connect to events.

public int Value { get; private set; } = 1;

public override void _Ready()
{
    Button plusButton = GetNode<Button>("PlusButton");
    plusButton.Pressed += () => ModifyValue(1);

    Button minusButton = GetNode<Button>("MinusButton");
    minusButton.Pressed += () => ModifyValue(-1);
}

private void ModifyValue(int modifier)
{
    Value += modifier;
}

I just fell onto that section a few mins ago and was just working on the solution.

For completeness i did:


var button = new Button();
button.Text = entry.Key.ToString();
button.Name = entry.Key.ToString();

button.Pressed += () => OnButtonPressed(button);
_flowContainer.AddChild(button);	


private void OnButtonPressed(Button button)
{
	GD.Print("Button pressed: " + button.Name);
}

Thank you gertkeno,Moreus.

1 Like

Its can make problem to unsubscribe lambda, even worse when you have custom signals who needed be unsubscribed manually more often.
You needed keep reference to your lambdas,

I think is just easier make two inline methods

public int Value { get; private set; } = 1;

public override void _Ready()
{
    Button plusButton = GetNode<Button>("PlusButton");
    plusButton.Pressed += ModifyValueAdd;

    Button minusButton = GetNode<Button>("MinusButton");
    minusButton.Pressed += ModifyValueSubstract;
}
private void ModifyValueAdd() => ModifyValue(1);
private void ModifyValueSubstract() => ModifyValue(-1);
private void ModifyValue(int modifier) => Value += modifier;

In that case yes, two functions works for plus one and minus one, but the original question is an unknown value and quantity of buttons.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.