How do you use a signal parameter in the called function?

Godot Version

v4.1.1.stable.official [bd6af8e0e]

Question

I’m trying to send a signal from a node up to its parent. This signal has an integer value being sent up as a parameter like so:

playerInputReceived.emit(number)

This is not a static value and depends upon some previous calculations happening in the child.

In the parent _ready function I have the following:

func _ready():
	connect("playerInputReceived", processInput())

However, the function processInput takes an argument. What I want is for the emitted signal parameter number to be passed into processInput as an argument. How do I direct that signal argument into processInput?

The only approach I can think of is to have a global variable for number to be assigned to and then passing that into processInput or similarily having a number variable in the parent, setting the value from the child node (through get_parent) and going forward. I want to know if there is a more elegant way of doing this though : )

Try this:

signal playerInputReceived(number)
#//at the start of the script

playerInputReceived.emit(_number) #//call it anywhere
func _ready():
	connect("playerInputReceived", processInput)

func processInput(number):
    print("Processed number:", number)

You can learn more about signals here.

1 Like

_ready(): In Object of type 'Node': Attempt to connect nonexistent signal 'playerInputReceived' to callable

I don’t have syntax errors after using the connect function like you’ve shown, bu now this error pops up.

You need to connect the signal with the node you wanted like this:

func _ready():
    node.playerInputReceived.connect(processInput)

Replace the node with the targeted node you wanted to connect.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.