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

  • Thread starter Darkmisc
  • Start date
  • Tags
    godot
  • #1
Darkmisc
204
27
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.
 

1. How do I connect a button's pressed signal to a function in Godot 4 using the editor?

In Godot 4, you can connect a button's pressed signal to a function directly from the editor. First, select the button node in the scene tree. Then, go to the "Node" tab beside the inspector on the right side of the editor. Here, you'll see a list of signals associated with the button. Find the "pressed" signal, and double-click it or click the "Connect..." button. This opens a dialog where you can specify the target node and the method to be called when the signal is emitted. You can either select an existing function or enter a new function name, and Godot will automatically create the function in the selected node's script.

2. How do I connect a button's pressed signal to a function in Godot 4 using GDScript?

To connect a button's pressed signal to a function using GDScript in Godot 4, first ensure your button node has a script attached or create a new one. In the script, you can use the `connect` method to bind the signal to a function. For example:

func _ready():    var button = $Button    button.connect("pressed", self, "_on_Button_pressed")
Here, `_on_Button_pressed` is the function that will be called when the button is pressed. Make sure to define this function in your script:
func _on_Button_pressed():    print("Button was pressed!")

3. What changes have been made to signal connection in Godot 4 compared to Godot 3?

In Godot 4, the process of connecting signals remains largely similar to Godot 3. However, there are improvements and refinements in the editor's UI/UX, making it more intuitive and user-friendly. The method names and the API structure are mostly unchanged, but Godot 4 includes more robust error checking and clearer feedback in the editor when connecting signals, which helps prevent common mistakes like connecting to a non-existent function or wrong type of arguments.

4. Can I connect a signal to a function in another script or node?

Yes, in Godot 4, you can connect a signal from one node to a function in a different node or script. When setting up the connection, either through GDScript or the editor, you need to specify the target node where the function is defined. For example, if you have a separate node with a script for handling UI actions, you can connect a button's pressed signal to a function in that script by specifying the path to the target node and the function name during the connection setup.

5. How can I disconnect a signal in Godot 4?

To disconnect a signal in Godot 4, you use the `disconnect` method. You need to specify the signal name, the target object, and the method from which the signal should be disconnected. For example:

$Button.disconnect("pressed", self, "_on_Button_pressed")
This code will remove the connection of the "pressed" signal from the button to the `_on_Button_pressed` function in the current script. It is important to ensure that the signal was previously connected with the exact same parameters.

Similar threads

  • Programming and Computer Science
Replies
4
Views
387
  • Programming and Computer Science
Replies
1
Views
544
  • Programming and Computer Science
Replies
0
Views
771
  • Programming and Computer Science
Replies
14
Views
3K
  • Programming and Computer Science
Replies
5
Views
994
  • Programming and Computer Science
Replies
1
Views
269
  • Programming and Computer Science
Replies
15
Views
1K
Replies
10
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
12
Views
9K
Back
Top