custom signals with parameters/arguments

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Dumuz

I’m trying to make a custom signal with an argument that passes the name of the parent to the function called, I’ve tried:

func _on_add_food_pressed():
    	var food = preload("res://Food.tscn").instance()
    	food.get_child(0).connect("pressed",self, "_feed_prompt(food)")


func _on_add_food_pressed():
    	var food = preload("res://Food.tscn").instance()
    	food.get_child(0).connect("pressed",self, str(_feed_prompt(food)))

but I keep getting the same error:

E 0:00:05.524 emit_signal: Error calling method from signal
‘pressed’: ‘Control(’: Method not found…

How do I make a custom signal with parameters attached?

:bust_in_silhouette: Reply From: Eric Ellingson

One problem is that "_feed_prompt(food)" is supposed to match the name of a function, but since you are trying to define the argument, there is no matching function found. is _feed_prompt a method on Food? if so, you should just call it directly since you’ve already captured the “pressed” signal at this point.

func _on_add_food_pressed():
    var food = preload("res://Food.tscn").instance()

Although, I’m having a hard time understanding what this is trying to accomplish, so it’s possible that it could be done some other way.

If you could share your scene/node structure, that would be helpful

In this context, food is just the variable name for the 'food' scene.

I wanted to be able to have connected signal in the OP pass the ID or name of the instance of food, so when the player uses it to eat, I can call .queue_free on the instance. There can be multiple instances of food, I’m not sure how else to identify to the script which food to delete from the inventory.

Dumuz | 2020-02-12 21:12

what is being “pressed”? Is it a button?

Eric Ellingson | 2020-02-13 04:14

Yes. There is a child node to the food scene, that is a button.

I know that you can connect signals via the Godot interface and add arguments there. But if it can be done there, it should be able to be done in script as well, I would assume.

I want to pass the ID of the button being pressed to the method being called. In this case; _feed_prompt(food). Kinda like the embedded signal in the Tween node: on_Tween_complete(object, key)

Dumuz | 2020-02-13 04:38

Ah, I see.

func _on_add_food_pressed():
    var food = preload("res://Food.tscn").instance()
    food.get_child(0).connect("pressed",self, "_feed_prompt", [ ])

func _feed_prompt(food_name: String) -> void:
    # whatever

So you pass just the string of the function name as the third parameter, and the fourth parameter is an array of all of the values you want passed as arguments to the function when the signal is emitted

Eric Ellingson | 2020-02-13 05:00

I had a feeling it was something like that, but I couldn’t find any documentation on a 4th parameter in the connect method. Thank you!

Dumuz | 2020-02-13 18:25