"method expected 0 arguments, but called with 1" despite setting up signal to take an argument

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

Hello,

I’m trying to test out custom signals with parameters in an event bus autoload in Godot 3.5. I have a camera with mouselook with a raycast child node, and I’m trying to print the name of the body the raycast collides with when I click. I have the following code in a script attached to the Events singleton:

signal player_interact(object_under_cursor)

and in my script attached to the world node:

onready var raycast = $Camera/RayCast

<mouselook code>

func _process(_delta):
if Input.is_action_just_pressed("ui_select"):
	Events.emit_signal("player_interact", raycast.get_collider())

and finally, in a script attached to a static body in the scene:

func _ready():
Events.connect("player_interact", self, "_on_player_interact")

func _on_player_interact():
	print(Events.player_interact().object_under_cursor)

But despite the fact that I have the player_interact signal set up to take an argument, I get the following error whenever I click: “emit_signal: Error calling method from signal ‘player_interact’: ‘StaticBody(StaticBody.gd)::_on_player_interact’: Method expected 0 arguments, but called with 1…”

I suspect I’m also doing something wrong in my _on_player_interact function as well, so any other corrections would be greatly appreciated!

:bust_in_silhouette: Reply From: jgodfrey

Your error is in this code:

func _on_player_interact():
    print(Events.player_interact().object_under_cursor)

You’ve set up your signal to pass along a collider, but the ultimate signal handler (above) doesn’t accept that collider arg. It should look something like:

func _on_player_interact(collider):
    print(collider)

Thanks a million! It’s working perfectly now.

enceladus | 2023-05-29 22:40

1 Like