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

  • Thread starter Thread starter Darkmisc
  • Start date Start date
  • Tags Tags
    godot
Click For Summary
SUMMARY

The discussion focuses on connecting button pressed signals in Godot 4 using GDScript. The user initially attempted to bind a test function to a button's pressed signal but encountered issues with the function not executing. After several unsuccessful attempts, the user discovered that the problem stemmed from a misunderstanding of another part of the code. The final solution involved correctly using the signal connection methods provided by Godot 4.

PREREQUISITES
  • Familiarity with GDScript in Godot 4
  • Understanding of signal and slot mechanisms in Godot
  • Basic knowledge of file handling in Godot
  • Experience with dynamic UI element creation in Godot
NEXT STEPS
  • Explore Godot 4 signal connections and binding methods
  • Learn about dynamic UI creation in Godot 4
  • Investigate debugging techniques in GDScript
  • Study file handling and directory management in Godot 4
USEFUL FOR

Game developers using Godot 4, particularly those working with dynamic user interfaces and signal management in GDScript.

Darkmisc
Messages
222
Reaction score
31
TL;DR
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:

[CODE lang="python" title="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()
[/CODE]

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

I've tried using this code:

[CODE lang="python" title="test"]nBut.pressed.connect(test.bind())

func test():
print("test")[/CODE]

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:
[CODE lang="python" title="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()
[/CODE]
 
Last edited:
Technology news on Phys.org
Nvm. It works. Problem was that I didn't understand another part of the code.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 0 ·
Replies
0
Views
3K
  • · Replies 14 ·
Replies
14
Views
6K
Replies
5
Views
2K
Replies
7
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 15 ·
Replies
15
Views
3K
Replies
10
Views
2K
Replies
1
Views
2K