Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to discard excessive arguments in signal connection #762

Closed
KoBeWi opened this issue Apr 28, 2020 · 6 comments · Fixed by godotengine/godot#53313
Closed

Allow to discard excessive arguments in signal connection #762

KoBeWi opened this issue Apr 28, 2020 · 6 comments · Fixed by godotengine/godot#53313

Comments

@KoBeWi
Copy link
Member

KoBeWi commented Apr 28, 2020

This is partially implemented, see this comment: #762 (comment)


Describe the project you are working on:
Game using lots of signals.

Describe the problem or limitation you are having in your project:
I sometimes want to free an object after finishing animation. I can do it entirely in editor except one thing - I can't connect animation_finished signal to queue_free, because it emits with one parameter, which makes the call fail. In many cases, I have to add a script just to connect this signal to a method taking unused argument and calling queue_free. It's meh. Recently I had my custom signal connected to queue_free (without script), but later decided that it needs additional argument in some cases, so the nodes that didn't need this argument are now broken xd (I have to add a script just to work around this limitation now)

Other use examples include methods like

func some_callback(whatever):
    ...

If I wouldn't have disabled warnings, I would get "unused parameter" here.

Here's an old issue about this proposal: godotengine/godot#21844
Some people already supported it >_>

Describe the feature / enhancement and how it helps to overcome the problem or limitation:
There could be a flag for connect called CONNECT_DISCARD_EXTRA (or some other name) that makes excessive arguments discarded, so that the method would call instead of throwing "Too many arguments" error.

Alternatively this could be a default behavior, but it might not be a good idea.

Describe how your proposal will work, with code, pseudocode, mockups, and/or diagrams:
Check this PR: godotengine/godot#38289

If this enhancement will not be used often, can it be worked around with a few lines of script?:
Well, kind of. The thing is that with this feature I wouldn't need some of these scripts.

Is there a reason why this should be core and not an add-on in the asset library?:
Because it's about core. It can't be an add-on (although it would be fine for me if it could).

@nathanfranke
Copy link
Contributor

I would like to connect this with a comment I posted here godotengine/godot#16863 (comment)

We could have two signal arguments: old_text and new_text, both being optional (If this proposal is fulfilled) which allows these function signals:

func _text_changed(): pass
func _text_changed(old_text): pass
func _text_changed(old_text, new_text): pass

@KoBeWi
Copy link
Member Author

KoBeWi commented Oct 11, 2020

Sooo godotengine/godot#42683 basically implemented this, but it's not available from editor yet, so I'll keep it open until then.

@NathanLovato
Copy link

Totally second this, it's a common case that leads to code indirection, having to jump from ready to the callback function. This proposal would definitely help shave off some unnecessary lines of code.

@2fd5
Copy link

2fd5 commented Jun 21, 2023

I am unsure why this is closed or rather if it addresses the proposal. Using unbind requires to know how many arguments are needed to be unbind.

It does not solve my use case. I do listen to certain events to track actions performed by the player to update goals statuses.
I do have custom resource with goal, that stores a signal to listen to in a variable. Then with a code I do connect to all goals signals to listen to on the level start.

Workaround, To connect and ignore the arguments to the signal, and to ignore the number of arguments I can give to unbind some big number. It will generate an error but the callable will be called with effectively ignoring the arguments.

E 0:00:32:0304   MouseSelection.gd:90 @ _action_open_tile_details(): Error calling from signal 'panels_selected' to callable: 'Node2D(level_advisor.gd)::_on_panels_selected': Method expected 10 arguments, but called with 0.
  <C++ Source>   core/object/object.cpp:1080 @ emit_signalp()

If signal connect would accept in its argument flags, new flag in ConnectFlags like CONNECT_IGNORE_ARGUMENTS or something it would make a much more cleaner solution IMHO.

Alternatively if Callable.unbind() could accept let's say (-1) to ignore or unbind all arguments.

Currently I would have to do a parser to get all my events and get number of arguments for each so that I can cleanly use unbind.

Not to mention to the workaround might stop working if unbind would be changed in a way that it will not call callable.

Am I missing something or am I going about it in the wrong way ?

This all is the relation to Godot 4.

@KoBeWi
Copy link
Member Author

KoBeWi commented Jun 21, 2023

If you are connecting a signal, you know beforehand how many arguments it has, no? Or are you emitting the same signal with different number of arguments? Maybe you should emit 2 signals? (one with arguments and one without)

@2fd5
Copy link

2fd5 commented Jul 5, 2023

If you are connecting a signal, you know beforehand how many arguments it has, no? Or are you emitting the same signal with different number of arguments? Maybe you should emit 2 signals? (one with arguments and one without)

Thank you for your answer and suggestions, emitting 2 separate signals for different purposes might be the solution.
It certainly solve the issue for me.

I think it sill does not addresses this proposal and use case where you want pragmatically connect to certain signals, and ignore any number of arguments in the connection.

Example use-case would be some observer, where you want to listen to list of signals, ignore the arguments as observer just wants to count the number each signal is being called.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants