How to connect signals from another node? #72
-
Currently, the pop up is being called so I can select a file, but the method itself is not called, I tried to use a default python method and it asked me to pass an @gdclass
class owo(Node2D):
def _ready(self) -> None:
file_dialog: FileDialog = FileDialog.cast(self.get_node(NodePath.new2("FileDialog")))
file_dialog.connect(StringName.new2("_on_file_dialog_file_selected"), Test())
print("Calling the select file thing OwO")
file_dialog.set_visible(True)
print("Done UwU")
file_dialog.set_visible(False)
def _process(self, delta:float) -> None:
pass
class Test(Callable):
def _on_file_dialog_file_selected(self, path: String): # Not being called
print(f"Path: {path}") |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
Hi, file_dialog.connect(StringName.new2("file_selected"), Callable.new2(self, StringName.new2("_on_file_dialog_file_selected"))) I don't know your specific use case. But there is a bit of a simpler way for add. I added the signals as properties of the classes and you can connect them directly (keep in mind, the method you want to connect to has to be a gdclass): file_dialog.file_selected.connect(self._on_file_dialog_file_selected) here is my full example code: from py4godot.methods import private
from py4godot.signals import signal, SignalArg
from py4godot.classes import gdclass
from py4godot.classes.core import Vector3, Callable, String, StringName, NodePath
from py4godot.classes.Node2D.Node2D import Node2D
from py4godot.classes.FileDialog.FileDialog import FileDialog
@gdclass
class owo(Node2D):
def _ready(self) -> None:
file_dialog: FileDialog = FileDialog.cast(self.get_node(NodePath.new2("FileDialog")))
# Method 1
#file_dialog.connect(StringName.new2("file_selected"), Callable.new2(self, StringName.new2("_on_file_dialog_file_selected")))
# Method 2
# Please keep in mind, that the method must be a method of a gdclass
file_dialog.file_selected.connect(self._on_file_dialog_file_selected)
print("Calling the select file thing OwO")
file_dialog.set_visible(True)
print("Done UwU")
file_dialog.set_visible(False)
def _process(self, delta:float) -> None:
pass
def _on_file_dialog_file_selected(self, path: String): # Not being called
print(f"Path: {path}") By the way. I couldn't find |
Beta Was this translation helpful? Give feedback.
-
Thank you for the answer, also could you reply why I get an error when I try to make the variable file_dialog declared in the class scope? @gdclass
class owo(Node2D):
file_dialog: FileDialog
def _ready(self) -> None:
pass Error:
It looks like its an issue of how im declaring it 🤔 |
Beta Was this translation helpful? Give feedback.
-
I'm sorry, I made the api a bit confusing. my_file_dialog = FileDialog.constructor() Should do the trick. I hope this helps |
Beta Was this translation helpful? Give feedback.
Hi,
I'm sorry to hear that.
I quickly went over it. It seems, for callable to be valid, you kind of have to use the methods new1 or new2 for construction. So you have to point to a method of a gdclass:
I don't know your specific use case. But there is a bit of a simpler way for add. I added the signals as properties of the classes and you can connect them directly (keep in mind, the method you want to connect to has to be a gdclass):
here is my full example code: