+3 votes

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)")

and

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(GUI.gd)::Null': Method not found..

How do I make a custom signal with parameters attached?

in Engine by (293 points)

1 Answer

+2 votes
Best answer

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()

    food.get_child(0)._feed_prompt(food)

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

by (1,599 points)
selected by

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.

what is being "pressed"? Is it a button?

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)

Ah, I see.

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

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

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!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.