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
AI Thread Summary
The discussion revolves around modifying a file explorer script in Godot 4 to change the functionality of buttons created for each file in a directory. The user initially attempted to connect a button press to a test function that prints "test" but encountered issues with the connection not triggering the print statement. Various connection methods were tried, including binding and using the connect method with different parameters, but none were successful initially. The user noted that print statements in the open_folder function did not execute, while those in the _ready() function did. Ultimately, the user resolved the issue, realizing it stemmed from a misunderstanding of another part of the code, indicating that the problem was not with the button connection itself.
Darkmisc
Messages
222
Reaction score
31
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:

[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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top