Hi, new to Godot here and first time asking a question, and it's a bit complicated....
I am attempting to connect a signal using code, from a child to its parent node, and have the signal pass an argument. The reason I want to use code and not the Godot GUI is that I am iterating a number of these children (game pieces) and the child scene is not in the tree before runtime (I don't know of any reason why it should be?).
The real issue isn't the iterating of the children, because I am totally successful connecting signals in code from my instanced children to the parent. The problem is that emit_signal(), which is working for those children, does nothing once I add an argument in that line, using the correct format. I don't get an error, but the signal does not emit.
When I set everything up in the child and parent to pass the argument, and simply don't add an argument to emit_signal(), there's no error: it emits a signal. In the parent, it seems to pass a value from the signal connection to the function, but the value that's passed to the function is Null.
But if I add the argument to emit_signal(), it does not emit a signal at all.
I created a fresh test tree for this issue and was able to recreate it, even with the child scene being in the tree from the beginning, confirming the issue occurs even in that case.
But this way I could use the GUI to connect the signal. As soon as I did that, the entire process worked correctly and the argument was passed with the signal. So the issue was not whether the child scene was in the tree, but just that it gets connected through the GUI.
After using the GUI, the connection is not written in the script. It calls the automatic function that the GUI creates, but I'm pretty sure I'm using the correct format when I code my own connection and function in the parent node: it's taken from reading every discussion I could find online, and I confirmed it not only by trying just about every small variation on the format, but by the fact that only in this format does the connected function even get called.
Any ideas on how I can get this working right? I know I could come up with a workaround but I would like to get the signal working fully. Thanks!
The full code of "childnode":
extends Node2D
var helens = 30
signal chosen
# This also works with chosen()
# And writing chosen(helens) will autofill helens as a variable
# in the signal dock, but doesn't affect this
func _ready():
pass
func _unhandled_key_input(event):
if Input.is_key_pressed(16777221):
print("pressed ENTER")
emit_signal("chosen", helens)
# This will not emit a signal with the argument here.
# It also doesn't work in other formats
# such as emit_signal("chosen", 30)
# or different argument types
# It only emits as emit_signal("chosen")
The full code of the parent node:
extends Node2D
func _ready():
var helens
$childnode.connect("chosen", self, "_test_signal", [helens])
pass
func _test_signal(helens):
print("test function called from ", helens)
So, if I remove the 'helens' argument from the emit_signal line, this prints "test function called from Null" .
If I retain the helens argument in emit_signal, it prints nothing.
If I use the GUI to connect the signal to the parent node, I can have 'helens' in the emit_signal line. I just have to add it to the parameters of the function that the GUI created in the parent node. The connection and function I coded manually still work, but aren't triggered if the argument is in the emit line.
Once it's connected by GUI, the parent node looks like this:
extends Node2D
func _ready():
var helens
$childnode.connect("chosen", self, "_test_signal", [helens])
pass
func _test_signal(helens):
print("test function called from ", helens)
func _on_childnode_chosen(helens):
print("GUI function called from ", helens)
This will print "GUI function called from 30"