0 votes

I am going through the buttons using a for each loop, and they should attach a signal on build.

I have some buttons that should be clickable on build, but it doesn't seem to be working. I get no errors, the buttons don't respond at all.

Anything I'm missing? Maybe it's how I write the array? (The "MenuButton" is just how I get access to the "scenetoload" String variable, it should be of the Button type.

  foreach (MenuButton button in GetNode("Menu/CenterRow/Buttons").GetChildren())
     {

    button.Connect("pressed", this, nameof(OnButtonPressed), new Godot.Collections.Array() { button.scene_to_load });

Where I'm getting the "MenuButton" from to access the scenetoload variable.

public class MenuButton : Button
{
    [Export]
    public String scene_to_load;
    
   


}

Then it should excute this method (The scenetoload variable should load & open the corresponding scene the buttons are linked to, but don't seem to respond.)

 void OnButtonPressed(String scene_to_load)
  {
    GetTree().ChangeScene(scene_to_load);
    //scene_path_to_load = scene_to_load;
  }   

Thank you very much for your help! Addtional info, can be provided if needed, thanks.

in Engine by (53 points)
edited by

1 Answer

+1 vote
Best answer

Was just incorrect C#

I was writing the line

  button.Connect("pressed", this, nameof(OnButtonPressed), new Godot.Collections.Array() { button.scene_to_load });

It should be more done like (May have to change how it gets the "NodePath"
on the "GetNode" bit & array)

 GetNode("Menu/CenterRow/Buttons").Connect("pressed", this, nameof(OnButtonPressed), new Godot.Collections.Array() { button.scene_to_load });

I was writing it similarly on how you would do it in GDScript, just had to read the Signals documentation again! My mistake :) - https://docs.godotengine.org/en/3.1/getting_started/step_by_step/signals.html#connecting-signals-in-code

by (53 points)
edited by
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.