How do I connect a button pressed signal with code in Godot 4?

  • Thread starter Darkmisc
  • Start date
  • Tags
    godot
  • #1
Darkmisc
213
28
TL;DR Summary
I've tried connecting a button pressed signal with nBut.pressed.connect(test.bind()), but it doesn't work.
Hi everyone

I've downloaded a file explorer for Godot 4. It creates a button for every file in the open directory. When you press the button, it fills a prompt with the path to the file.

This is the code for it:

open file:
nBut.pressed.connect(open_folder.bind(file_name))func open_folder(folder_name:String):
    if file:
        path = path.get_base_dir()
    path = path + "/" + folder_name
    set_layout()

I'd like to modify the code so that something else happens when you press the button.

I've tried using this code:

test:
nBut.pressed.connect(test.bind())

func test():
    print("test")

The code doesn't give me an error message, but it doesn't print "test" when I press the button. The existing code, however, will run file_open.

I've also tried nBut.pressed.connect("test", self)

nButpressed.connect(self, "test")

and

nBut.pressed.connect("pressed", self, "test")

but none of these work either.

Does anyone know the correct way to do this?

I can't connect the signal in the inspector because I'm not making the buttons manually. I need code to do it because I won't know how many files (and corresponding buttons) there will be in the directory.

Thanks
It won't print when I put print commands into open_folder, although it will print if I put print commands into _ready(). Not sure why that is.

EDIT:
open_folder:
func open_folder(folder_name:String):
    print("A")
    if file:
        print("B")
        path = path.get_base_dir()
    path = path + "/" + folder_name
    set_layout()
 
Last edited:
Technology news on Phys.org
  • #2
Nvm. It works. Problem was that I didn't understand another part of the code.
 

Related to How do I connect a button pressed signal with code in Godot 4?

1. How do I connect a button pressed signal in Godot 4?

To connect a button pressed signal in Godot 4, you can use the `connect` method in your script. For example, if you have a button named "button" and you want to connect its pressed signal to a function called "on_button_pressed", you can use the following code: `button.connect("pressed", self, "on_button_pressed")`.

2. Can I connect a button pressed signal to a function in a different script?

Yes, you can connect a button pressed signal to a function in a different script by passing the script instance as the second argument in the `connect` method. For example, if the function "on_button_pressed" is in a different script called "OtherScript", you can use the following code: `button.connect("pressed", OtherScript, "on_button_pressed")`.

3. How do I disconnect a button pressed signal in Godot 4?

To disconnect a button pressed signal in Godot 4, you can use the `disconnect` method. For example, if you want to disconnect the pressed signal from the function "on_button_pressed", you can use the following code: `button.disconnect("pressed", self, "on_button_pressed")`.

4. Can I connect multiple functions to a button pressed signal?

Yes, you can connect multiple functions to a button pressed signal in Godot 4. Simply call the `connect` method multiple times with different function names. For example, you can connect two functions "on_button_pressed" and "on_button_clicked" to the button pressed signal using the following code: `button.connect("pressed", self, "on_button_pressed")` and `button.connect("pressed", self, "on_button_clicked")`.

5. Is there a way to connect a button pressed signal using the Godot editor?

Yes, you can connect a button pressed signal using the Godot editor by selecting the button node in the scene tree, going to the "Node" tab, and clicking on the "Node" button. Then, select the "Signals" tab, find the "pressed" signal, and click on the "Connect" button to create a new function in your script that will be called when the button is pressed.

Similar threads

  • Programming and Computer Science
Replies
4
Views
759
  • Programming and Computer Science
Replies
1
Views
941
  • Programming and Computer Science
Replies
0
Views
1K
  • Programming and Computer Science
Replies
14
Views
3K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
1
Views
662
  • Programming and Computer Science
Replies
15
Views
2K
Replies
10
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
12
Views
9K
Back
Top